-
-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathAuthSheet.tsx
More file actions
264 lines (233 loc) · 7.79 KB
/
Copy pathAuthSheet.tsx
File metadata and controls
264 lines (233 loc) · 7.79 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import React, {useState, useEffect} from 'react';
import {View, Alert} from 'react-native';
import {observer} from 'mobx-react-lite';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {Text, Button, TextInput, ActivityIndicator} from 'react-native-paper';
import {GoogleIcon} from '../../../assets/icons';
import {useTheme} from '../../../hooks';
import {Sheet} from '../../Sheet';
import {createStyles} from './styles';
import {authService, PalsHubErrorHandler} from '../../../services';
interface AuthSheetProps {
isVisible: boolean;
onClose: () => void;
}
const GoogleButtonIcon = () => <GoogleIcon width={20} height={20} />;
export const AuthSheet: React.FC<AuthSheetProps> = observer(
({isVisible, onClose}) => {
const theme = useTheme();
const insets = useSafeAreaInsets();
const styles = createStyles(theme);
const [isSignUp, setIsSignUp] = useState(false);
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [fullName, setFullName] = useState('');
const [isLoading, setIsLoading] = useState(false);
const authState = authService.authState;
// Close sheet automatically when user becomes authenticated
useEffect(() => {
if (authState.isAuthenticated && isVisible) {
onClose();
}
}, [authState.isAuthenticated, isVisible, onClose]);
const handleEmailAuth = async () => {
if (!email.trim() || !password.trim()) {
Alert.alert('Error', 'Please fill in all required fields.');
return;
}
if (isSignUp && !fullName.trim()) {
Alert.alert('Error', 'Please enter your full name.');
return;
}
try {
setIsLoading(true);
authService.clearError();
if (isSignUp) {
const ok = await authService.signUpWithEmail(
email.trim(),
password,
fullName.trim(),
);
if (ok) {
Alert.alert(
'Account Created',
'Please check your email to verify your account.',
[{text: 'OK', onPress: onClose}],
);
}
} else {
const ok = await authService.signInWithEmail(email.trim(), password);
if (ok) {
Alert.alert('Welcome Back!', 'You have successfully signed in.', [
{text: 'OK', onPress: onClose},
]);
}
}
} catch (error) {
const errorInfo = PalsHubErrorHandler.handle(error);
Alert.alert('Authentication Error', errorInfo.userMessage);
} finally {
setIsLoading(false);
}
};
const handleGoogleAuth = async () => {
try {
setIsLoading(true);
authService.clearError();
await authService.signInWithGoogle();
// Sheet will close automatically via useEffect when auth state changes
} catch (error) {
const errorInfo = PalsHubErrorHandler.handle(error);
Alert.alert('Google Sign-In Error', errorInfo.userMessage);
} finally {
setIsLoading(false);
}
};
const handleForgotPassword = async () => {
if (!email.trim()) {
Alert.alert('Error', 'Please enter your email address first.');
return;
}
try {
setIsLoading(true);
const ok = await authService.resetPassword(email.trim());
if (ok) {
Alert.alert(
'Password Reset',
'Check your email for password reset instructions.',
[{text: 'OK'}],
);
}
} catch (error) {
const errorInfo = PalsHubErrorHandler.handle(error);
Alert.alert('Error', errorInfo.userMessage);
} finally {
setIsLoading(false);
}
};
const resetForm = () => {
setEmail('');
setPassword('');
setFullName('');
setIsSignUp(false);
authService.clearError();
};
const handleClose = () => {
resetForm();
onClose();
};
return (
<Sheet
title={isSignUp ? 'Create Account' : 'Sign In'}
isVisible={isVisible}
onClose={handleClose}
snapPoints={['85%']}>
<Sheet.ScrollView
contentContainerStyle={[
styles.authSheet,
{paddingBottom: insets.bottom + 16},
]}>
{/* Loading Indicator */}
{authState.isLoading && (
<View style={styles.authLoadingContainer}>
<ActivityIndicator size="large" color={theme.colors.primary} />
<Text style={styles.authSubtitle}>Signing you in...</Text>
</View>
)}
{/* Error Message */}
{authState.error && (
<Text style={[styles.authSubtitle, styles.authErrorText]}>
{authState.error}
</Text>
)}
{/* Email/Password Form */}
<View style={styles.authForm}>
{isSignUp && (
<TextInput
testID="full-name-input"
label="Full Name"
value={fullName}
onChangeText={setFullName}
style={styles.authInput}
mode="outlined"
disabled={isLoading || authState.isLoading}
/>
)}
<TextInput
testID="email-input"
label="Email"
value={email}
onChangeText={setEmail}
keyboardType="email-address"
autoCapitalize="none"
style={styles.authInput}
mode="outlined"
disabled={isLoading || authState.isLoading}
/>
<TextInput
testID="password-input"
label="Password"
value={password}
onChangeText={setPassword}
secureTextEntry
style={styles.authInput}
mode="outlined"
disabled={isLoading || authState.isLoading}
/>
<Button
mode="contained"
onPress={handleEmailAuth}
loading={isLoading}
disabled={authState.isLoading}
style={styles.authButton}
contentStyle={styles.authButtonContent}>
{isSignUp ? 'Create Account' : 'Sign In'}
</Button>
{!isSignUp && (
<Button
mode="text"
onPress={handleForgotPassword}
disabled={isLoading || authState.isLoading}>
Forgot Password?
</Button>
)}
</View>
{/* Divider */}
<View style={styles.authDivider}>
<View style={styles.authDividerLine} />
<Text style={styles.authDividerText}>or</Text>
<View style={styles.authDividerLine} />
</View>
{/* Google Sign-In */}
<Button
mode="outlined"
onPress={handleGoogleAuth}
loading={isLoading}
disabled={authState.isLoading}
style={styles.authSocialButton}
contentStyle={styles.authButtonContent}
icon={GoogleButtonIcon}>
Continue with Google
</Button>
{/* Toggle Sign Up/Sign In */}
<View style={styles.authToggle}>
<Text style={styles.authToggleText}>
{isSignUp
? 'Already have an account? '
: "Don't have an account? "}
</Text>
<Button
mode="text"
onPress={() => setIsSignUp(!isSignUp)}
disabled={isLoading || authState.isLoading}
compact
labelStyle={styles.authToggleLink}
contentStyle={styles.authToggleButtonContent}>
{isSignUp ? 'Sign In' : 'Sign Up'}
</Button>
</View>
</Sheet.ScrollView>
</Sheet>
);
},
);