|
1 | 1 | import * as React from 'react'; |
| 2 | +import { StatusBar } from 'expo-status-bar'; |
| 3 | +import { useState } from 'react'; |
| 4 | +import { Pressable, StyleSheet, Text, TextInput, View } from 'react-native'; |
| 5 | +import { AuthProvider, useAuth } from 'react-native-laravel-sanctum'; |
2 | 6 |
|
3 | | -import { StyleSheet, View } from 'react-native'; |
| 7 | +function Login() { |
| 8 | + const { login, updateUser, setUserIsAuthenticated } = useAuth(); |
| 9 | + const [email, setEmail] = useState(''); |
| 10 | + const [password, setPassword] = useState(''); |
| 11 | + |
| 12 | + const handleLogin = async () => { |
| 13 | + const tokenObtained = await login(email, password, 'Test-Device'); |
| 14 | + if (tokenObtained) { |
| 15 | + const user = await updateUser(); |
| 16 | + if (user !== null && user.id) { |
| 17 | + setUserIsAuthenticated(true); |
| 18 | + } |
| 19 | + } |
| 20 | + }; |
4 | 21 |
|
5 | | -export default function App() { |
6 | 22 | return ( |
7 | | - <View> |
8 | | - <View style={styles.container} /> |
| 23 | + <View style={styles.view}> |
| 24 | + <Text style={styles.caption}>You are not logged in.</Text> |
| 25 | + <TextInput |
| 26 | + style={styles.textInput} |
| 27 | + onChangeText={(text) => setEmail(text)} |
| 28 | + value={email} |
| 29 | + placeholder="Email" |
| 30 | + /> |
| 31 | + <TextInput |
| 32 | + style={styles.textInput} |
| 33 | + onChangeText={(text) => setPassword(text)} |
| 34 | + value={password} |
| 35 | + secureTextEntry |
| 36 | + placeholder="Password" |
| 37 | + /> |
| 38 | + <Pressable style={styles.button} onPress={handleLogin}> |
| 39 | + <Text style={styles.buttonText}>Login</Text> |
| 40 | + </Pressable> |
9 | 41 | </View> |
10 | 42 | ); |
11 | 43 | } |
12 | 44 |
|
| 45 | +function Logout() { |
| 46 | + const { logout, setUserIsAuthenticated, updateUser, currentUser } = useAuth(); |
| 47 | + |
| 48 | + const handleLogout = async () => { |
| 49 | + const isLoggedOut = await logout(); |
| 50 | + if (isLoggedOut) { |
| 51 | + setUserIsAuthenticated(false); |
| 52 | + await updateUser(); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <View style={styles.view}> |
| 58 | + {currentUser && ( |
| 59 | + <Text style={styles.caption}> |
| 60 | + You are logged in as {currentUser.name} |
| 61 | + </Text> |
| 62 | + )} |
| 63 | + <Pressable style={styles.button} onPress={handleLogout}> |
| 64 | + <Text style={styles.buttonText}>Logout</Text> |
| 65 | + </Pressable> |
| 66 | + </View> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +function Example() { |
| 71 | + const { isAuthenticated } = useAuth(); |
| 72 | + return ( |
| 73 | + <View style={styles.view}>{isAuthenticated ? <Logout /> : <Login />}</View> |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +export default function App() { |
| 78 | + const config = { |
| 79 | + loginUrl: 'http://127.0.0.1:8000/api/sanctum/token', |
| 80 | + logoutUrl: 'http://127.0.0.1:8000/api/logout', |
| 81 | + userUrl: 'http://127.0.0.1:8000/api/user', |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <AuthProvider config={config}> |
| 86 | + <View style={styles.container}> |
| 87 | + <Example /> |
| 88 | + <StatusBar style="auto" /> |
| 89 | + </View> |
| 90 | + </AuthProvider> |
| 91 | + ); |
| 92 | +} |
| 93 | + |
13 | 94 | const styles = StyleSheet.create({ |
14 | 95 | container: { |
15 | 96 | flex: 1, |
| 97 | + backgroundColor: '#fff', |
16 | 98 | alignItems: 'center', |
17 | 99 | justifyContent: 'center', |
18 | 100 | }, |
19 | | - box: { |
20 | | - width: 60, |
21 | | - height: 60, |
22 | | - marginVertical: 20, |
| 101 | + button: { |
| 102 | + backgroundColor: '#f59e0b', |
| 103 | + padding: 12, |
| 104 | + minWidth: '80%', |
| 105 | + borderRadius: 5, |
| 106 | + alignItems: 'center', |
| 107 | + }, |
| 108 | + buttonText: { |
| 109 | + fontWeight: '800', |
| 110 | + letterSpacing: 0.5, |
| 111 | + lineHeight: 20, |
| 112 | + color: '#fff', |
| 113 | + }, |
| 114 | + textInput: { |
| 115 | + height: 40, |
| 116 | + borderColor: 'gray', |
| 117 | + borderWidth: 1, |
| 118 | + minWidth: '80%', |
| 119 | + padding: 10, |
| 120 | + borderRadius: 5, |
| 121 | + marginBottom: 10, |
| 122 | + }, |
| 123 | + caption: { |
| 124 | + marginBottom: 10, |
| 125 | + fontWeight: '800', |
| 126 | + letterSpacing: 0.5, |
| 127 | + lineHeight: 20, |
| 128 | + color: '#000', |
| 129 | + textAlign: 'center', |
| 130 | + }, |
| 131 | + view: { |
| 132 | + padding: 15, |
| 133 | + alignItems: 'center', |
23 | 134 | }, |
24 | 135 | }); |
0 commit comments