-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuth.jsx
111 lines (102 loc) · 3.24 KB
/
Auth.jsx
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
import React, { useState } from 'react';
import { Alert, StyleSheet, View, Image } from 'react-native';
import { supabase } from './lib/supabase'
import { Button, Input } from 'react-native-elements';
import { useNavigation } from '@react-navigation/native'; // Import useNavigation
import axios from 'axios';
export default function Auth() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const navigation = useNavigation(); // Initialize useNavigation
async function signInWithEmail() {
const { error } = await supabase.auth.signInWithPassword({
email: email,
password: password,
});
await supabase.auth.getSession();
if (error) {
Alert.alert(error.message);
} else {
// Redirect to the "Home" screen on successful sign-in
const apiURL = `${process.env.EXPO_PUBLIC_BACKEND_BASE_URI}/maps/init`;
supabase.auth.getUser().then((res) => {
const userId = res.data.user.id;
axios.post(apiURL, {
userId: userId
}).then((res) => {
console.log(res.data);
})
setLoading(false);
navigation.navigate('Home', {userId}, navigation)
});
}
}
async function signUpWithEmail() {
const {
data: { session },
error,
} = await supabase.auth.signUp({
email: email,
password: password,
});
if (error) Alert.alert(error.message);
if (!session) Alert.alert('Please check your inbox for email verification!');
}
return (
<View style={styles.container}>
<Image
source={require('./assets/white-logo-removebg-preview.png')} // Provide the correct path to your image
style={styles.logo}
/>
<View style={[styles.verticallySpaced, styles.mt20]}>
<Input
label="Email"
leftIcon={{ type: 'font-awesome', name: 'envelope' }}
onChangeText={(text) => setEmail(text)}
value={email}
placeholder="[email protected]"
autoCapitalize={'none'}
/>
</View>
<View style={styles.verticallySpaced}>
<Input
label="Password"
leftIcon={{ type: 'font-awesome', name: 'lock' }}
onChangeText={(text) => setPassword(text)}
value={password}
secureTextEntry={true}
placeholder="Password"
autoCapitalize={'none'}
/>
</View>
<View style={[styles.verticallySpaced, styles.mt20]}>
<Button title="Sign in" disabled={loading} onPress={() => signInWithEmail()} />
</View>
<View style={styles.verticallySpaced}>
<Button title="Sign up" disabled={loading} onPress={() => signUpWithEmail()} />
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
marginTop: 0,
padding: 12,
alignItems: 'center',
},
verticallySpaced: {
paddingTop: 4,
paddingBottom: 4,
alignSelf: 'stretch',
},
mt20: {
marginTop: 20,
},
logo: {
marginTop: 0,
width: 200, // Adjust the width as needed
height: 200, // Adjust the height as needed
marginBottom: 0, // Add margin at the bottom to space it from other elements
},
});