Skip to content

Commit b8c06b0

Browse files
committed
docs: update example
1 parent b04e82b commit b8c06b0

File tree

3 files changed

+125
-8
lines changed

3 files changed

+125
-8
lines changed

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"expo": "~49.0.7",
13+
"expo-secure-store": "~12.3.1",
1314
"expo-status-bar": "~1.6.0",
1415
"react": "18.2.0",
1516
"react-dom": "18.2.0",

example/src/App.tsx

Lines changed: 119 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,135 @@
11
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';
26

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+
};
421

5-
export default function App() {
622
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>
941
</View>
1042
);
1143
}
1244

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+
1394
const styles = StyleSheet.create({
1495
container: {
1596
flex: 1,
97+
backgroundColor: '#fff',
1698
alignItems: 'center',
1799
justifyContent: 'center',
18100
},
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',
23134
},
24135
});

example/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,6 +3995,11 @@ expo-pwa@0.0.127:
39953995
commander "2.20.0"
39963996
update-check "1.5.3"
39973997

3998+
expo-secure-store@~12.3.1:
3999+
version "12.3.1"
4000+
resolved "https://registry.yarnpkg.com/expo-secure-store/-/expo-secure-store-12.3.1.tgz#7e5ba94f6e7374f132108c9feb5cc811568f3db6"
4001+
integrity sha512-XLIgWDiIuiR0c+AA4NCWWibAMHCZUyRcy+lQBU49U6rvG+xmd3YrBJfQjfqAPyLroEqnLPGTWUX57GyRsfDOQw==
4002+
39984003
expo-status-bar@~1.6.0:
39994004
version "1.6.0"
40004005
resolved "https://registry.yarnpkg.com/expo-status-bar/-/expo-status-bar-1.6.0.tgz#e79ffdb9a84d2e0ec9a0dc7392d9ab364fefa9cf"

0 commit comments

Comments
 (0)