dauth-context-react 0.2.110 → 0.2.112
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 +8 -7
- package/dist/api/interfaces/dauth.api.responses.d.ts +38 -0
- package/dist/dauth-context-react.cjs.development.js +31 -28
- 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 +31 -28
- package/dist/dauth-context-react.esm.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/reducer/dauth.actions.d.ts +3 -3
- package/package.json +1 -1
- package/src/api/dauth.api.ts +15 -8
- package/src/api/interfaces/dauth.api.responses.ts +43 -0
- package/src/index.tsx +17 -14
- package/src/reducer/dauth.actions.ts +14 -8
package/dist/index.d.ts
CHANGED
|
@@ -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: {
|
|
@@ -90,20 +97,20 @@ export const refreshAccessTokenAPI = async (
|
|
|
90
97
|
|
|
91
98
|
export const verifyTokenAPI = async ({
|
|
92
99
|
domainName,
|
|
93
|
-
|
|
100
|
+
tsk,
|
|
94
101
|
token,
|
|
95
102
|
}: {
|
|
96
103
|
domainName: string;
|
|
97
|
-
|
|
104
|
+
tsk: string;
|
|
98
105
|
token: string;
|
|
99
|
-
}): Promise<
|
|
106
|
+
}): Promise<IverifyTokenAPIResponse> => {
|
|
100
107
|
const params = {
|
|
101
108
|
method: 'POST',
|
|
102
109
|
headers: {
|
|
103
110
|
Authorization: token,
|
|
104
111
|
'Content-Type': 'application/json',
|
|
105
112
|
},
|
|
106
|
-
body: JSON.stringify({
|
|
113
|
+
body: JSON.stringify({ tsk }),
|
|
107
114
|
};
|
|
108
115
|
const response = await fetch(
|
|
109
116
|
`${getServerBasePath({ domainName })}/${
|
|
@@ -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
|
@@ -17,29 +17,29 @@ import { IDauthUser } from './interfaces';
|
|
|
17
17
|
|
|
18
18
|
interface DauthProviderProps {
|
|
19
19
|
domainName: string;
|
|
20
|
-
|
|
20
|
+
tsk: string;
|
|
21
21
|
children: React.ReactNode;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
25
25
|
props: DauthProviderProps
|
|
26
26
|
) => {
|
|
27
|
-
const { domainName,
|
|
27
|
+
const { domainName, tsk, children } = props;
|
|
28
28
|
const [dauthState, dispatch] = useReducer(userReducer, initialDauthState);
|
|
29
29
|
|
|
30
|
-
const
|
|
30
|
+
const isValidTsk = useCallback(
|
|
31
31
|
async (token: string) => {
|
|
32
32
|
const verifyToken = await verifyTokenAPI({
|
|
33
33
|
domainName,
|
|
34
34
|
token,
|
|
35
|
-
|
|
35
|
+
tsk,
|
|
36
36
|
});
|
|
37
37
|
if (verifyToken.response.status !== 200) {
|
|
38
38
|
return false;
|
|
39
39
|
}
|
|
40
40
|
return true;
|
|
41
41
|
},
|
|
42
|
-
[domainName,
|
|
42
|
+
[domainName, tsk]
|
|
43
43
|
);
|
|
44
44
|
|
|
45
45
|
// Check token periodically
|
|
@@ -48,7 +48,7 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
48
48
|
let interval = setInterval(async () => {
|
|
49
49
|
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
50
50
|
if (!token_ls) return;
|
|
51
|
-
const isValid = await
|
|
51
|
+
const isValid = await isValidTsk(token_ls);
|
|
52
52
|
if (isValid) {
|
|
53
53
|
return action.checkTokenAction({
|
|
54
54
|
dispatch,
|
|
@@ -56,11 +56,12 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
56
56
|
token: token_ls,
|
|
57
57
|
});
|
|
58
58
|
} else {
|
|
59
|
-
|
|
59
|
+
action.setLogoutAction({ dispatch });
|
|
60
|
+
throw new Error('Ask value in DauthProvider is not valid');
|
|
60
61
|
}
|
|
61
|
-
}, 1000 * 60 *
|
|
62
|
+
}, 1000 * 60 * 5);
|
|
62
63
|
return () => clearInterval(interval);
|
|
63
|
-
}, [dauthState.isAuthenticated,
|
|
64
|
+
}, [dauthState.isAuthenticated, isValidTsk]);
|
|
64
65
|
|
|
65
66
|
// Catch login redirect
|
|
66
67
|
useEffect(() => {
|
|
@@ -70,7 +71,7 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
70
71
|
const urlParams = new URLSearchParams(queryString);
|
|
71
72
|
const token_url = urlParams.get(TOKEN_LS);
|
|
72
73
|
if (token_url && !dauthState.isAuthenticated) {
|
|
73
|
-
const isValid = await
|
|
74
|
+
const isValid = await isValidTsk(token_url);
|
|
74
75
|
if (isValid) {
|
|
75
76
|
return action.setDauthStateAction({
|
|
76
77
|
dispatch,
|
|
@@ -78,7 +79,8 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
78
79
|
domainName,
|
|
79
80
|
});
|
|
80
81
|
} else {
|
|
81
|
-
|
|
82
|
+
action.setLogoutAction({ dispatch });
|
|
83
|
+
throw new Error('Ask value in DauthProvider is not valid');
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
})();
|
|
@@ -89,15 +91,16 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
89
91
|
(async () => {
|
|
90
92
|
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
91
93
|
if (token_ls && !dauthState.isAuthenticated) {
|
|
92
|
-
const isValid = await
|
|
94
|
+
const isValid = await isValidTsk(token_ls);
|
|
93
95
|
if (isValid) {
|
|
94
96
|
return action.setAutoLoginAction({
|
|
95
97
|
dispatch,
|
|
96
|
-
|
|
98
|
+
token_ls,
|
|
97
99
|
domainName,
|
|
98
100
|
});
|
|
99
101
|
} else {
|
|
100
|
-
|
|
102
|
+
action.setLogoutAction({ dispatch });
|
|
103
|
+
throw new Error('Ask value in DauthProvider is not valid');
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
})();
|
|
@@ -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
|
-
|
|
70
|
-
);
|
|
69
|
+
token_ls
|
|
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,
|
|
@@ -86,13 +86,17 @@ export async function setAutoLoginAction({
|
|
|
86
86
|
return;
|
|
87
87
|
} else {
|
|
88
88
|
window.location.replace(
|
|
89
|
-
`${getClientBasePath({ domainName })}/${
|
|
89
|
+
`${getClientBasePath({ domainName })}/${
|
|
90
|
+
routes.tenantSignin
|
|
91
|
+
}/${domainName}`
|
|
90
92
|
);
|
|
91
93
|
return resetUser(dispatch);
|
|
92
94
|
}
|
|
93
95
|
} else {
|
|
94
96
|
window.location.replace(
|
|
95
|
-
`${getClientBasePath({ domainName })}/${
|
|
97
|
+
`${getClientBasePath({ domainName })}/${
|
|
98
|
+
routes.tenantSignin
|
|
99
|
+
}/${domainName}`
|
|
96
100
|
);
|
|
97
101
|
return resetUser(dispatch);
|
|
98
102
|
}
|
|
@@ -245,7 +249,9 @@ export async function checkTokenAction({
|
|
|
245
249
|
return;
|
|
246
250
|
} else {
|
|
247
251
|
window.location.replace(
|
|
248
|
-
`${getClientBasePath({ domainName })}/${
|
|
252
|
+
`${getClientBasePath({ domainName })}/${
|
|
253
|
+
routes.tenantSignin
|
|
254
|
+
}/${domainName}`
|
|
249
255
|
);
|
|
250
256
|
return resetUser(dispatch);
|
|
251
257
|
}
|