dauth-context-react 0.2.109 → 0.2.111
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.
- package/dist/api/dauth.api.d.ts +6 -5
- package/dist/api/interfaces/dauth.api.responses.d.ts +38 -0
- package/dist/dauth-context-react.cjs.development.js +54 -24
- package/dist/dauth-context-react.cjs.development.js.map +1 -1
- package/dist/dauth-context-react.cjs.production.min.js +1 -1
- package/dist/dauth-context-react.cjs.production.min.js.map +1 -1
- package/dist/dauth-context-react.esm.js +54 -24
- package/dist/dauth-context-react.esm.js.map +1 -1
- package/dist/reducer/dauth.actions.d.ts +3 -3
- package/package.json +1 -1
- package/src/api/dauth.api.ts +12 -5
- package/src/api/interfaces/dauth.api.responses.ts +43 -0
- package/src/index.tsx +42 -18
- package/src/reducer/dauth.actions.ts +4 -4
|
@@ -8,10 +8,10 @@ declare type TSetDauthStateAction = {
|
|
|
8
8
|
export declare function setDauthStateAction({ dispatch, token, domainName, }: TSetDauthStateAction): Promise<void>;
|
|
9
9
|
declare type TSetAutoLoginAction = {
|
|
10
10
|
dispatch: React.Dispatch<any>;
|
|
11
|
-
|
|
11
|
+
token_ls: string;
|
|
12
12
|
domainName: string;
|
|
13
13
|
};
|
|
14
|
-
export declare function setAutoLoginAction({ dispatch,
|
|
14
|
+
export declare function setAutoLoginAction({ dispatch, token_ls, domainName, }: TSetAutoLoginAction): Promise<void>;
|
|
15
15
|
export declare function setLogoutAction({ dispatch, }: {
|
|
16
16
|
dispatch: React.Dispatch<any>;
|
|
17
17
|
}): void;
|
|
@@ -36,6 +36,6 @@ export declare function checkTokenAction({ dispatch, domainName, token, }: {
|
|
|
36
36
|
export declare function getAccessTokenAction({ dispatch, domainName, }: {
|
|
37
37
|
dispatch: React.Dispatch<any>;
|
|
38
38
|
domainName: string;
|
|
39
|
-
}): Promise<
|
|
39
|
+
}): Promise<string | undefined>;
|
|
40
40
|
export declare const resetUser: (dispatch: React.Dispatch<any>) => void;
|
|
41
41
|
export {};
|
package/package.json
CHANGED
package/src/api/dauth.api.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { routes } from './utils/routes';
|
|
2
2
|
import { getServerBasePath } from './utils/config';
|
|
3
3
|
import { IDauthUser } from '../interfaces';
|
|
4
|
+
import {
|
|
5
|
+
IgetUserAPIResponse,
|
|
6
|
+
IrefreshAccessTokenAPIResponse,
|
|
7
|
+
IsendEmailVerificationAPIResponse,
|
|
8
|
+
IupdateUserAPIResponse,
|
|
9
|
+
IverifyTokenAPIResponse,
|
|
10
|
+
} from './interfaces/dauth.api.responses';
|
|
4
11
|
|
|
5
12
|
export const getUserAPI = async (
|
|
6
13
|
domainName: string,
|
|
7
14
|
token: string
|
|
8
|
-
): Promise<
|
|
15
|
+
): Promise<IgetUserAPIResponse> => {
|
|
9
16
|
const params = {
|
|
10
17
|
method: 'GET',
|
|
11
18
|
headers: {
|
|
@@ -27,7 +34,7 @@ export const updateUserAPI = async (
|
|
|
27
34
|
domainName: string,
|
|
28
35
|
user: Partial<IDauthUser>,
|
|
29
36
|
token: string
|
|
30
|
-
): Promise<
|
|
37
|
+
): Promise<IupdateUserAPIResponse> => {
|
|
31
38
|
const params = {
|
|
32
39
|
method: 'PATCH',
|
|
33
40
|
headers: {
|
|
@@ -49,7 +56,7 @@ export const updateUserAPI = async (
|
|
|
49
56
|
export const sendEmailVerificationAPI = async (
|
|
50
57
|
domainName: string,
|
|
51
58
|
token: string
|
|
52
|
-
): Promise<
|
|
59
|
+
): Promise<IsendEmailVerificationAPIResponse> => {
|
|
53
60
|
const params = {
|
|
54
61
|
method: 'GET',
|
|
55
62
|
headers: {
|
|
@@ -70,7 +77,7 @@ export const sendEmailVerificationAPI = async (
|
|
|
70
77
|
export const refreshAccessTokenAPI = async (
|
|
71
78
|
domainName: string,
|
|
72
79
|
token: string
|
|
73
|
-
): Promise<
|
|
80
|
+
): Promise<IrefreshAccessTokenAPIResponse> => {
|
|
74
81
|
const params = {
|
|
75
82
|
method: 'GET',
|
|
76
83
|
headers: {
|
|
@@ -96,7 +103,7 @@ export const verifyTokenAPI = async ({
|
|
|
96
103
|
domainName: string;
|
|
97
104
|
ask: string;
|
|
98
105
|
token: string;
|
|
99
|
-
}): Promise<
|
|
106
|
+
}): Promise<IverifyTokenAPIResponse> => {
|
|
100
107
|
const params = {
|
|
101
108
|
method: 'POST',
|
|
102
109
|
headers: {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { IDauthDomainState, IDauthUser } from '../../interfaces';
|
|
2
|
+
|
|
3
|
+
export interface IgetUserAPIResponse {
|
|
4
|
+
response: Response;
|
|
5
|
+
data: {
|
|
6
|
+
status: string;
|
|
7
|
+
user: IDauthUser;
|
|
8
|
+
domain: IDauthDomainState;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface IupdateUserAPIResponse {
|
|
13
|
+
response: Response;
|
|
14
|
+
data: {
|
|
15
|
+
status: string;
|
|
16
|
+
user: IDauthUser;
|
|
17
|
+
message: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IsendEmailVerificationAPIResponse {
|
|
22
|
+
response: Response;
|
|
23
|
+
data: {
|
|
24
|
+
status: string;
|
|
25
|
+
message: string;
|
|
26
|
+
emailStatus: string;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface IrefreshAccessTokenAPIResponse {
|
|
31
|
+
response: Response;
|
|
32
|
+
data: {
|
|
33
|
+
accessToken: string;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface IverifyTokenAPIResponse {
|
|
38
|
+
response: Response;
|
|
39
|
+
data: {
|
|
40
|
+
status: string;
|
|
41
|
+
message: string;
|
|
42
|
+
};
|
|
43
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -26,8 +26,8 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
26
26
|
) => {
|
|
27
27
|
const { domainName, ask, children } = props;
|
|
28
28
|
const [dauthState, dispatch] = useReducer(userReducer, initialDauthState);
|
|
29
|
-
|
|
30
|
-
const
|
|
29
|
+
|
|
30
|
+
const isValidAsk = useCallback(
|
|
31
31
|
async (token: string) => {
|
|
32
32
|
const verifyToken = await verifyTokenAPI({
|
|
33
33
|
domainName,
|
|
@@ -35,23 +35,33 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
35
35
|
ask,
|
|
36
36
|
});
|
|
37
37
|
if (verifyToken.response.status !== 200) {
|
|
38
|
-
return
|
|
38
|
+
return false;
|
|
39
39
|
}
|
|
40
|
+
return true;
|
|
40
41
|
},
|
|
41
|
-
[domainName, ask]
|
|
42
|
-
)
|
|
43
|
-
|
|
42
|
+
[domainName, ask]
|
|
43
|
+
);
|
|
44
|
+
|
|
44
45
|
// Check token periodically
|
|
45
46
|
useEffect(() => {
|
|
46
47
|
if (!dauthState.isAuthenticated) return;
|
|
47
48
|
let interval = setInterval(async () => {
|
|
48
49
|
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
49
50
|
if (!token_ls) return;
|
|
50
|
-
await
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
const isValid = await isValidAsk(token_ls);
|
|
52
|
+
if (isValid) {
|
|
53
|
+
return action.checkTokenAction({
|
|
54
|
+
dispatch,
|
|
55
|
+
domainName,
|
|
56
|
+
token: token_ls,
|
|
57
|
+
});
|
|
58
|
+
} else {
|
|
59
|
+
action.setLogoutAction({ dispatch });
|
|
60
|
+
throw new Error('Ask value in DauthProvider is not valid');
|
|
61
|
+
}
|
|
62
|
+
}, 1000 * 60 * 5);
|
|
53
63
|
return () => clearInterval(interval);
|
|
54
|
-
}, []);
|
|
64
|
+
}, [dauthState.isAuthenticated, isValidAsk]);
|
|
55
65
|
|
|
56
66
|
// Catch login redirect
|
|
57
67
|
useEffect(() => {
|
|
@@ -61,8 +71,17 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
61
71
|
const urlParams = new URLSearchParams(queryString);
|
|
62
72
|
const token_url = urlParams.get(TOKEN_LS);
|
|
63
73
|
if (token_url && !dauthState.isAuthenticated) {
|
|
64
|
-
await
|
|
65
|
-
|
|
74
|
+
const isValid = await isValidAsk(token_url);
|
|
75
|
+
if (isValid) {
|
|
76
|
+
return action.setDauthStateAction({
|
|
77
|
+
dispatch,
|
|
78
|
+
token: token_url,
|
|
79
|
+
domainName,
|
|
80
|
+
});
|
|
81
|
+
} else {
|
|
82
|
+
action.setLogoutAction({ dispatch });
|
|
83
|
+
throw new Error('Ask value in DauthProvider is not valid');
|
|
84
|
+
}
|
|
66
85
|
}
|
|
67
86
|
})();
|
|
68
87
|
}, []);
|
|
@@ -72,12 +91,17 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
72
91
|
(async () => {
|
|
73
92
|
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
74
93
|
if (token_ls && !dauthState.isAuthenticated) {
|
|
75
|
-
await
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
94
|
+
const isValid = await isValidAsk(token_ls);
|
|
95
|
+
if (isValid) {
|
|
96
|
+
return action.setAutoLoginAction({
|
|
97
|
+
dispatch,
|
|
98
|
+
token_ls,
|
|
99
|
+
domainName,
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
action.setLogoutAction({ dispatch });
|
|
103
|
+
throw new Error('Ask value in DauthProvider is not valid');
|
|
104
|
+
}
|
|
81
105
|
}
|
|
82
106
|
})();
|
|
83
107
|
}, []);
|
|
@@ -54,22 +54,22 @@ export async function setDauthStateAction({
|
|
|
54
54
|
|
|
55
55
|
type TSetAutoLoginAction = {
|
|
56
56
|
dispatch: React.Dispatch<any>;
|
|
57
|
-
|
|
57
|
+
token_ls: string;
|
|
58
58
|
domainName: string;
|
|
59
59
|
};
|
|
60
60
|
export async function setAutoLoginAction({
|
|
61
61
|
dispatch,
|
|
62
|
-
|
|
62
|
+
token_ls,
|
|
63
63
|
domainName,
|
|
64
64
|
}: TSetAutoLoginAction) {
|
|
65
65
|
dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
|
|
66
66
|
try {
|
|
67
67
|
const refreshAccessTokenFetch = await refreshAccessTokenAPI(
|
|
68
68
|
domainName,
|
|
69
|
-
|
|
69
|
+
token_ls
|
|
70
70
|
);
|
|
71
71
|
if (refreshAccessTokenFetch.response.status === 200) {
|
|
72
|
-
const getUserFetch = await getUserAPI(domainName,
|
|
72
|
+
const getUserFetch = await getUserAPI(domainName, token_ls);
|
|
73
73
|
if (getUserFetch.response.status === 200) {
|
|
74
74
|
dispatch({
|
|
75
75
|
type: DauthTypes.LOGIN,
|