-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
136 lines (125 loc) · 3.26 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
import { StatusBar } from 'expo-status-bar';
import React, { useEffect, useState } from 'react';
import { Pressable, StyleSheet, Text, TextInput, View } from 'react-native';
import { AuthProvider, useAuth } from 'react-native-laravel-sanctum';
function Login() {
const { login, getToken, updateUser, setUserIsAuthenticated, isAuthenticated, logout, currentUser } = useAuth();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleLogin = async () => {
console.log(await getToken());
const tokenObtained = await login(email, password, 'Test-Device');
console.log(tokenObtained);
if (tokenObtained) {
const user = await updateUser();
console.log(user);
if (user.id) {
setUserIsAuthenticated(true);
}
}
};
return (
<View style={styles.view}>
<Text style={styles.caption}>You are not logged in.</Text>
<TextInput
style={styles.textInput}
onChangeText={text => setEmail(text)}
value={email}
placeholder='Email'
/>
<TextInput
style={styles.textInput}
onChangeText={text => setPassword(text)}
value={password}
secureTextEntry
placeholder='Password'
/>
<Pressable style={styles.button} onPress={handleLogin}>
<Text style={styles.buttonText}>Login</Text>
</Pressable>
</View>
);
}
function Logout() {
const { logout, setUserIsAuthenticated, updateUser, currentUser, getToken } = useAuth();
const handleLogout = async () => {
const isLoggedOut = await logout();
if (isLoggedOut) {
setUserIsAuthenticated(false);
await updateUser();
}
};
return (
<View style={styles.view}>
<Text style={styles.caption}>You are logged in as {currentUser ? currentUser.name : ''}</Text>
<Pressable style={styles.button} onPress={handleLogout}>
<Text style={styles.buttonText}>Logout</Text>
</Pressable>
</View>
);
}
function Example() {
const { isAuthenticated } = useAuth();
return (
<View style={styles.view}>
{isAuthenticated ? <Logout /> : <Login />}
</View>
);
}
export default function App() {
const config = {
loginUrl: 'http://127.0.0.1:8000/api/sanctum/token',
logoutUrl: 'http://127.0.0.1:8000/api/logout',
userUrl: 'http://127.0.0.1:8000/api/user'
};
return (
<AuthProvider config={config}>
<View style={styles.container}>
<Example />
<StatusBar style='auto' />
</View>
</AuthProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
button: {
backgroundColor: '#f59e0b',
padding: 12,
minWidth: '80%',
borderRadius: 5,
alignItems: 'center',
},
buttonText: {
fontWeight: '800',
letterSpacing: 0.5,
lineHeight: 20,
color: '#fff',
},
textInput: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
minWidth: '80%',
padding: 10,
borderRadius: 5,
marginBottom: 10,
},
caption: {
marginBottom: 10,
fontWeight: '800',
letterSpacing: 0.5,
lineHeight: 20,
color: '#000',
textAlign: 'center',
},
view: {
padding: 15,
alignItems: 'center',
},
});