feat(mobile): let admins create accounts
This commit is contained in:
3 files changed
+209
-1
No files matched your search
@@ -3,6 +3,7 @@ import { useAction, useMutation, useQuery } from "convex/react"
|
||||
import { Image } from "expo-image"
|
||||
import * as ImagePicker from "expo-image-picker"
|
||||
import { useState } from "react"
|
||||
import { router } from "expo-router"
|
||||
import {
|
||||
Platform,
|
||||
Pressable,
|
||||
@@ -459,7 +460,7 @@ function UsersSection({
|
||||
<SettingsCard title="Sign Up">
|
||||
<SwitchRow
|
||||
title="Disable account creation"
|
||||
subtitle="Stop people from registering themselves. Admins can still create accounts from the web app."
|
||||
subtitle="Stop people from registering themselves. Admins can still create accounts."
|
||||
value={appSettings?.disableAccountCreation ?? false}
|
||||
onValueChange={(checked) =>
|
||||
void setDisableCreation({ disabled: checked }).catch(
|
||||
@@ -473,6 +474,10 @@ function UsersSection({
|
||||
title="People"
|
||||
subtitle="Manage who can access this server."
|
||||
>
|
||||
<Button
|
||||
title="Create account"
|
||||
onPress={() => router.push("/(app)/create-account")}
|
||||
/>
|
||||
<View style={styles.userList}>
|
||||
{users.map((user, index) => (
|
||||
<View
|
||||
|
||||
@@ -75,6 +75,18 @@ export default function AppLayout() {
|
||||
gestureDirection: "vertical",
|
||||
}}
|
||||
/>
|
||||
<Stack.Screen
|
||||
name="create-account"
|
||||
options={{
|
||||
title: "Create account",
|
||||
presentation: "modal",
|
||||
headerLargeTitle: false,
|
||||
headerTransparent: false,
|
||||
headerStyle: { backgroundColor: "#000" },
|
||||
headerTintColor: "#fff",
|
||||
contentStyle: { backgroundColor: "#000" },
|
||||
}}
|
||||
/>
|
||||
<Stack.Screen
|
||||
name="player"
|
||||
options={{
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
import { useAction, useQuery } from "convex/react"
|
||||
import { ConvexError } from "convex/values"
|
||||
import { router, Stack } from "expo-router"
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
import {
|
||||
KeyboardAvoidingView,
|
||||
Platform,
|
||||
Pressable,
|
||||
ScrollView,
|
||||
StyleSheet,
|
||||
Switch,
|
||||
Text,
|
||||
View,
|
||||
} from "react-native"
|
||||
import { SafeAreaView } from "react-native-safe-area-context"
|
||||
import { toast } from "sonner-native"
|
||||
|
||||
import { Button, Input, LoadingView } from "@/components/ui/primitives"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
|
||||
export default function CreateAccountScreen() {
|
||||
const user = useQuery(api.auth.currentUser)
|
||||
const createUser = useAction(api.users.adminCreateUser)
|
||||
const [name, setName] = useState("")
|
||||
const [email, setEmail] = useState("")
|
||||
const [password, setPassword] = useState("")
|
||||
const [isAdmin, setIsAdmin] = useState(false)
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const pending = useRef(false)
|
||||
const mounted = useRef(true)
|
||||
|
||||
useEffect(() => {
|
||||
mounted.current = true
|
||||
return () => {
|
||||
mounted.current = false
|
||||
}
|
||||
}, [])
|
||||
|
||||
async function handleCreate() {
|
||||
if (pending.current || !user?.isAdmin) return
|
||||
setError(null)
|
||||
if (!name.trim() || !email.trim().includes("@") || password.length < 8) {
|
||||
setError(
|
||||
"Enter a name, valid email, and password of at least 8 characters."
|
||||
)
|
||||
return
|
||||
}
|
||||
pending.current = true
|
||||
setSubmitting(true)
|
||||
try {
|
||||
await createUser({
|
||||
name: name.trim(),
|
||||
email: email.trim().toLowerCase(),
|
||||
password,
|
||||
isAdmin,
|
||||
})
|
||||
toast.success("Account created")
|
||||
if (mounted.current) router.back()
|
||||
} catch (error) {
|
||||
if (!mounted.current) return
|
||||
setError(
|
||||
error instanceof ConvexError && typeof error.data === "string"
|
||||
? error.data
|
||||
: "Could not create account. Try again."
|
||||
)
|
||||
} finally {
|
||||
pending.current = false
|
||||
if (mounted.current) setSubmitting(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<SafeAreaView edges={["bottom", "left", "right"]} style={styles.screen}>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
headerLeft: () => (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
onPress={() => router.back()}
|
||||
style={styles.back}
|
||||
>
|
||||
<Text style={styles.text}>‹ Users</Text>
|
||||
</Pressable>
|
||||
),
|
||||
gestureEnabled: !submitting,
|
||||
}}
|
||||
/>
|
||||
{user === undefined ? (
|
||||
<LoadingView />
|
||||
) : !user?.isAdmin ? (
|
||||
<Text style={[styles.text, styles.message]}>
|
||||
Admin access required.
|
||||
</Text>
|
||||
) : (
|
||||
<KeyboardAvoidingView
|
||||
style={styles.screen}
|
||||
behavior={Platform.OS === "ios" ? "padding" : undefined}
|
||||
keyboardVerticalOffset={Platform.OS === "ios" ? 100 : 0}
|
||||
>
|
||||
<ScrollView
|
||||
contentContainerStyle={styles.form}
|
||||
keyboardShouldPersistTaps="handled"
|
||||
keyboardDismissMode="on-drag"
|
||||
>
|
||||
<Text style={styles.text}>Name</Text>
|
||||
<Input
|
||||
accessibilityLabel="Name"
|
||||
value={name}
|
||||
onChangeText={setName}
|
||||
autoCapitalize="words"
|
||||
autoComplete="off"
|
||||
editable={!submitting}
|
||||
style={styles.input}
|
||||
/>
|
||||
<Text style={styles.text}>Email</Text>
|
||||
<Input
|
||||
accessibilityLabel="Email"
|
||||
value={email}
|
||||
onChangeText={setEmail}
|
||||
keyboardType="email-address"
|
||||
autoCapitalize="none"
|
||||
autoCorrect={false}
|
||||
autoComplete="off"
|
||||
editable={!submitting}
|
||||
style={styles.input}
|
||||
/>
|
||||
<Text style={styles.text}>Password</Text>
|
||||
<Input
|
||||
accessibilityLabel="Password"
|
||||
value={password}
|
||||
onChangeText={setPassword}
|
||||
secureTextEntry
|
||||
autoCapitalize="none"
|
||||
autoCorrect={false}
|
||||
autoComplete="new-password"
|
||||
placeholder="At least 8 characters"
|
||||
editable={!submitting}
|
||||
style={styles.input}
|
||||
/>
|
||||
<View style={styles.row}>
|
||||
<Text style={styles.text}>Grant admin access</Text>
|
||||
<Switch
|
||||
accessibilityLabel="Grant admin access"
|
||||
value={isAdmin}
|
||||
onValueChange={setIsAdmin}
|
||||
disabled={submitting}
|
||||
/>
|
||||
</View>
|
||||
{error ? (
|
||||
<Text accessibilityRole="alert" style={styles.error}>
|
||||
{error}
|
||||
</Text>
|
||||
) : null}
|
||||
<Button
|
||||
title={submitting ? "Creating account…" : "Create account"}
|
||||
disabled={submitting}
|
||||
onPress={() => void handleCreate()}
|
||||
tint={{ background: "#fff", foreground: "#000" }}
|
||||
/>
|
||||
</ScrollView>
|
||||
</KeyboardAvoidingView>
|
||||
)}
|
||||
</SafeAreaView>
|
||||
)
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
screen: { flex: 1, backgroundColor: "#000" },
|
||||
form: { padding: 24, gap: 12 },
|
||||
text: { color: "#fff", fontSize: 16 },
|
||||
input: {
|
||||
backgroundColor: "#000",
|
||||
color: "#fff",
|
||||
borderRadius: 0,
|
||||
borderWidth: 0,
|
||||
borderBottomWidth: 1,
|
||||
borderColor: "#555",
|
||||
paddingHorizontal: 0,
|
||||
marginBottom: 12,
|
||||
},
|
||||
row: {
|
||||
flexDirection: "row",
|
||||
alignItems: "center",
|
||||
justifyContent: "space-between",
|
||||
paddingVertical: 12,
|
||||
},
|
||||
back: { minHeight: 44, justifyContent: "center" },
|
||||
message: { padding: 24 },
|
||||
error: { color: "#ff7777" },
|
||||
})
|
||||
Reference in new issue
Block a user