dauth-context-react 0.2.108 → 0.2.110
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 +6 -1
- package/dist/{routes.d.ts → api/utils/routes.d.ts} +1 -0
- package/dist/dauth-context-react.cjs.development.js +211 -68
- 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 +212 -69
- package/dist/dauth-context-react.esm.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/initialDauthState.d.ts +1 -45
- package/dist/interfaces.d.ts +45 -0
- package/dist/reducer/dauth.actions.d.ts +1 -1
- package/dist/reducer/dauth.reducer.d.ts +1 -1
- package/package.json +2 -5
- package/src/api/dauth.api.ts +29 -2
- package/src/{routes.ts → api/utils/routes.ts} +1 -0
- package/src/index.tsx +64 -31
- package/src/initialDauthState.ts +1 -57
- package/src/interfaces.ts +57 -0
- package/src/reducer/dauth.actions.ts +2 -2
- package/src/reducer/dauth.reducer.ts +1 -1
|
@@ -1,47 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
_id: string;
|
|
3
|
-
name: string;
|
|
4
|
-
lastname: string;
|
|
5
|
-
nickname: string;
|
|
6
|
-
email: string;
|
|
7
|
-
isVerified: boolean;
|
|
8
|
-
language: string;
|
|
9
|
-
avatar: {
|
|
10
|
-
id: string;
|
|
11
|
-
url: string;
|
|
12
|
-
};
|
|
13
|
-
role: string;
|
|
14
|
-
telPrefix: string;
|
|
15
|
-
telSuffix: string;
|
|
16
|
-
createdAt: Date;
|
|
17
|
-
updatedAt: Date;
|
|
18
|
-
lastLogin: Date;
|
|
19
|
-
}
|
|
20
|
-
export interface IDauthDomainState {
|
|
21
|
-
name: string;
|
|
22
|
-
loginRedirect: string;
|
|
23
|
-
allowedOrigins: string[];
|
|
24
|
-
}
|
|
25
|
-
export interface IDauthState {
|
|
26
|
-
user: IDauthUser;
|
|
27
|
-
domain: IDauthDomainState;
|
|
28
|
-
isLoading: boolean;
|
|
29
|
-
isAuthenticated: boolean;
|
|
30
|
-
loginWithRedirect: () => void;
|
|
31
|
-
logout: () => void;
|
|
32
|
-
getAccessToken: () => Promise<string>;
|
|
33
|
-
updateUser: ({ name, lastname, nickname, telPrefix, telSuffix, language, avatar, }: Partial<IDauthUser>) => Promise<boolean>;
|
|
34
|
-
updateUserWithRedirect: () => void;
|
|
35
|
-
sendEmailVerificationStatus: {
|
|
36
|
-
status: IActionStatus;
|
|
37
|
-
isLoading: boolean;
|
|
38
|
-
};
|
|
39
|
-
sendEmailVerification: () => Promise<boolean>;
|
|
40
|
-
}
|
|
41
|
-
export interface IActionStatus {
|
|
42
|
-
type: TStatusTypes;
|
|
43
|
-
message: string;
|
|
44
|
-
}
|
|
45
|
-
export declare type TStatusTypes = 'success' | 'error' | 'info' | 'warning';
|
|
1
|
+
import { IDauthState } from "./interfaces";
|
|
46
2
|
declare const initialDauthState: IDauthState;
|
|
47
3
|
export default initialDauthState;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface IDauthUser {
|
|
2
|
+
_id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
lastname: string;
|
|
5
|
+
nickname: string;
|
|
6
|
+
email: string;
|
|
7
|
+
isVerified: boolean;
|
|
8
|
+
language: string;
|
|
9
|
+
avatar: {
|
|
10
|
+
id: string;
|
|
11
|
+
url: string;
|
|
12
|
+
};
|
|
13
|
+
role: string;
|
|
14
|
+
telPrefix: string;
|
|
15
|
+
telSuffix: string;
|
|
16
|
+
createdAt: Date;
|
|
17
|
+
updatedAt: Date;
|
|
18
|
+
lastLogin: Date;
|
|
19
|
+
}
|
|
20
|
+
export interface IDauthDomainState {
|
|
21
|
+
name: string;
|
|
22
|
+
loginRedirect: string;
|
|
23
|
+
allowedOrigins: string[];
|
|
24
|
+
}
|
|
25
|
+
export interface IDauthState {
|
|
26
|
+
user: IDauthUser;
|
|
27
|
+
domain: IDauthDomainState;
|
|
28
|
+
isLoading: boolean;
|
|
29
|
+
isAuthenticated: boolean;
|
|
30
|
+
loginWithRedirect: () => void;
|
|
31
|
+
logout: () => void;
|
|
32
|
+
getAccessToken: () => Promise<string>;
|
|
33
|
+
updateUser: ({ name, lastname, nickname, telPrefix, telSuffix, language, avatar, }: Partial<IDauthUser>) => Promise<boolean>;
|
|
34
|
+
updateUserWithRedirect: () => void;
|
|
35
|
+
sendEmailVerificationStatus: {
|
|
36
|
+
status: IActionStatus;
|
|
37
|
+
isLoading: boolean;
|
|
38
|
+
};
|
|
39
|
+
sendEmailVerification: () => Promise<boolean>;
|
|
40
|
+
}
|
|
41
|
+
export interface IActionStatus {
|
|
42
|
+
type: TStatusTypes;
|
|
43
|
+
message: string;
|
|
44
|
+
}
|
|
45
|
+
export declare type TStatusTypes = 'success' | 'error' | 'info' | 'warning';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { IDauthState } from '../
|
|
1
|
+
import { IDauthState } from '../interfaces';
|
|
2
2
|
export default function userReducer(state: IDauthState, action: any): IDauthState;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.2.
|
|
2
|
+
"version": "0.2.110",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"typings": "dist/index.d.ts",
|
|
@@ -62,8 +62,5 @@
|
|
|
62
62
|
"react",
|
|
63
63
|
"context",
|
|
64
64
|
"authentication"
|
|
65
|
-
]
|
|
66
|
-
"dependencies": {
|
|
67
|
-
"expo-jwt": "^1.8.0"
|
|
68
|
-
}
|
|
65
|
+
]
|
|
69
66
|
}
|
package/src/api/dauth.api.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { routes } from '../routes';
|
|
1
|
+
import { routes } from './utils/routes';
|
|
3
2
|
import { getServerBasePath } from './utils/config';
|
|
3
|
+
import { IDauthUser } from '../interfaces';
|
|
4
4
|
|
|
5
5
|
export const getUserAPI = async (
|
|
6
6
|
domainName: string,
|
|
@@ -87,3 +87,30 @@ export const refreshAccessTokenAPI = async (
|
|
|
87
87
|
const data = await response.json();
|
|
88
88
|
return { response, data };
|
|
89
89
|
};
|
|
90
|
+
|
|
91
|
+
export const verifyTokenAPI = async ({
|
|
92
|
+
domainName,
|
|
93
|
+
ask,
|
|
94
|
+
token,
|
|
95
|
+
}: {
|
|
96
|
+
domainName: string;
|
|
97
|
+
ask: string;
|
|
98
|
+
token: string;
|
|
99
|
+
}): Promise<any> => {
|
|
100
|
+
const params = {
|
|
101
|
+
method: 'POST',
|
|
102
|
+
headers: {
|
|
103
|
+
Authorization: token,
|
|
104
|
+
'Content-Type': 'application/json',
|
|
105
|
+
},
|
|
106
|
+
body: JSON.stringify({ ask }),
|
|
107
|
+
};
|
|
108
|
+
const response = await fetch(
|
|
109
|
+
`${getServerBasePath({ domainName })}/${
|
|
110
|
+
routes.tenantVerifyToken
|
|
111
|
+
}/${domainName}`,
|
|
112
|
+
params
|
|
113
|
+
);
|
|
114
|
+
const data = await response.json();
|
|
115
|
+
return { response, data };
|
|
116
|
+
};
|
package/src/index.tsx
CHANGED
|
@@ -6,13 +6,14 @@ import React, {
|
|
|
6
6
|
createContext,
|
|
7
7
|
useContext,
|
|
8
8
|
} from 'react';
|
|
9
|
-
import initialDauthState
|
|
9
|
+
import initialDauthState from './initialDauthState';
|
|
10
10
|
import userReducer from './reducer/dauth.reducer';
|
|
11
11
|
import * as action from './reducer/dauth.actions';
|
|
12
12
|
import { getClientBasePath } from './api/utils/config';
|
|
13
13
|
import { TOKEN_LS } from './constants';
|
|
14
|
-
import { routes } from './routes';
|
|
15
|
-
import
|
|
14
|
+
import { routes } from './api/utils/routes';
|
|
15
|
+
import { verifyTokenAPI } from './api/dauth.api';
|
|
16
|
+
import { IDauthUser } from './interfaces';
|
|
16
17
|
|
|
17
18
|
interface DauthProviderProps {
|
|
18
19
|
domainName: string;
|
|
@@ -26,48 +27,80 @@ export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
|
26
27
|
const { domainName, ask, children } = props;
|
|
27
28
|
const [dauthState, dispatch] = useReducer(userReducer, initialDauthState);
|
|
28
29
|
|
|
30
|
+
const isValidAsk = useCallback(
|
|
31
|
+
async (token: string) => {
|
|
32
|
+
const verifyToken = await verifyTokenAPI({
|
|
33
|
+
domainName,
|
|
34
|
+
token,
|
|
35
|
+
ask,
|
|
36
|
+
});
|
|
37
|
+
if (verifyToken.response.status !== 200) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
},
|
|
42
|
+
[domainName, ask]
|
|
43
|
+
);
|
|
44
|
+
|
|
29
45
|
// Check token periodically
|
|
30
46
|
useEffect(() => {
|
|
31
47
|
if (!dauthState.isAuthenticated) return;
|
|
32
|
-
let interval = setInterval(() => {
|
|
48
|
+
let interval = setInterval(async () => {
|
|
33
49
|
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
34
50
|
if (!token_ls) return;
|
|
35
|
-
|
|
51
|
+
const isValid = await isValidAsk(token_ls);
|
|
52
|
+
if (isValid) {
|
|
53
|
+
return action.checkTokenAction({
|
|
54
|
+
dispatch,
|
|
55
|
+
domainName,
|
|
56
|
+
token: token_ls,
|
|
57
|
+
});
|
|
58
|
+
} else {
|
|
59
|
+
return action.setLogoutAction({ dispatch });
|
|
60
|
+
}
|
|
36
61
|
}, 1000 * 60 * 1);
|
|
37
62
|
return () => clearInterval(interval);
|
|
38
|
-
}, []);
|
|
63
|
+
}, [dauthState.isAuthenticated, isValidAsk]);
|
|
39
64
|
|
|
40
65
|
// Catch login redirect
|
|
41
66
|
useEffect(() => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
67
|
+
(async () => {
|
|
68
|
+
const queryString = window.location.search;
|
|
69
|
+
if (!queryString) return;
|
|
70
|
+
const urlParams = new URLSearchParams(queryString);
|
|
71
|
+
const token_url = urlParams.get(TOKEN_LS);
|
|
72
|
+
if (token_url && !dauthState.isAuthenticated) {
|
|
73
|
+
const isValid = await isValidAsk(token_url);
|
|
74
|
+
if (isValid) {
|
|
75
|
+
return action.setDauthStateAction({
|
|
76
|
+
dispatch,
|
|
77
|
+
token: token_url,
|
|
78
|
+
domainName,
|
|
79
|
+
});
|
|
80
|
+
} else {
|
|
81
|
+
return action.setLogoutAction({ dispatch });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
})();
|
|
49
85
|
}, []);
|
|
50
86
|
|
|
51
87
|
// Auto Login
|
|
52
88
|
useEffect(() => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
domainName,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
89
|
+
(async () => {
|
|
90
|
+
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
91
|
+
if (token_ls && !dauthState.isAuthenticated) {
|
|
92
|
+
const isValid = await isValidAsk(token_ls);
|
|
93
|
+
if (isValid) {
|
|
94
|
+
return action.setAutoLoginAction({
|
|
95
|
+
dispatch,
|
|
96
|
+
dauth_state_ls: token_ls,
|
|
97
|
+
domainName,
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
return action.setLogoutAction({ dispatch });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
})();
|
|
71
104
|
}, []);
|
|
72
105
|
|
|
73
106
|
const loginWithRedirect = useCallback(() => {
|
package/src/initialDauthState.ts
CHANGED
|
@@ -1,60 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
_id: string;
|
|
3
|
-
name: string;
|
|
4
|
-
lastname: string;
|
|
5
|
-
nickname: string;
|
|
6
|
-
email: string;
|
|
7
|
-
isVerified: boolean;
|
|
8
|
-
language: string;
|
|
9
|
-
avatar: {
|
|
10
|
-
id: string;
|
|
11
|
-
url: string;
|
|
12
|
-
};
|
|
13
|
-
role: string;
|
|
14
|
-
telPrefix: string;
|
|
15
|
-
telSuffix: string;
|
|
16
|
-
createdAt: Date;
|
|
17
|
-
updatedAt: Date;
|
|
18
|
-
lastLogin: Date;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface IDauthDomainState {
|
|
22
|
-
name: string;
|
|
23
|
-
loginRedirect: string;
|
|
24
|
-
allowedOrigins: string[];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface IDauthState {
|
|
28
|
-
user: IDauthUser;
|
|
29
|
-
domain: IDauthDomainState;
|
|
30
|
-
isLoading: boolean;
|
|
31
|
-
isAuthenticated: boolean;
|
|
32
|
-
loginWithRedirect: () => void;
|
|
33
|
-
logout: () => void;
|
|
34
|
-
getAccessToken: () => Promise<string>;
|
|
35
|
-
updateUser: ({
|
|
36
|
-
name,
|
|
37
|
-
lastname,
|
|
38
|
-
nickname,
|
|
39
|
-
telPrefix,
|
|
40
|
-
telSuffix,
|
|
41
|
-
language,
|
|
42
|
-
avatar,
|
|
43
|
-
}: Partial<IDauthUser>) => Promise<boolean>;
|
|
44
|
-
updateUserWithRedirect: () => void;
|
|
45
|
-
// Send email verification
|
|
46
|
-
sendEmailVerificationStatus: {
|
|
47
|
-
status: IActionStatus;
|
|
48
|
-
isLoading: boolean;
|
|
49
|
-
};
|
|
50
|
-
sendEmailVerification: () => Promise<boolean>;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface IActionStatus {
|
|
54
|
-
type: TStatusTypes;
|
|
55
|
-
message: string;
|
|
56
|
-
}
|
|
57
|
-
export type TStatusTypes = 'success' | 'error' | 'info' | 'warning';
|
|
1
|
+
import { IActionStatus, IDauthDomainState, IDauthState, IDauthUser } from "./interfaces";
|
|
58
2
|
|
|
59
3
|
const initialDauthState: IDauthState = {
|
|
60
4
|
user: {
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export interface IDauthUser {
|
|
2
|
+
_id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
lastname: string;
|
|
5
|
+
nickname: string;
|
|
6
|
+
email: string;
|
|
7
|
+
isVerified: boolean;
|
|
8
|
+
language: string;
|
|
9
|
+
avatar: {
|
|
10
|
+
id: string;
|
|
11
|
+
url: string;
|
|
12
|
+
};
|
|
13
|
+
role: string;
|
|
14
|
+
telPrefix: string;
|
|
15
|
+
telSuffix: string;
|
|
16
|
+
createdAt: Date;
|
|
17
|
+
updatedAt: Date;
|
|
18
|
+
lastLogin: Date;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IDauthDomainState {
|
|
22
|
+
name: string;
|
|
23
|
+
loginRedirect: string;
|
|
24
|
+
allowedOrigins: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface IDauthState {
|
|
28
|
+
user: IDauthUser;
|
|
29
|
+
domain: IDauthDomainState;
|
|
30
|
+
isLoading: boolean;
|
|
31
|
+
isAuthenticated: boolean;
|
|
32
|
+
loginWithRedirect: () => void;
|
|
33
|
+
logout: () => void;
|
|
34
|
+
getAccessToken: () => Promise<string>;
|
|
35
|
+
updateUser: ({
|
|
36
|
+
name,
|
|
37
|
+
lastname,
|
|
38
|
+
nickname,
|
|
39
|
+
telPrefix,
|
|
40
|
+
telSuffix,
|
|
41
|
+
language,
|
|
42
|
+
avatar,
|
|
43
|
+
}: Partial<IDauthUser>) => Promise<boolean>;
|
|
44
|
+
updateUserWithRedirect: () => void;
|
|
45
|
+
// Send email verification
|
|
46
|
+
sendEmailVerificationStatus: {
|
|
47
|
+
status: IActionStatus;
|
|
48
|
+
isLoading: boolean;
|
|
49
|
+
};
|
|
50
|
+
sendEmailVerification: () => Promise<boolean>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface IActionStatus {
|
|
54
|
+
type: TStatusTypes;
|
|
55
|
+
message: string;
|
|
56
|
+
}
|
|
57
|
+
export type TStatusTypes = 'success' | 'error' | 'info' | 'warning';
|
|
@@ -6,8 +6,8 @@ import {
|
|
|
6
6
|
} from '../api/dauth.api';
|
|
7
7
|
import { getClientBasePath } from '../api/utils/config';
|
|
8
8
|
import { TOKEN_LS } from '../constants';
|
|
9
|
-
import { IDauthDomainState, IDauthUser } from '../
|
|
10
|
-
import { routes } from '../routes';
|
|
9
|
+
import { IDauthDomainState, IDauthUser } from '../interfaces';
|
|
10
|
+
import { routes } from '../api/utils/routes';
|
|
11
11
|
import * as DauthTypes from './dauth.types';
|
|
12
12
|
|
|
13
13
|
type TSetDauthStateAction = {
|