dauth-context-react 0.2.0 → 0.2.100

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/index.d.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import React from 'react';
2
- import { IDauthState } from './initialDauthState';
3
2
  interface DauthProviderProps {
4
3
  domainName: string;
5
4
  sid: string;
6
5
  children: React.ReactNode;
7
6
  }
8
7
  export declare const DauthProvider: React.FC<DauthProviderProps>;
9
- export declare const useDauth: () => IDauthState;
8
+ export declare const useDauth: () => import("./initialDauthState").IDauthState;
10
9
  export {};
@@ -31,10 +31,10 @@ export interface IDauthState {
31
31
  isAuthenticated: boolean;
32
32
  loginWithRedirect: () => void;
33
33
  logout: () => void;
34
- getAccessToken: () => any;
35
- updateUser: ({ name, lastname, nickname, tel_prefix, tel_suffix, language, avatar, }: Partial<IDauthUser>) => void;
34
+ getAccessToken: () => Promise<string>;
35
+ updateUser: ({ name, lastname, nickname, tel_prefix, tel_suffix, language, avatar, }: Partial<IDauthUser>) => Promise<boolean>;
36
36
  updateUserWithRedirect: () => void;
37
- sev: {
37
+ sendEmailVerificationStatus: {
38
38
  status: IActionStatus;
39
39
  isLoading: boolean;
40
40
  };
@@ -1,42 +1,43 @@
1
+ /// <reference types="react" />
1
2
  import { IDauthUser } from '../initialDauthState';
2
3
  declare type TSetDauthStateAction = {
3
- dispatch: any;
4
- dauth_state: string;
4
+ dispatch: React.Dispatch<any>;
5
+ token: string;
5
6
  domainName: string;
6
7
  };
7
- export declare function setDauthStateAction({ dispatch, dauth_state, domainName, }: TSetDauthStateAction): Promise<any>;
8
+ export declare function setDauthStateAction({ dispatch, token, 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<any>;
15
- export declare function setLogoutAction({ dispatch }: {
16
- dispatch: any;
17
- }): Promise<any>;
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<any>;
25
+ export declare function setUpdateUserAction({ dispatch, domainName, user, token, }: TSetUpdateAction): Promise<boolean>;
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<any>;
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<any>;
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) => any;
42
+ export declare const resetUser: (dispatch: React.Dispatch<any>) => void;
42
43
  export {};
@@ -1 +1,2 @@
1
- export default function userReducer(state: any, action: any): any;
1
+ import { IDauthState } from '../initialDauthState';
2
+ export default function userReducer(state: IDauthState, action: any): IDauthState;
@@ -0,0 +1,7 @@
1
+ export declare const routes: {
2
+ tenantSignin: string;
3
+ tenantUpdateUser: string;
4
+ tenantGetUser: string;
5
+ tenantResendEmailVerification: string;
6
+ tenantRefreshAccessToken: string;
7
+ };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.0",
2
+ "version": "0.2.100",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -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 })}/t-get-user/${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 })}/t-update-user/${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
- })}/t-resend-email-verification/${domainName}`,
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
- })}/t-refresh-access-token/${domainName}`,
84
+ })}/${routes.tenantRefreshAccessToken}/${domainName}`,
80
85
  params
81
86
  );
82
87
  const data = await response.json();
package/src/constants.ts CHANGED
@@ -1 +1 @@
1
- export const DAUTH_STATE = 'dauth_state';
1
+ export const TOKEN_LS = 'dauth_state';
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
- import { DAUTH_STATE } from './constants';
13
+ import { TOKEN_LS } from './constants';
14
+ import { routes } from './routes';
17
15
 
18
16
  interface DauthProviderProps {
19
17
  domainName: string;
@@ -25,14 +23,13 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
25
23
  props: DauthProviderProps
26
24
  ) => {
27
25
  const { domainName, sid, children } = props;
28
- const [ds, dispatch] = useReducer(userReducer, initialDauthState);
29
- const dauthState = ds as IDauthState;
26
+ const [dauthState, dispatch] = useReducer(userReducer, initialDauthState);
30
27
 
31
28
  // Check token periodically
32
29
  useEffect(() => {
33
30
  if (!dauthState.isAuthenticated) return;
34
31
  let interval = setInterval(() => {
35
- const token_ls = localStorage.getItem(DAUTH_STATE);
32
+ const token_ls = localStorage.getItem(TOKEN_LS);
36
33
  if (!token_ls) return;
37
34
  action.checkTokenAction({ dispatch, domainName, sid, token: token_ls });
38
35
  }, 1000 * 60 * 2);
@@ -44,15 +41,15 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
44
41
  const queryString = window.location.search;
45
42
  if (!queryString) return;
46
43
  const urlParams = new URLSearchParams(queryString);
47
- const dauth_state = urlParams.get(DAUTH_STATE);
48
- if (dauth_state && !dauthState.isAuthenticated) {
49
- action.setDauthStateAction({ dispatch, dauth_state, domainName });
44
+ const token_url = urlParams.get(TOKEN_LS);
45
+ if (token_url && !dauthState.isAuthenticated) {
46
+ action.setDauthStateAction({ dispatch, token: token_url, domainName });
50
47
  }
51
48
  }, []);
52
49
 
53
50
  // Auto Login
54
51
  useEffect(() => {
55
- const dauth_state_ls = localStorage.getItem(DAUTH_STATE);
52
+ const dauth_state_ls = localStorage.getItem(TOKEN_LS);
56
53
  if (dauth_state_ls && !dauthState.isAuthenticated) {
57
54
  action.setAutoLoginAction({ dispatch, dauth_state_ls, domainName, sid });
58
55
  }
@@ -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 })}/t-signin/${sid}`
60
+ `${getClientBasePath({ domainName })}/${routes.tenantSignin}/${sid}`
64
61
  );
65
62
  }, [domainName, sid]);
66
63
 
@@ -70,11 +67,11 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
70
67
 
71
68
  const getAccessToken = useCallback(async () => {
72
69
  const token = await action.getAccessTokenAction({ dispatch, domainName });
73
- return token;
70
+ return token as string;
74
71
  }, []);
75
72
 
76
73
  const updateUser = useCallback(
77
- ({
74
+ async ({
78
75
  name,
79
76
  lastname,
80
77
  nickname,
@@ -83,7 +80,7 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
83
80
  language,
84
81
  avatar,
85
82
  }: Partial<IDauthUser>) => {
86
- const token_ls = localStorage.getItem(DAUTH_STATE);
83
+ const token_ls = localStorage.getItem(TOKEN_LS);
87
84
  const user = {
88
85
  name,
89
86
  lastname,
@@ -93,26 +90,29 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
93
90
  language,
94
91
  avatar,
95
92
  } as Partial<IDauthUser>;
96
- return action.setUpdateUserAction({
93
+ return (await action.setUpdateUserAction({
97
94
  dispatch,
98
95
  domainName,
99
96
  user,
100
97
  token: token_ls,
101
- });
98
+ })) as boolean;
102
99
  },
103
100
  [domainName]
104
101
  );
105
102
 
106
103
  const updateUserWithRedirect = useCallback(() => {
107
- const token_ls = localStorage.getItem(DAUTH_STATE);
104
+ const token_ls = localStorage.getItem(TOKEN_LS);
108
105
  if (!token_ls) return;
109
106
  return window.location.replace(
110
- `${getClientBasePath({ domainName })}/t-update-user/${sid}/${token_ls}`
107
+ `${getClientBasePath({ domainName })}
108
+ /${routes.tenantUpdateUser}
109
+ /${sid}
110
+ /${token_ls}`
111
111
  );
112
112
  }, [domainName, sid]);
113
113
 
114
114
  const sendEmailVerification = useCallback(() => {
115
- const token_ls = localStorage.getItem(DAUTH_STATE);
115
+ const token_ls = localStorage.getItem(TOKEN_LS);
116
116
  if (!token_ls) return;
117
117
  return action.sendEmailVerificationAction({
118
118
  dispatch,
@@ -33,7 +33,7 @@ export interface IDauthState {
33
33
  isAuthenticated: boolean;
34
34
  loginWithRedirect: () => void;
35
35
  logout: () => void;
36
- getAccessToken: () => any;
36
+ getAccessToken: () => Promise<string>;
37
37
  updateUser: ({
38
38
  name,
39
39
  lastname,
@@ -42,10 +42,10 @@ export interface IDauthState {
42
42
  tel_suffix,
43
43
  language,
44
44
  avatar,
45
- }: Partial<IDauthUser>) => void;
45
+ }: Partial<IDauthUser>) => Promise<boolean>;
46
46
  updateUserWithRedirect: () => void;
47
47
  // Send email verification
48
- sev: {
48
+ sendEmailVerificationStatus: {
49
49
  status: IActionStatus;
50
50
  isLoading: boolean;
51
51
  };
@@ -67,11 +67,11 @@ const initialDauthState: IDauthState = {
67
67
  isAuthenticated: false,
68
68
  loginWithRedirect: () => {},
69
69
  logout: () => {},
70
- getAccessToken: () => initialDauthState.getAccessToken() || '',
71
- updateUser: () => {},
70
+ getAccessToken: () => Promise.resolve(''),
71
+ updateUser: () => Promise.resolve(false),
72
72
  updateUserWithRedirect: () => {},
73
73
  // Send email verification
74
- sev: {
74
+ sendEmailVerificationStatus: {
75
75
  status: {
76
76
  type: 'info',
77
77
  message: 'Sending email verification...',
@@ -5,23 +5,24 @@ import {
5
5
  updateUserAPI,
6
6
  } from '../api/dauth.api';
7
7
  import { getClientBasePath } from '../api/utils/config';
8
- import { DAUTH_STATE } from '../constants';
8
+ import { TOKEN_LS } 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
- dauth_state: string;
14
+ dispatch: React.Dispatch<any>;
15
+ token: string;
15
16
  domainName: string;
16
17
  };
17
18
  export async function setDauthStateAction({
18
19
  dispatch,
19
- dauth_state,
20
+ token,
20
21
  domainName,
21
22
  }: TSetDauthStateAction) {
22
23
  dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
23
24
  try {
24
- const getUserFetch = await getUserAPI(domainName, dauth_state);
25
+ const getUserFetch = await getUserAPI(domainName, token);
25
26
  if (getUserFetch.response.status === 200) {
26
27
  dispatch({
27
28
  type: DauthTypes.LOGIN,
@@ -36,7 +37,7 @@ export async function setDauthStateAction({
36
37
  document.title,
37
38
  getUserFetch.data.domain.loginRedirect
38
39
  );
39
- return localStorage.setItem(DAUTH_STATE, dauth_state);
40
+ return localStorage.setItem(TOKEN_LS, token);
40
41
  } else {
41
42
  return resetUser(dispatch);
42
43
  }
@@ -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;
@@ -81,19 +82,19 @@ export async function setAutoLoginAction({
81
82
  },
82
83
  });
83
84
  localStorage.setItem(
84
- DAUTH_STATE,
85
+ TOKEN_LS,
85
86
  refreshAccessTokenFetch.data.accessToken
86
87
  );
87
88
  return;
88
89
  } else {
89
90
  window.location.replace(
90
- `${getClientBasePath({ domainName })}/t-signin/${sid}`
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 })}/t-signin/${sid}`
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({ dispatch }: { dispatch: any }) {
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,
@@ -120,7 +125,7 @@ export async function setLogoutAction({ dispatch }: { dispatch: any }) {
120
125
  isAuthenticated: false,
121
126
  },
122
127
  });
123
- localStorage.removeItem(DAUTH_STATE);
128
+ localStorage.removeItem(TOKEN_LS);
124
129
  return dispatch({
125
130
  type: DauthTypes.SET_IS_LOADING,
126
131
  payload: { isLoading: false },
@@ -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;
@@ -143,29 +148,32 @@ export async function setUpdateUserAction({
143
148
  window.document.documentElement.setAttribute('lang', user.language);
144
149
  }
145
150
  if (!token) {
146
- return dispatch({
151
+ dispatch({
147
152
  type: DauthTypes.UPDATE_USER,
148
153
  payload: user,
149
154
  });
155
+ return false;
150
156
  }
151
157
  try {
152
158
  const getUserFetch = await updateUserAPI(domainName, user, token);
153
159
  if (getUserFetch.response.status === 200) {
154
- return dispatch({
160
+ dispatch({
155
161
  type: DauthTypes.UPDATE_USER,
156
162
  payload: getUserFetch.data.user,
157
163
  });
164
+ return true;
158
165
  } else {
159
166
  console.log('Update user error', getUserFetch.data.message);
160
- return;
167
+ return false;
161
168
  }
162
169
  } catch (error) {
163
170
  console.log('Update user error', error);
171
+ return false;
164
172
  }
165
173
  }
166
174
 
167
175
  type TSetSendEmailVerificationAction = {
168
- dispatch: any;
176
+ dispatch: React.Dispatch<any>;
169
177
  domainName: string;
170
178
  token: string;
171
179
  };
@@ -224,7 +232,7 @@ export async function checkTokenAction({
224
232
  sid,
225
233
  token,
226
234
  }: {
227
- dispatch: any;
235
+ dispatch: React.Dispatch<any>;
228
236
  domainName: string;
229
237
  sid: string;
230
238
  token: string;
@@ -238,7 +246,7 @@ export async function checkTokenAction({
238
246
  return;
239
247
  } else {
240
248
  window.location.replace(
241
- `${getClientBasePath({ domainName })}/t-signin/${sid}`
249
+ `${getClientBasePath({ domainName })}/${routes.tenantSignin}/${sid}`
242
250
  );
243
251
  return resetUser(dispatch);
244
252
  }
@@ -252,10 +260,10 @@ export async function getAccessTokenAction({
252
260
  dispatch,
253
261
  domainName,
254
262
  }: {
255
- dispatch: any;
263
+ dispatch: React.Dispatch<any>;
256
264
  domainName: string;
257
265
  }) {
258
- const token_ls = localStorage.getItem(DAUTH_STATE);
266
+ const token_ls = localStorage.getItem(TOKEN_LS);
259
267
  if (!token_ls) return;
260
268
  try {
261
269
  const refreshAccessTokenFetch = await refreshAccessTokenAPI(
@@ -276,8 +284,8 @@ export async function getAccessTokenAction({
276
284
 
277
285
  ///////////////////////////////////////////
278
286
  //////////////////////////////////////////
279
- export const resetUser = (dispatch: any) => {
280
- localStorage.removeItem(DAUTH_STATE);
287
+ export const resetUser = (dispatch: React.Dispatch<any>) => {
288
+ localStorage.removeItem(TOKEN_LS);
281
289
  return dispatch({
282
290
  type: DauthTypes.LOGIN,
283
291
  payload: {
@@ -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: any, action: any) {
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
- return {
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
- return {
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
- return {
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
- return {
37
+ case DauthTypes.SET_SEND_EMAIL_VERIFICATION_STATUS: {
38
+ const setSendEmailVerificationStatus: IDauthState = {
32
39
  ...state,
33
- sev: {
34
- ...state.sev,
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
- return {
51
+ case DauthTypes.SET_SEND_EMAIL_VERIFICATION_IS_LOADING: {
52
+ const setSendEmailVerificationIsLoading: IDauthState = {
44
53
  ...state,
45
- sev: {
46
- ...state.sev,
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
@@ -0,0 +1,7 @@
1
+ export const routes = {
2
+ tenantSignin: 't-signin',
3
+ tenantUpdateUser: 't-update-user',
4
+ tenantGetUser: 't-get-user',
5
+ tenantResendEmailVerification: 't-resend-email-verification',
6
+ tenantRefreshAccessToken: 't-refresh-access-token',
7
+ };