dauth-context-react 0.2.104 → 0.2.106
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/dauth-context-react.cjs.development.js +58 -23
- 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 +58 -23
- package/dist/dauth-context-react.esm.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/initialDauthState.d.ts +5 -7
- package/dist/reducer/dauth.actions.d.ts +2 -4
- package/package.json +6 -2
- package/src/index.tsx +56 -17
- package/src/initialDauthState.ts +6 -8
- package/src/reducer/dauth.actions.ts +4 -8
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
export interface IDauthUser {
|
|
2
2
|
_id: string;
|
|
3
|
-
dauthLicense?: string;
|
|
4
|
-
sid: string;
|
|
5
3
|
name: string;
|
|
6
4
|
lastname: string;
|
|
7
5
|
nickname: string;
|
|
8
6
|
email: string;
|
|
9
|
-
|
|
7
|
+
isVerified: boolean;
|
|
10
8
|
language: string;
|
|
11
9
|
avatar: {
|
|
12
10
|
id: string;
|
|
13
11
|
url: string;
|
|
14
12
|
};
|
|
15
13
|
role: string;
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
telPrefix: string;
|
|
15
|
+
telSuffix: string;
|
|
18
16
|
createdAt: Date;
|
|
19
17
|
updatedAt: Date;
|
|
20
|
-
|
|
18
|
+
lastLogin: Date;
|
|
21
19
|
}
|
|
22
20
|
export interface IDauthDomainState {
|
|
23
21
|
name: string;
|
|
@@ -32,7 +30,7 @@ export interface IDauthState {
|
|
|
32
30
|
loginWithRedirect: () => void;
|
|
33
31
|
logout: () => void;
|
|
34
32
|
getAccessToken: () => Promise<string>;
|
|
35
|
-
updateUser: ({ name, lastname, nickname,
|
|
33
|
+
updateUser: ({ name, lastname, nickname, telPrefix, telSuffix, language, avatar, }: Partial<IDauthUser>) => Promise<boolean>;
|
|
36
34
|
updateUserWithRedirect: () => void;
|
|
37
35
|
sendEmailVerificationStatus: {
|
|
38
36
|
status: IActionStatus;
|
|
@@ -10,9 +10,8 @@ declare type TSetAutoLoginAction = {
|
|
|
10
10
|
dispatch: React.Dispatch<any>;
|
|
11
11
|
dauth_state_ls: string;
|
|
12
12
|
domainName: string;
|
|
13
|
-
sid: string;
|
|
14
13
|
};
|
|
15
|
-
export declare function setAutoLoginAction({ dispatch, dauth_state_ls, domainName,
|
|
14
|
+
export declare function setAutoLoginAction({ dispatch, dauth_state_ls, domainName, }: TSetAutoLoginAction): Promise<void>;
|
|
16
15
|
export declare function setLogoutAction({ dispatch, }: {
|
|
17
16
|
dispatch: React.Dispatch<any>;
|
|
18
17
|
}): void;
|
|
@@ -29,10 +28,9 @@ declare type TSetSendEmailVerificationAction = {
|
|
|
29
28
|
token: string;
|
|
30
29
|
};
|
|
31
30
|
export declare function sendEmailVerificationAction({ dispatch, domainName, token, }: TSetSendEmailVerificationAction): Promise<boolean>;
|
|
32
|
-
export declare function checkTokenAction({ dispatch, domainName,
|
|
31
|
+
export declare function checkTokenAction({ dispatch, domainName, token, }: {
|
|
33
32
|
dispatch: React.Dispatch<any>;
|
|
34
33
|
domainName: string;
|
|
35
|
-
sid: string;
|
|
36
34
|
token: string;
|
|
37
35
|
}): Promise<void>;
|
|
38
36
|
export declare function getAccessTokenAction({ dispatch, domainName, }: {
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.2.
|
|
2
|
+
"version": "0.2.106",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"typings": "dist/index.d.ts",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
],
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@size-limit/preset-small-lib": "^11.0.2",
|
|
51
|
+
"@types/jsonwebtoken": "^9.0.7",
|
|
51
52
|
"@types/react": "^18.2.55",
|
|
52
53
|
"@types/react-dom": "^18.2.19",
|
|
53
54
|
"husky": "^9.0.10",
|
|
@@ -62,5 +63,8 @@
|
|
|
62
63
|
"react",
|
|
63
64
|
"context",
|
|
64
65
|
"authentication"
|
|
65
|
-
]
|
|
66
|
+
],
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"jsonwebtoken": "^9.0.2"
|
|
69
|
+
}
|
|
66
70
|
}
|
package/src/index.tsx
CHANGED
|
@@ -12,30 +12,65 @@ import * as action from './reducer/dauth.actions';
|
|
|
12
12
|
import { getClientBasePath } from './api/utils/config';
|
|
13
13
|
import { TOKEN_LS } from './constants';
|
|
14
14
|
import { routes } from './routes';
|
|
15
|
+
import jwt from 'jsonwebtoken';
|
|
15
16
|
|
|
16
17
|
interface DauthProviderProps {
|
|
17
18
|
domainName: string;
|
|
18
|
-
|
|
19
|
+
ask: string;
|
|
19
20
|
children: React.ReactNode;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
23
24
|
props: DauthProviderProps
|
|
24
25
|
) => {
|
|
25
|
-
const { domainName,
|
|
26
|
+
const { domainName, ask, children } = props;
|
|
26
27
|
const [dauthState, dispatch] = useReducer(userReducer, initialDauthState);
|
|
27
28
|
|
|
29
|
+
// useEffect(() => {
|
|
30
|
+
// const verifyToken = async () => {
|
|
31
|
+
// const token = localStorage.getItem(TOKEN_LS);
|
|
32
|
+
// if (!token) return;
|
|
33
|
+
|
|
34
|
+
// try {
|
|
35
|
+
// const response = await fetch('/api/verifyToken', {
|
|
36
|
+
// method: 'POST',
|
|
37
|
+
// headers: {
|
|
38
|
+
// 'Content-Type': 'application/json',
|
|
39
|
+
// Authorization: `Bearer ${token}`,
|
|
40
|
+
// },
|
|
41
|
+
// body: JSON.stringify({ token }),
|
|
42
|
+
// });
|
|
43
|
+
|
|
44
|
+
// if (response.ok) {
|
|
45
|
+
// const data = await response.json();
|
|
46
|
+
// if (data.valid) {
|
|
47
|
+
// dispatch({ type: 'SET_AUTHENTICATED', payload: true });
|
|
48
|
+
// } else {
|
|
49
|
+
// dispatch({ type: 'SET_AUTHENTICATED', payload: false });
|
|
50
|
+
// }
|
|
51
|
+
// } else {
|
|
52
|
+
// dispatch({ type: 'SET_AUTHENTICATED', payload: false });
|
|
53
|
+
// }
|
|
54
|
+
// } catch (error) {
|
|
55
|
+
// console.error('Error verifying token:', error);
|
|
56
|
+
// dispatch({ type: 'SET_AUTHENTICATED', payload: false });
|
|
57
|
+
// }
|
|
58
|
+
// };
|
|
59
|
+
|
|
60
|
+
// verifyToken();
|
|
61
|
+
// }, [dispatch]);
|
|
62
|
+
|
|
28
63
|
// Check token periodically
|
|
29
64
|
useEffect(() => {
|
|
30
65
|
if (!dauthState.isAuthenticated) return;
|
|
31
66
|
let interval = setInterval(() => {
|
|
32
67
|
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
33
68
|
if (!token_ls) return;
|
|
34
|
-
action.checkTokenAction({ dispatch, domainName,
|
|
35
|
-
}, 1000 * 60 *
|
|
69
|
+
action.checkTokenAction({ dispatch, domainName, token: token_ls });
|
|
70
|
+
}, 1000 * 60 * 1);
|
|
36
71
|
return () => clearInterval(interval);
|
|
37
72
|
}, []);
|
|
38
|
-
|
|
73
|
+
|
|
39
74
|
// Catch login redirect
|
|
40
75
|
useEffect(() => {
|
|
41
76
|
const queryString = window.location.search;
|
|
@@ -46,20 +81,24 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
46
81
|
action.setDauthStateAction({ dispatch, token: token_url, domainName });
|
|
47
82
|
}
|
|
48
83
|
}, []);
|
|
49
|
-
|
|
84
|
+
|
|
50
85
|
// Auto Login
|
|
51
86
|
useEffect(() => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
87
|
+
if (dauthState.isAuthenticated) return;
|
|
88
|
+
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
89
|
+
if (!token_ls) return;
|
|
90
|
+
const verify = jwt.verify(token_ls, ask as string);
|
|
91
|
+
console.log({ verify });
|
|
92
|
+
if (token_ls && !dauthState.isAuthenticated) {
|
|
93
|
+
action.setAutoLoginAction({ dispatch, dauth_state_ls: token_ls, domainName });
|
|
55
94
|
}
|
|
56
95
|
}, []);
|
|
57
96
|
|
|
58
97
|
const loginWithRedirect = useCallback(() => {
|
|
59
98
|
return window.location.replace(
|
|
60
|
-
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${
|
|
99
|
+
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${domainName}`
|
|
61
100
|
);
|
|
62
|
-
}, [domainName,
|
|
101
|
+
}, [domainName, domainName]);
|
|
63
102
|
|
|
64
103
|
const logout = useCallback(() => {
|
|
65
104
|
return action.setLogoutAction({ dispatch });
|
|
@@ -75,8 +114,8 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
75
114
|
name,
|
|
76
115
|
lastname,
|
|
77
116
|
nickname,
|
|
78
|
-
|
|
79
|
-
|
|
117
|
+
telPrefix,
|
|
118
|
+
telSuffix,
|
|
80
119
|
language,
|
|
81
120
|
avatar,
|
|
82
121
|
}: Partial<IDauthUser>) => {
|
|
@@ -85,8 +124,8 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
85
124
|
name,
|
|
86
125
|
lastname,
|
|
87
126
|
nickname,
|
|
88
|
-
|
|
89
|
-
|
|
127
|
+
telPrefix,
|
|
128
|
+
telSuffix,
|
|
90
129
|
language,
|
|
91
130
|
avatar,
|
|
92
131
|
} as Partial<IDauthUser>;
|
|
@@ -106,9 +145,9 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
106
145
|
return window.location.replace(
|
|
107
146
|
`${getClientBasePath({ domainName })}/${
|
|
108
147
|
routes.tenantUpdateUser
|
|
109
|
-
}/${
|
|
148
|
+
}/${domainName}/${token_ls}`
|
|
110
149
|
);
|
|
111
|
-
}, [domainName,
|
|
150
|
+
}, [domainName, domainName]);
|
|
112
151
|
|
|
113
152
|
const sendEmailVerification = useCallback(async () => {
|
|
114
153
|
const token_ls = localStorage.getItem(TOKEN_LS);
|
package/src/initialDauthState.ts
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
export interface IDauthUser {
|
|
2
2
|
_id: string;
|
|
3
|
-
dauthLicense?: string;
|
|
4
|
-
sid: string;
|
|
5
3
|
name: string;
|
|
6
4
|
lastname: string;
|
|
7
5
|
nickname: string;
|
|
8
6
|
email: string;
|
|
9
|
-
|
|
7
|
+
isVerified: boolean;
|
|
10
8
|
language: string;
|
|
11
9
|
avatar: {
|
|
12
10
|
id: string;
|
|
13
11
|
url: string;
|
|
14
12
|
};
|
|
15
13
|
role: string;
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
telPrefix: string;
|
|
15
|
+
telSuffix: string;
|
|
18
16
|
createdAt: Date;
|
|
19
17
|
updatedAt: Date;
|
|
20
|
-
|
|
18
|
+
lastLogin: Date;
|
|
21
19
|
}
|
|
22
20
|
|
|
23
21
|
export interface IDauthDomainState {
|
|
@@ -38,8 +36,8 @@ export interface IDauthState {
|
|
|
38
36
|
name,
|
|
39
37
|
lastname,
|
|
40
38
|
nickname,
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
telPrefix,
|
|
40
|
+
telSuffix,
|
|
43
41
|
language,
|
|
44
42
|
avatar,
|
|
45
43
|
}: Partial<IDauthUser>) => Promise<boolean>;
|
|
@@ -56,20 +56,18 @@ type TSetAutoLoginAction = {
|
|
|
56
56
|
dispatch: React.Dispatch<any>;
|
|
57
57
|
dauth_state_ls: string;
|
|
58
58
|
domainName: string;
|
|
59
|
-
sid: string;
|
|
60
59
|
};
|
|
61
60
|
export async function setAutoLoginAction({
|
|
62
61
|
dispatch,
|
|
63
62
|
dauth_state_ls,
|
|
64
63
|
domainName,
|
|
65
|
-
sid,
|
|
66
64
|
}: TSetAutoLoginAction) {
|
|
67
65
|
dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
|
|
68
66
|
try {
|
|
69
67
|
const refreshAccessTokenFetch = await refreshAccessTokenAPI(
|
|
70
68
|
domainName,
|
|
71
69
|
dauth_state_ls
|
|
72
|
-
);
|
|
70
|
+
);
|
|
73
71
|
if (refreshAccessTokenFetch.response.status === 200) {
|
|
74
72
|
const getUserFetch = await getUserAPI(domainName, dauth_state_ls);
|
|
75
73
|
if (getUserFetch.response.status === 200) {
|
|
@@ -88,13 +86,13 @@ export async function setAutoLoginAction({
|
|
|
88
86
|
return;
|
|
89
87
|
} else {
|
|
90
88
|
window.location.replace(
|
|
91
|
-
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${
|
|
89
|
+
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${domainName}`
|
|
92
90
|
);
|
|
93
91
|
return resetUser(dispatch);
|
|
94
92
|
}
|
|
95
93
|
} else {
|
|
96
94
|
window.location.replace(
|
|
97
|
-
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${
|
|
95
|
+
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${domainName}`
|
|
98
96
|
);
|
|
99
97
|
return resetUser(dispatch);
|
|
100
98
|
}
|
|
@@ -232,12 +230,10 @@ export async function sendEmailVerificationAction({
|
|
|
232
230
|
export async function checkTokenAction({
|
|
233
231
|
dispatch,
|
|
234
232
|
domainName,
|
|
235
|
-
sid,
|
|
236
233
|
token,
|
|
237
234
|
}: {
|
|
238
235
|
dispatch: React.Dispatch<any>;
|
|
239
236
|
domainName: string;
|
|
240
|
-
sid: string;
|
|
241
237
|
token: string;
|
|
242
238
|
}) {
|
|
243
239
|
try {
|
|
@@ -249,7 +245,7 @@ export async function checkTokenAction({
|
|
|
249
245
|
return;
|
|
250
246
|
} else {
|
|
251
247
|
window.location.replace(
|
|
252
|
-
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${
|
|
248
|
+
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${domainName}`
|
|
253
249
|
);
|
|
254
250
|
return resetUser(dispatch);
|
|
255
251
|
}
|