-
-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathsign-in.tsx
More file actions
177 lines (171 loc) · 4.63 KB
/
Copy pathsign-in.tsx
File metadata and controls
177 lines (171 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { useQuery } from "@tanstack/react-query";
import * as Linking from "expo-linking";
import { Link, router } from "expo-router";
import { Github, Mail } from "lucide-react-native";
import { useState } from "react";
import {
KeyboardAvoidingView,
Platform,
StyleSheet,
Text,
View,
} from "react-native";
import { ErrorBanner, Button, Field, Screen } from "@/components/ui";
import { getAuthConfig } from "@/lib/api";
import { authClient } from "@/lib/auth";
import { colors, spacing, typography } from "@/theme";
export default function SignInScreen() {
const config = useQuery({
queryKey: ["auth-config"],
queryFn: getAuthConfig,
});
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
async function signInEmail() {
setLoading(true);
setError(null);
try {
const result = await authClient.signIn.email({ email, password });
if (result?.error) {
throw new Error(result.error.message || "Could not sign in");
}
router.replace("/");
} catch (err: any) {
setError(err.message || "Could not sign in");
} finally {
setLoading(false);
}
}
async function signInSocial(provider: "github" | "google" | "microsoft") {
setLoading(true);
setError(null);
try {
const result = await authClient.signIn.social({
provider,
callbackURL: Linking.createURL("/"),
});
if (result?.error) {
throw new Error(
result.error.message || "Could not start social sign in",
);
}
} catch (err: any) {
setError(err.message || "Could not start social sign in");
} finally {
setLoading(false);
}
}
const providers = config.data?.socialProviders;
return (
<Screen>
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : undefined}
style={styles.wrap}
>
<View style={styles.header}>
<Text style={styles.title}>Better Chatbot</Text>
<Text style={styles.subtitle}>Sign in to continue your chats.</Text>
</View>
<ErrorBanner message={error} />
{config.data?.emailAndPasswordEnabled !== false ? (
<View style={styles.form}>
<Field
autoComplete="email"
keyboardType="email-address"
label="Email"
onChangeText={setEmail}
value={email}
/>
<Field
autoComplete="password"
label="Password"
onChangeText={setPassword}
secureTextEntry
value={password}
/>
<Button
disabled={!email || !password}
icon={Mail}
label="Sign in"
loading={loading}
onPress={signInEmail}
/>
</View>
) : null}
<View style={styles.social}>
{providers?.google ? (
<Button
label="Continue with Google"
loading={loading}
onPress={() => signInSocial("google")}
variant="secondary"
/>
) : null}
{providers?.github ? (
<Button
icon={Github}
label="Continue with GitHub"
loading={loading}
onPress={() => signInSocial("github")}
variant="secondary"
/>
) : null}
{providers?.microsoft ? (
<Button
label="Continue with Microsoft"
loading={loading}
onPress={() => signInSocial("microsoft")}
variant="secondary"
/>
) : null}
</View>
{config.data?.signUpEnabled !== false ? (
<Text style={styles.footer}>
New here?{" "}
<Link href="/sign-up" style={styles.link}>
Create account
</Link>
</Text>
) : null}
</KeyboardAvoidingView>
</Screen>
);
}
const styles = StyleSheet.create({
wrap: {
flex: 1,
justifyContent: "center",
width: "100%",
maxWidth: 420,
alignSelf: "center",
gap: 20,
paddingHorizontal: spacing.screenX,
},
header: {
gap: 6,
},
title: {
color: colors.foreground,
...typography.title,
},
subtitle: {
color: colors.mutedForeground,
...typography.body,
},
form: {
gap: 14,
},
social: {
gap: 10,
},
footer: {
color: colors.mutedForeground,
textAlign: "center",
},
link: {
color: colors.foreground,
fontWeight: "700",
},
});