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