dauth-context-react 0.1.999 → 0.2.1
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/README.md +3 -0
- package/dist/dauth-context-react.cjs.development.js +59 -37
- 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 +59 -37
- package/dist/dauth-context-react.esm.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/initialDauthState.d.ts +1 -1
- package/dist/reducer/dauth.actions.d.ts +16 -15
- package/dist/reducer/dauth.reducer.d.ts +2 -1
- package/dist/routes.d.ts +7 -0
- package/package.json +1 -1
- package/src/api/dauth.api.ts +9 -4
- package/src/index.tsx +9 -9
- package/src/initialDauthState.ts +2 -2
- package/src/reducer/dauth.actions.ts +16 -11
- package/src/reducer/dauth.reducer.ts +26 -15
- package/src/routes.ts +7 -0
|
@@ -1,42 +1,43 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
1
2
|
import { IDauthUser } from '../initialDauthState';
|
|
2
3
|
declare type TSetDauthStateAction = {
|
|
3
|
-
dispatch: any
|
|
4
|
+
dispatch: React.Dispatch<any>;
|
|
4
5
|
dauth_state: string;
|
|
5
6
|
domainName: string;
|
|
6
7
|
};
|
|
7
|
-
export declare function setDauthStateAction({ dispatch, dauth_state, domainName, }: TSetDauthStateAction): Promise<
|
|
8
|
+
export declare function setDauthStateAction({ dispatch, dauth_state, domainName, }: TSetDauthStateAction): Promise<void>;
|
|
8
9
|
declare type TSetAutoLoginAction = {
|
|
9
|
-
dispatch: any
|
|
10
|
+
dispatch: React.Dispatch<any>;
|
|
10
11
|
dauth_state_ls: string;
|
|
11
12
|
domainName: string;
|
|
12
13
|
sid: string;
|
|
13
14
|
};
|
|
14
|
-
export declare function setAutoLoginAction({ dispatch, dauth_state_ls, domainName, sid, }: TSetAutoLoginAction): Promise<
|
|
15
|
-
export declare function setLogoutAction({ dispatch }: {
|
|
16
|
-
dispatch: any
|
|
17
|
-
}): Promise<
|
|
15
|
+
export declare function setAutoLoginAction({ dispatch, dauth_state_ls, domainName, sid, }: TSetAutoLoginAction): Promise<void>;
|
|
16
|
+
export declare function setLogoutAction({ dispatch, }: {
|
|
17
|
+
dispatch: React.Dispatch<any>;
|
|
18
|
+
}): Promise<void>;
|
|
18
19
|
declare type TSetUpdateAction = {
|
|
19
|
-
dispatch: any
|
|
20
|
+
dispatch: React.Dispatch<any>;
|
|
20
21
|
domainName: string;
|
|
21
22
|
user: Partial<IDauthUser>;
|
|
22
23
|
token: string | null;
|
|
23
24
|
};
|
|
24
|
-
export declare function setUpdateUserAction({ dispatch, domainName, user, token, }: TSetUpdateAction): Promise<
|
|
25
|
+
export declare function setUpdateUserAction({ dispatch, domainName, user, token, }: TSetUpdateAction): Promise<void>;
|
|
25
26
|
declare type TSetSendEmailVerificationAction = {
|
|
26
|
-
dispatch: any
|
|
27
|
+
dispatch: React.Dispatch<any>;
|
|
27
28
|
domainName: string;
|
|
28
29
|
token: string;
|
|
29
30
|
};
|
|
30
|
-
export declare function sendEmailVerificationAction({ dispatch, domainName, token, }: TSetSendEmailVerificationAction): Promise<
|
|
31
|
+
export declare function sendEmailVerificationAction({ dispatch, domainName, token, }: TSetSendEmailVerificationAction): Promise<void>;
|
|
31
32
|
export declare function checkTokenAction({ dispatch, domainName, sid, token, }: {
|
|
32
|
-
dispatch: any
|
|
33
|
+
dispatch: React.Dispatch<any>;
|
|
33
34
|
domainName: string;
|
|
34
35
|
sid: string;
|
|
35
36
|
token: string;
|
|
36
|
-
}): Promise<
|
|
37
|
+
}): Promise<void>;
|
|
37
38
|
export declare function getAccessTokenAction({ dispatch, domainName, }: {
|
|
38
|
-
dispatch: any
|
|
39
|
+
dispatch: React.Dispatch<any>;
|
|
39
40
|
domainName: string;
|
|
40
41
|
}): Promise<any>;
|
|
41
|
-
export declare const resetUser: (dispatch: any) =>
|
|
42
|
+
export declare const resetUser: (dispatch: React.Dispatch<any>) => void;
|
|
42
43
|
export {};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { IDauthState } from '../initialDauthState';
|
|
2
|
+
export default function userReducer(state: IDauthState, action: any): IDauthState;
|
package/dist/routes.d.ts
ADDED
package/package.json
CHANGED
package/src/api/dauth.api.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IDauthUser } from '../initialDauthState';
|
|
2
|
+
import { routes } from '../routes';
|
|
2
3
|
import { getServerBasePath } from './utils/config';
|
|
3
4
|
|
|
4
5
|
export const getUserAPI = async (
|
|
@@ -13,7 +14,9 @@ export const getUserAPI = async (
|
|
|
13
14
|
},
|
|
14
15
|
};
|
|
15
16
|
const response = await fetch(
|
|
16
|
-
`${getServerBasePath({ domainName })}
|
|
17
|
+
`${getServerBasePath({ domainName })}/${
|
|
18
|
+
routes.tenantGetUser
|
|
19
|
+
}/${domainName}`,
|
|
17
20
|
params
|
|
18
21
|
);
|
|
19
22
|
const data = await response.json();
|
|
@@ -34,7 +37,9 @@ export const updateUserAPI = async (
|
|
|
34
37
|
body: JSON.stringify(user),
|
|
35
38
|
};
|
|
36
39
|
const response = await fetch(
|
|
37
|
-
`${getServerBasePath({ domainName })}
|
|
40
|
+
`${getServerBasePath({ domainName })}
|
|
41
|
+
/${routes.tenantUpdateUser}
|
|
42
|
+
/${domainName}`,
|
|
38
43
|
params
|
|
39
44
|
);
|
|
40
45
|
const data = await response.json();
|
|
@@ -55,7 +60,7 @@ export const sendEmailVerificationAPI = async (
|
|
|
55
60
|
const response = await fetch(
|
|
56
61
|
`${getServerBasePath({
|
|
57
62
|
domainName,
|
|
58
|
-
})}
|
|
63
|
+
})}/${routes.tenantResendEmailVerification}/${domainName}`,
|
|
59
64
|
params
|
|
60
65
|
);
|
|
61
66
|
const data = await response.json();
|
|
@@ -76,7 +81,7 @@ export const refreshAccessTokenAPI = async (
|
|
|
76
81
|
const response = await fetch(
|
|
77
82
|
`${getServerBasePath({
|
|
78
83
|
domainName,
|
|
79
|
-
})}
|
|
84
|
+
})}/${routes.tenantRefreshAccessToken}/${domainName}`,
|
|
80
85
|
params
|
|
81
86
|
);
|
|
82
87
|
const data = await response.json();
|
package/src/index.tsx
CHANGED
|
@@ -6,14 +6,12 @@ import React, {
|
|
|
6
6
|
createContext,
|
|
7
7
|
useContext,
|
|
8
8
|
} from 'react';
|
|
9
|
-
import initialDauthState, {
|
|
10
|
-
IDauthState,
|
|
11
|
-
IDauthUser,
|
|
12
|
-
} from './initialDauthState';
|
|
9
|
+
import initialDauthState, { IDauthUser } from './initialDauthState';
|
|
13
10
|
import userReducer from './reducer/dauth.reducer';
|
|
14
11
|
import * as action from './reducer/dauth.actions';
|
|
15
12
|
import { getClientBasePath } from './api/utils/config';
|
|
16
13
|
import { DAUTH_STATE } from './constants';
|
|
14
|
+
import { routes } from './routes';
|
|
17
15
|
|
|
18
16
|
interface DauthProviderProps {
|
|
19
17
|
domainName: string;
|
|
@@ -25,10 +23,9 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
25
23
|
props: DauthProviderProps
|
|
26
24
|
) => {
|
|
27
25
|
const { domainName, sid, children } = props;
|
|
28
|
-
const [
|
|
29
|
-
const dauthState = ds as IDauthState;
|
|
26
|
+
const [dauthState, dispatch] = useReducer(userReducer, initialDauthState);
|
|
30
27
|
|
|
31
|
-
// Check token
|
|
28
|
+
// Check token periodically
|
|
32
29
|
useEffect(() => {
|
|
33
30
|
if (!dauthState.isAuthenticated) return;
|
|
34
31
|
let interval = setInterval(() => {
|
|
@@ -60,7 +57,7 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
60
57
|
|
|
61
58
|
const loginWithRedirect = useCallback(() => {
|
|
62
59
|
return window.location.replace(
|
|
63
|
-
`${getClientBasePath({ domainName })}
|
|
60
|
+
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${sid}`
|
|
64
61
|
);
|
|
65
62
|
}, [domainName, sid]);
|
|
66
63
|
|
|
@@ -107,7 +104,10 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
107
104
|
const token_ls = localStorage.getItem(DAUTH_STATE);
|
|
108
105
|
if (!token_ls) return;
|
|
109
106
|
return window.location.replace(
|
|
110
|
-
`${getClientBasePath({ domainName })}
|
|
107
|
+
`${getClientBasePath({ domainName })}
|
|
108
|
+
/${routes.tenantUpdateUser}
|
|
109
|
+
/${sid}
|
|
110
|
+
/${token_ls}`
|
|
111
111
|
);
|
|
112
112
|
}, [domainName, sid]);
|
|
113
113
|
|
package/src/initialDauthState.ts
CHANGED
|
@@ -45,7 +45,7 @@ export interface IDauthState {
|
|
|
45
45
|
}: Partial<IDauthUser>) => void;
|
|
46
46
|
updateUserWithRedirect: () => void;
|
|
47
47
|
// Send email verification
|
|
48
|
-
|
|
48
|
+
sendEmailVerificationStatus: {
|
|
49
49
|
status: IActionStatus;
|
|
50
50
|
isLoading: boolean;
|
|
51
51
|
};
|
|
@@ -71,7 +71,7 @@ const initialDauthState: IDauthState = {
|
|
|
71
71
|
updateUser: () => {},
|
|
72
72
|
updateUserWithRedirect: () => {},
|
|
73
73
|
// Send email verification
|
|
74
|
-
|
|
74
|
+
sendEmailVerificationStatus: {
|
|
75
75
|
status: {
|
|
76
76
|
type: 'info',
|
|
77
77
|
message: 'Sending email verification...',
|
|
@@ -7,10 +7,11 @@ import {
|
|
|
7
7
|
import { getClientBasePath } from '../api/utils/config';
|
|
8
8
|
import { DAUTH_STATE } from '../constants';
|
|
9
9
|
import { IDauthDomainState, IDauthUser } from '../initialDauthState';
|
|
10
|
+
import { routes } from '../routes';
|
|
10
11
|
import * as DauthTypes from './dauth.types';
|
|
11
12
|
|
|
12
13
|
type TSetDauthStateAction = {
|
|
13
|
-
dispatch: any
|
|
14
|
+
dispatch: React.Dispatch<any>;
|
|
14
15
|
dauth_state: string;
|
|
15
16
|
domainName: string;
|
|
16
17
|
};
|
|
@@ -52,7 +53,7 @@ export async function setDauthStateAction({
|
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
type TSetAutoLoginAction = {
|
|
55
|
-
dispatch: any
|
|
56
|
+
dispatch: React.Dispatch<any>;
|
|
56
57
|
dauth_state_ls: string;
|
|
57
58
|
domainName: string;
|
|
58
59
|
sid: string;
|
|
@@ -87,13 +88,13 @@ export async function setAutoLoginAction({
|
|
|
87
88
|
return;
|
|
88
89
|
} else {
|
|
89
90
|
window.location.replace(
|
|
90
|
-
`${getClientBasePath({ domainName })}
|
|
91
|
+
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${sid}`
|
|
91
92
|
);
|
|
92
93
|
return resetUser(dispatch);
|
|
93
94
|
}
|
|
94
95
|
} else {
|
|
95
96
|
window.location.replace(
|
|
96
|
-
`${getClientBasePath({ domainName })}
|
|
97
|
+
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${sid}`
|
|
97
98
|
);
|
|
98
99
|
return resetUser(dispatch);
|
|
99
100
|
}
|
|
@@ -108,7 +109,11 @@ export async function setAutoLoginAction({
|
|
|
108
109
|
}
|
|
109
110
|
}
|
|
110
111
|
|
|
111
|
-
export async function setLogoutAction({
|
|
112
|
+
export async function setLogoutAction({
|
|
113
|
+
dispatch,
|
|
114
|
+
}: {
|
|
115
|
+
dispatch: React.Dispatch<any>;
|
|
116
|
+
}) {
|
|
112
117
|
dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
|
|
113
118
|
dispatch({
|
|
114
119
|
type: DauthTypes.LOGIN,
|
|
@@ -128,7 +133,7 @@ export async function setLogoutAction({ dispatch }: { dispatch: any }) {
|
|
|
128
133
|
}
|
|
129
134
|
|
|
130
135
|
type TSetUpdateAction = {
|
|
131
|
-
dispatch: any
|
|
136
|
+
dispatch: React.Dispatch<any>;
|
|
132
137
|
domainName: string;
|
|
133
138
|
user: Partial<IDauthUser>;
|
|
134
139
|
token: string | null;
|
|
@@ -165,7 +170,7 @@ export async function setUpdateUserAction({
|
|
|
165
170
|
}
|
|
166
171
|
|
|
167
172
|
type TSetSendEmailVerificationAction = {
|
|
168
|
-
dispatch: any
|
|
173
|
+
dispatch: React.Dispatch<any>;
|
|
169
174
|
domainName: string;
|
|
170
175
|
token: string;
|
|
171
176
|
};
|
|
@@ -224,7 +229,7 @@ export async function checkTokenAction({
|
|
|
224
229
|
sid,
|
|
225
230
|
token,
|
|
226
231
|
}: {
|
|
227
|
-
dispatch: any
|
|
232
|
+
dispatch: React.Dispatch<any>;
|
|
228
233
|
domainName: string;
|
|
229
234
|
sid: string;
|
|
230
235
|
token: string;
|
|
@@ -238,7 +243,7 @@ export async function checkTokenAction({
|
|
|
238
243
|
return;
|
|
239
244
|
} else {
|
|
240
245
|
window.location.replace(
|
|
241
|
-
`${getClientBasePath({ domainName })}
|
|
246
|
+
`${getClientBasePath({ domainName })}/${routes.tenantSignin}/${sid}`
|
|
242
247
|
);
|
|
243
248
|
return resetUser(dispatch);
|
|
244
249
|
}
|
|
@@ -252,7 +257,7 @@ export async function getAccessTokenAction({
|
|
|
252
257
|
dispatch,
|
|
253
258
|
domainName,
|
|
254
259
|
}: {
|
|
255
|
-
dispatch: any
|
|
260
|
+
dispatch: React.Dispatch<any>;
|
|
256
261
|
domainName: string;
|
|
257
262
|
}) {
|
|
258
263
|
const token_ls = localStorage.getItem(DAUTH_STATE);
|
|
@@ -276,7 +281,7 @@ export async function getAccessTokenAction({
|
|
|
276
281
|
|
|
277
282
|
///////////////////////////////////////////
|
|
278
283
|
//////////////////////////////////////////
|
|
279
|
-
export const resetUser = (dispatch: any) => {
|
|
284
|
+
export const resetUser = (dispatch: React.Dispatch<any>) => {
|
|
280
285
|
localStorage.removeItem(DAUTH_STATE);
|
|
281
286
|
return dispatch({
|
|
282
287
|
type: DauthTypes.LOGIN,
|
|
@@ -1,52 +1,63 @@
|
|
|
1
|
+
import { IDauthState } from '../initialDauthState';
|
|
1
2
|
import * as DauthTypes from './dauth.types';
|
|
2
3
|
|
|
3
|
-
export default function userReducer(state:
|
|
4
|
+
export default function userReducer(state: IDauthState, action: any) {
|
|
4
5
|
const { type, payload } = action;
|
|
5
6
|
|
|
6
7
|
switch (type) {
|
|
7
|
-
case DauthTypes.LOGIN:
|
|
8
|
-
|
|
8
|
+
case DauthTypes.LOGIN: {
|
|
9
|
+
const login: IDauthState = {
|
|
9
10
|
...state,
|
|
10
11
|
user: payload.user,
|
|
11
12
|
domain: payload.domain,
|
|
12
13
|
isAuthenticated: payload.isAuthenticated,
|
|
13
14
|
};
|
|
15
|
+
return login;
|
|
16
|
+
}
|
|
14
17
|
|
|
15
|
-
case DauthTypes.SET_IS_LOADING:
|
|
16
|
-
|
|
18
|
+
case DauthTypes.SET_IS_LOADING: {
|
|
19
|
+
const isLoading: IDauthState = {
|
|
17
20
|
...state,
|
|
18
21
|
isLoading: payload.isLoading,
|
|
19
22
|
};
|
|
23
|
+
return isLoading;
|
|
24
|
+
}
|
|
20
25
|
|
|
21
|
-
case DauthTypes.UPDATE_USER:
|
|
22
|
-
|
|
26
|
+
case DauthTypes.UPDATE_USER: {
|
|
27
|
+
const updateUser: IDauthState = {
|
|
23
28
|
...state,
|
|
24
29
|
user: {
|
|
25
30
|
...state.user,
|
|
26
31
|
...payload,
|
|
27
32
|
},
|
|
28
33
|
};
|
|
34
|
+
return updateUser;
|
|
35
|
+
}
|
|
29
36
|
|
|
30
|
-
case DauthTypes.SET_SEND_EMAIL_VERIFICATION_STATUS:
|
|
31
|
-
|
|
37
|
+
case DauthTypes.SET_SEND_EMAIL_VERIFICATION_STATUS: {
|
|
38
|
+
const setSendEmailVerificationStatus: IDauthState = {
|
|
32
39
|
...state,
|
|
33
|
-
|
|
34
|
-
...state.
|
|
40
|
+
sendEmailVerificationStatus: {
|
|
41
|
+
...state.sendEmailVerificationStatus,
|
|
35
42
|
status: {
|
|
36
43
|
type: payload.type,
|
|
37
44
|
message: payload.message,
|
|
38
45
|
},
|
|
39
46
|
},
|
|
40
47
|
};
|
|
48
|
+
return setSendEmailVerificationStatus;
|
|
49
|
+
}
|
|
41
50
|
|
|
42
|
-
case DauthTypes.SET_SEND_EMAIL_VERIFICATION_IS_LOADING:
|
|
43
|
-
|
|
51
|
+
case DauthTypes.SET_SEND_EMAIL_VERIFICATION_IS_LOADING: {
|
|
52
|
+
const setSendEmailVerificationIsLoading: IDauthState = {
|
|
44
53
|
...state,
|
|
45
|
-
|
|
46
|
-
...state.
|
|
54
|
+
sendEmailVerificationStatus: {
|
|
55
|
+
...state.sendEmailVerificationStatus,
|
|
47
56
|
isLoading: payload,
|
|
48
57
|
},
|
|
49
58
|
};
|
|
59
|
+
return setSendEmailVerificationIsLoading;
|
|
60
|
+
}
|
|
50
61
|
|
|
51
62
|
default:
|
|
52
63
|
return state;
|
package/src/routes.ts
ADDED