dauth-context-react 0.1.2 → 0.1.4

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.
@@ -1,63 +1,42 @@
1
1
  export interface IDauthUser {
2
- _id: string
3
- ssid: string
4
- name: string
5
- lastname: string
6
- nickname: string
7
- email: string
8
- is_verified: boolean
9
- language: string
10
- avatar: string
11
- createdAt: Date
12
- updatedAt: Date
13
- last_login: Date
2
+ _id: string;
3
+ ssid: string;
4
+ name: string;
5
+ lastname: string;
6
+ nickname: string;
7
+ email: string;
8
+ is_verified: boolean;
9
+ language: string;
10
+ avatar: string;
11
+ createdAt: Date;
12
+ updatedAt: Date;
13
+ last_login: Date;
14
14
  }
15
15
 
16
- // export interface IDauthDomain {
17
- // name: string
18
- // loginRedirect: string
19
- // allowedOrigins: string[]
20
- // }
21
-
22
- // export interface IDauthState {
23
- // user: IDauthUser
24
- // domain: IDauthDomain
25
- // isLoading: boolean
26
- // isAuthenticated: boolean
27
- // loginWithRedirect: () => void
28
- // logout: () => void
29
- // getAccessToken: () => void
30
- // }
31
-
32
- // const userState = {
33
- // _id: "",
34
- // ssid: "",
35
- // name: "",
36
- // lastname: "",
37
- // nickname: "",
38
- // email: "",
39
- // is_verified: false,
40
- // language: "",
41
- // avatar: "",
42
- // createdAt: new Date(),
43
- // updatedAt: new Date(),
44
- // last_login: new Date(),
45
- // }
46
-
47
- const domainState = {
48
- name: "",
49
- loginRedirect: "",
50
- allowedOrigins: [""],
16
+ export interface IDauthDomain {
17
+ name: string;
18
+ loginRedirect: string;
19
+ allowedOrigins: string[];
51
20
  }
52
21
 
53
- const initialDauthState = {
54
- user: {} as IDauthUser,
55
- domain: domainState,
56
- isLoading: true,
57
- isAuthenticated: false,
58
- loginWithRedirect: () => { },
59
- logout: () => { },
60
- getAccessToken: () => { },
22
+ export interface IDauthState {
23
+ user: IDauthUser;
24
+ domain: IDauthDomain;
25
+ isLoading: boolean;
26
+ isAuthenticated: boolean;
27
+ loginWithRedirect: () => void;
28
+ logout: () => void;
29
+ getAccessToken: () => void;
61
30
  }
62
31
 
63
- export default initialDauthState
32
+ const initialDauthState: IDauthState = {
33
+ user: {} as IDauthUser,
34
+ domain: {} as IDauthDomain,
35
+ isLoading: true,
36
+ isAuthenticated: false,
37
+ loginWithRedirect: () => {},
38
+ logout: () => {},
39
+ getAccessToken: () => {},
40
+ };
41
+
42
+ export default initialDauthState;
@@ -1,9 +1,20 @@
1
1
  import { getTenantUserAPI } from '../api/dauth.api';
2
2
  import { DAUTH_STATE } from '../constants';
3
- import * as DauthTypes from './dauth.types'
3
+ import * as DauthTypes from './dauth.types';
4
4
 
5
- export async function setDauthStateAction({ dispatch, dauth_state, domainName, ssid }: { dispatch: any, dauth_state: string, domainName: string, ssid: string }) {
6
- dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } })
5
+ type TSetDauthStateAction = {
6
+ dispatch: any;
7
+ dauth_state: string;
8
+ domainName: string;
9
+ ssid: string;
10
+ };
11
+ export async function setDauthStateAction({
12
+ dispatch,
13
+ dauth_state,
14
+ domainName,
15
+ ssid,
16
+ }: TSetDauthStateAction) {
17
+ dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
7
18
  try {
8
19
  const getUserFetch = await getTenantUserAPI(domainName, ssid, dauth_state);
9
20
  if (getUserFetch.response.status === 200) {
@@ -14,8 +25,12 @@ export async function setDauthStateAction({ dispatch, dauth_state, domainName, s
14
25
  domain: getUserFetch.data.domain,
15
26
  isAuthenticated: true,
16
27
  },
17
- })
18
- window.history.replaceState({}, document.title, getUserFetch.data.domain.loginRedirect);
28
+ });
29
+ window.history.replaceState(
30
+ {},
31
+ document.title,
32
+ getUserFetch.data.domain.loginRedirect
33
+ );
19
34
  return localStorage.setItem(DAUTH_STATE, dauth_state);
20
35
  } else {
21
36
  return localStorage.removeItem(DAUTH_STATE);
@@ -24,14 +39,32 @@ export async function setDauthStateAction({ dispatch, dauth_state, domainName, s
24
39
  localStorage.removeItem(DAUTH_STATE);
25
40
  console.log(error);
26
41
  } finally {
27
- dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: false } })
42
+ dispatch({
43
+ type: DauthTypes.SET_IS_LOADING,
44
+ payload: { isLoading: false },
45
+ });
28
46
  }
29
47
  }
30
48
 
31
- export async function setAutoLoginAction({ dispatch, dauth_state_ls, domainName, ssid }: { dispatch: any, dauth_state_ls: string, domainName: string, ssid: string }) {
32
- dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } })
49
+ type TSetAutoLoginAction = {
50
+ dispatch: any;
51
+ dauth_state_ls: string;
52
+ domainName: string;
53
+ ssid: string;
54
+ };
55
+ export async function setAutoLoginAction({
56
+ dispatch,
57
+ dauth_state_ls,
58
+ domainName,
59
+ ssid,
60
+ }: TSetAutoLoginAction) {
61
+ dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
33
62
  try {
34
- const getUserFetch = await getTenantUserAPI(domainName, ssid, dauth_state_ls);
63
+ const getUserFetch = await getTenantUserAPI(
64
+ domainName,
65
+ ssid,
66
+ dauth_state_ls
67
+ );
35
68
  if (getUserFetch.response.status === 200) {
36
69
  dispatch({
37
70
  type: DauthTypes.LOGIN,
@@ -40,22 +73,24 @@ export async function setAutoLoginAction({ dispatch, dauth_state_ls, domainName,
40
73
  domain: getUserFetch.data.domain,
41
74
  isAuthenticated: true,
42
75
  },
43
- })
76
+ });
44
77
  localStorage.setItem(DAUTH_STATE, dauth_state_ls);
45
78
  } else {
46
79
  localStorage.removeItem(DAUTH_STATE);
47
80
  }
48
-
49
81
  } catch (error) {
50
82
  localStorage.removeItem(DAUTH_STATE);
51
83
  console.log(error);
52
84
  } finally {
53
- dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: false } })
85
+ dispatch({
86
+ type: DauthTypes.SET_IS_LOADING,
87
+ payload: { isLoading: false },
88
+ });
54
89
  }
55
90
  }
56
91
 
57
92
  export async function setLogoutAction({ dispatch }: { dispatch: any }) {
58
- dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } })
93
+ dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
59
94
  dispatch({
60
95
  type: DauthTypes.LOGIN,
61
96
  payload: {
@@ -63,7 +98,10 @@ export async function setLogoutAction({ dispatch }: { dispatch: any }) {
63
98
  domain: {},
64
99
  isAuthenticated: false,
65
100
  },
66
- })
101
+ });
67
102
  localStorage.removeItem(DAUTH_STATE);
68
- return dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: false } })
69
- }
103
+ return dispatch({
104
+ type: DauthTypes.SET_IS_LOADING,
105
+ payload: { isLoading: false },
106
+ });
107
+ }
@@ -1,24 +1,24 @@
1
- import * as DauthTypes from './dauth.types'
1
+ import * as DauthTypes from './dauth.types';
2
2
 
3
3
  export default function userReducer(state: any, action: any) {
4
- const { type, payload } = action;
4
+ const { type, payload } = action;
5
5
 
6
- switch (type) {
7
- case DauthTypes.LOGIN:
8
- return {
9
- ...state,
10
- user: payload.user,
11
- domain: payload.domain,
12
- isAuthenticated: payload.isAuthenticated,
13
- }
6
+ switch (type) {
7
+ case DauthTypes.LOGIN:
8
+ return {
9
+ ...state,
10
+ user: payload.user,
11
+ domain: payload.domain,
12
+ isAuthenticated: payload.isAuthenticated,
13
+ };
14
14
 
15
- case DauthTypes.SET_IS_LOADING:
16
- return {
17
- ...state,
18
- isLoading: payload.isLoading,
19
- }
15
+ case DauthTypes.SET_IS_LOADING:
16
+ return {
17
+ ...state,
18
+ isLoading: payload.isLoading,
19
+ };
20
20
 
21
- default:
22
- return state
23
- }
24
- }
21
+ default:
22
+ return state;
23
+ }
24
+ }
@@ -1,2 +1,2 @@
1
1
  export const LOGIN = 'LOGIN';
2
- export const SET_IS_LOADING = 'SET_IS_LOADING';
2
+ export const SET_IS_LOADING = 'SET_IS_LOADING';