-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.js
164 lines (151 loc) · 4.39 KB
/
App.js
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
import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, Button, StyleSheet, ScrollView } from 'react-native';
import { initializeApp } from '@firebase/app';
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged, signOut } from '@firebase/auth';
const firebaseConfig = {
apiKey: "YOUR - apiKey",
authDomain: "YOUR - authDomain",
projectId: "YOUR - projectId",
storageBucket: "YOUR - storageBucket",
messagingSenderId: "YOUR - messagingSenderId",
appId: "YOUR - appId",
measurementId: "YOUR - measurementId"
};
const app = initializeApp(firebaseConfig);
const AuthScreen = ({ email, setEmail, password, setPassword, isLogin, setIsLogin, handleAuthentication }) => {
return (
<View style={styles.authContainer}>
<Text style={styles.title}>{isLogin ? 'Sign In' : 'Sign Up'}</Text>
<TextInput
style={styles.input}
value={email}
onChangeText={setEmail}
placeholder="Email"
autoCapitalize="none"
/>
<TextInput
style={styles.input}
value={password}
onChangeText={setPassword}
placeholder="Password"
secureTextEntry
/>
<View style={styles.buttonContainer}>
<Button title={isLogin ? 'Sign In' : 'Sign Up'} onPress={handleAuthentication} color="#3498db" />
</View>
<View style={styles.bottomContainer}>
<Text style={styles.toggleText} onPress={() => setIsLogin(!isLogin)}>
{isLogin ? 'Need an account? Sign Up' : 'Already have an account? Sign In'}
</Text>
</View>
</View>
);
}
const AuthenticatedScreen = ({ user, handleAuthentication }) => {
return (
<View style={styles.authContainer}>
<Text style={styles.title}>Welcome</Text>
<Text style={styles.emailText}>{user.email}</Text>
<Button title="Logout" onPress={handleAuthentication} color="#e74c3c" />
</View>
);
};
export default App = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [user, setUser] = useState(null); // Track user authentication state
const [isLogin, setIsLogin] = useState(true);
const auth = getAuth(app);
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
setUser(user);
});
return () => unsubscribe();
}, [auth]);
const handleAuthentication = async () => {
try {
if (user) {
// If user is already authenticated, log out
console.log('User logged out successfully!');
await signOut(auth);
} else {
// Sign in or sign up
if (isLogin) {
// Sign in
await signInWithEmailAndPassword(auth, email, password);
console.log('User signed in successfully!');
} else {
// Sign up
await createUserWithEmailAndPassword(auth, email, password);
console.log('User created successfully!');
}
}
} catch (error) {
console.error('Authentication error:', error.message);
}
};
return (
<ScrollView contentContainerStyle={styles.container}>
{user ? (
// Show user's email if user is authenticated
<AuthenticatedScreen user={user} handleAuthentication={handleAuthentication} />
) : (
// Show sign-in or sign-up form if user is not authenticated
<AuthScreen
email={email}
setEmail={setEmail}
password={password}
setPassword={setPassword}
isLogin={isLogin}
setIsLogin={setIsLogin}
handleAuthentication={handleAuthentication}
/>
)}
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
flexGrow: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 16,
backgroundColor: '#f0f0f0',
},
authContainer: {
width: '80%',
maxWidth: 400,
backgroundColor: '#fff',
padding: 16,
borderRadius: 8,
elevation: 3,
},
title: {
fontSize: 24,
marginBottom: 16,
textAlign: 'center',
},
input: {
height: 40,
borderColor: '#ddd',
borderWidth: 1,
marginBottom: 16,
padding: 8,
borderRadius: 4,
},
buttonContainer: {
marginBottom: 16,
},
toggleText: {
color: '#3498db',
textAlign: 'center',
},
bottomContainer: {
marginTop: 20,
},
emailText: {
fontSize: 18,
textAlign: 'center',
marginBottom: 20,
},
});