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.
@@ -1,47 +1,3 @@
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
+ 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,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { IDauthUser } from '../initialDauthState';
2
+ import { IDauthUser } from '../interfaces';
3
3
  declare type TSetDauthStateAction = {
4
4
  dispatch: React.Dispatch<any>;
5
5
  token: string;
@@ -1,2 +1,2 @@
1
- import { IDauthState } from '../initialDauthState';
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.108",
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
  }
@@ -1,6 +1,6 @@
1
- import { IDauthUser } from '../initialDauthState';
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
+ };
@@ -4,4 +4,5 @@ export const routes = {
4
4
  tenantGetUser: 't-get-user',
5
5
  tenantResendEmailVerification: 't-resend-email-verification',
6
6
  tenantRefreshAccessToken: 't-refresh-access-token',
7
+ tenantVerifyToken: "t-verify-token",
7
8
  };
package/src/index.tsx CHANGED
@@ -6,13 +6,14 @@ import React, {
6
6
  createContext,
7
7
  useContext,
8
8
  } from 'react';
9
- import initialDauthState, { IDauthUser } from './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 JWT from 'expo-jwt';
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
- action.checkTokenAction({ dispatch, domainName, token: token_ls });
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
- const queryString = window.location.search;
43
- if (!queryString) return;
44
- const urlParams = new URLSearchParams(queryString);
45
- const token_url = urlParams.get(TOKEN_LS);
46
- if (token_url && !dauthState.isAuthenticated) {
47
- action.setDauthStateAction({ dispatch, token: token_url, domainName });
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
- if (dauthState.isAuthenticated) return;
54
- const token_ls = localStorage.getItem(TOKEN_LS);
55
- if (!token_ls) return;
56
-
57
- try {
58
- const verify = JWT.decode(token_ls, ask as string);
59
- console.log({ verify });
60
- } catch (error) {
61
- console.log(error);
62
- }
63
-
64
- if (token_ls && !dauthState.isAuthenticated) {
65
- action.setAutoLoginAction({
66
- dispatch,
67
- dauth_state_ls: token_ls,
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(() => {
@@ -1,60 +1,4 @@
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';
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 '../initialDauthState';
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 = {
@@ -1,4 +1,4 @@
1
- import { IDauthState } from '../initialDauthState';
1
+ import { IDauthState } from '../interfaces';
2
2
  import * as DauthTypes from './dauth.types';
3
3
 
4
4
  export default function userReducer(state: IDauthState, action: any) {