@boneframework/native-components 1.0.13 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,21 +4,18 @@ import AuthContext from "../contexts/auth";
|
|
|
4
4
|
import useAuth from "../hooks/useAuth";
|
|
5
5
|
|
|
6
6
|
function SessionProvider(props: object) {
|
|
7
|
-
const
|
|
8
|
-
const auth = useAuth();
|
|
9
|
-
setUser(auth.user);
|
|
7
|
+
const {login, logout, updateUser, user, isLoading} = useAuth();
|
|
10
8
|
|
|
11
9
|
return (
|
|
12
10
|
<AuthContext.Provider
|
|
13
11
|
value={{
|
|
14
12
|
signIn: async (authToken: string) => {
|
|
15
|
-
|
|
16
|
-
setUser(auth.user);
|
|
13
|
+
login(authToken);
|
|
17
14
|
},
|
|
18
15
|
signOut: () => {
|
|
19
|
-
|
|
20
|
-
setUser(null);
|
|
16
|
+
logout();
|
|
21
17
|
},
|
|
18
|
+
update: data => updateUser(data),
|
|
22
19
|
user,
|
|
23
20
|
}}>
|
|
24
21
|
{props.children}
|
|
@@ -26,6 +23,4 @@ function SessionProvider(props: object) {
|
|
|
26
23
|
)
|
|
27
24
|
}
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
|
|
31
26
|
export default SessionProvider;
|
package/contexts/auth.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
|
|
3
|
+
const AuthContext = createContext<{
|
|
4
|
+
login: () => void;
|
|
5
|
+
logout: () => void;
|
|
6
|
+
updateUser: () => void;
|
|
7
|
+
user?: object | null;
|
|
8
|
+
isLoading: boolean;
|
|
9
|
+
}>({
|
|
10
|
+
login: () => null,
|
|
11
|
+
logout: () => null,
|
|
12
|
+
updateUser: () => null,
|
|
13
|
+
user: null,
|
|
14
|
+
isLoading: false,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export default AuthContext;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useContext} from "react";
|
|
1
|
+
import {useContext, useState} from "react";
|
|
2
2
|
|
|
3
3
|
import AuthContext from "../contexts/auth";
|
|
4
4
|
import authStorage from "../utilities/authStorage";
|
|
@@ -8,23 +8,27 @@ import usersApi from "../api/users";
|
|
|
8
8
|
export default useAuth = () => {
|
|
9
9
|
const profileApi = useApi(usersApi.getProfile);
|
|
10
10
|
const {user, setUser} = useContext(AuthContext);
|
|
11
|
+
const {isLoading, setIsLoading} = useState(false);
|
|
11
12
|
|
|
12
13
|
const login = async authToken => {
|
|
13
|
-
|
|
14
|
+
setIsLoading(true);
|
|
14
15
|
await authStorage.storeAuthToken(authToken).then(async () => {
|
|
15
16
|
const user = await profileApi.request(authToken);
|
|
16
17
|
authStorage.storeUser(user.data);
|
|
17
18
|
user.data.authToken = authToken;
|
|
18
19
|
setUser(user.data);
|
|
20
|
+
setIsLoading(false);
|
|
19
21
|
});
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
const updateUser = async user => {
|
|
25
|
+
setIsLoading(true);
|
|
23
26
|
const authToken = user.authToken;
|
|
24
27
|
await delete user.authToken;
|
|
25
28
|
authStorage.storeUser(user);
|
|
26
29
|
user.authToken = authToken;
|
|
27
30
|
setUser({...user});
|
|
31
|
+
setIsLoading(false);
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
const logout = () => {
|
|
@@ -33,5 +37,5 @@ export default useAuth = () => {
|
|
|
33
37
|
authStorage.removeUser();
|
|
34
38
|
}
|
|
35
39
|
|
|
36
|
-
return {login, logout, updateUser, user};
|
|
40
|
+
return {login, logout, updateUser, user, isLoading};
|
|
37
41
|
}
|
package/package.json
CHANGED