-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthTD.tsx
More file actions
229 lines (212 loc) · 6.63 KB
/
Copy pathauthTD.tsx
File metadata and controls
229 lines (212 loc) · 6.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React, { useState } from "react";
import { getFirestore, doc, setDoc } from "firebase/firestore";
import { Stack } from "expo-router";
import {
View,
Text,
TextInput,
TouchableOpacity,
SafeAreaView,
ActivityIndicator,
} from "react-native";
import { useRouter } from "expo-router";
import {
getAuth,
signInWithEmailAndPassword,
createUserWithEmailAndPassword,
signInAnonymously,
updateProfile,
} from "firebase/auth";
const auth = getAuth();
const db = getFirestore();
export default function Auth() {
const router = useRouter();
const [isLogin, setIsLogin] = useState(true); // Toggle between login and signup
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState(""); // Only used for signup
const [name, setName] = useState(""); // Only used for signup
const [loading, setLoading] = useState(false);
const handleAuth = async () => {
if (loading) return; // Prevent duplicate submissions
setLoading(true);
try {
if (isLogin) {
// Login logic
await signInWithEmailAndPassword(auth, email.trim(), password);
alert("Login successful!");
router.replace("(tabs)"); // Redirect to the tabs page
} else {
// Signup logic
if (password !== confirmPassword) {
alert("Passwords do not match!");
setLoading(false);
return;
}
// Firebase Authentication
const userCredential = await createUserWithEmailAndPassword(auth, email.trim(), password);
const user = userCredential.user;
if (user) {
await updateProfile(user, { displayName: name });
const userData = {
email: email.trim(),
displayName: name,
movie_list: [],
downloads: [],
};
const userRef = doc(db, "users", user.uid); // Use uid as the document ID
await setDoc(userRef, userData);
}
alert("Account created successfully!");
setIsLogin(true); // Switch to login after successful signup
}
} catch (error) {
console.error("Authentication error: ", error);
alert(error.message || "Authentication failed");
} finally {
setLoading(false);
}
};
const handleGuestSignIn = async () => {
if (loading) return; // Prevent duplicate submissions
setLoading(true);
try {
// Sign in anonymously
const userCredential = await signInAnonymously(auth);
const user = userCredential.user;
// Optionally, save guest user data in Firestore
const userRef = doc(db, "users", user.uid);
await setDoc(userRef, {
displayName: "Guest",
movie_list: [],
downloads: [],
});
alert("Signed in as Guest!");
router.replace("(tabs)"); // Redirect to the tabs page
} catch (error) {
console.error("Guest sign-in error: ", error);
alert(error.message || "Failed to sign in as Guest");
} finally {
setLoading(false);
}
};
return (
<SafeAreaView style={{ flex: 1, backgroundColor: "#1a1a2e" }}>
{loading && (
<View
style={{
position: "absolute",
width: "100%",
height: "100%",
backgroundColor: "rgba(0, 0, 0, 0.5)",
justifyContent: "center",
alignItems: "center",
zIndex: 100,
}}
>
<ActivityIndicator size="large" color="#FF6A3D" />
</View>
)}
<Stack.Screen options={{ headerShown: false }} />
<View style={{ flex: 1, justifyContent: "center", alignItems: "center", padding: 24 }}>
<Text style={{ color: "#fff", fontSize: 18, fontWeight: "500", marginBottom: 24 }}>
{isLogin ? "Login" : "Sign Up"}
</Text>
{!isLogin && (
<TextInput
style={{
width: "100%",
backgroundColor: "rgba(255,255,255,0.1)",
borderRadius: 8,
padding: 12,
marginBottom: 16,
color: "#fff",
}}
placeholder="Name"
placeholderTextColor="#ccc"
value={name}
onChangeText={setName}
/>
)}
<TextInput
style={{
width: "100%",
backgroundColor: "rgba(255,255,255,0.1)",
borderRadius: 8,
padding: 12,
marginBottom: 16,
color: "#fff",
}}
placeholder="Email"
placeholderTextColor="#ccc"
value={email}
onChangeText={setEmail}
keyboardType="email-address"
/>
<TextInput
style={{
width: "100%",
backgroundColor: "rgba(255,255,255,0.1)",
borderRadius: 8,
padding: 12,
marginBottom: 16,
color: "#fff",
}}
placeholder="Password"
placeholderTextColor="#ccc"
value={password}
onChangeText={setPassword}
secureTextEntry
/>
{!isLogin && (
<TextInput
style={{
width: "100%",
backgroundColor: "rgba(255,255,255,0.1)",
borderRadius: 8,
padding: 12,
marginBottom: 16,
color: "#fff",
}}
placeholder="Confirm Password"
placeholderTextColor="#ccc"
value={confirmPassword}
onChangeText={setConfirmPassword}
secureTextEntry
/>
)}
<TouchableOpacity
style={{
backgroundColor: "#FF6A3D",
width: "100%",
padding: 16,
borderRadius: 8,
alignItems: "center",
}}
onPress={handleAuth}
>
<Text style={{ color: "#fff", fontSize: 16, fontWeight: "bold" }}>
{isLogin ? "Login" : "Sign Up"}
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => setIsLogin(!isLogin)} style={{ marginTop: 16 }}>
<Text style={{ color: "#f88e6e", textDecorationLine: "underline" }}>
{isLogin ? "Don’t have an account? Sign up" : "Already have an account? Login"}
</Text>
</TouchableOpacity>
<TouchableOpacity
style={{
marginTop: 16,
padding: 16,
backgroundColor: "#555",
borderRadius: 8,
alignItems: "center",
}}
onPress={handleGuestSignIn}
>
<Text style={{ color: "#fff", fontSize: 16 }}>Sign in as Guest</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
}