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.
- package/README.md +55 -148
- package/dist/dauth-context-react.cjs.development.js +14 -48
- 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 +12 -48
- package/dist/dauth-context-react.esm.js.map +1 -1
- package/dist/index.d.ts +3 -28
- package/dist/initialDauthState.d.ts +9 -7
- package/dist/reducer/dauth.actions.d.ts +7 -4
- package/package.json +61 -1
- package/src/api/dauth.api.ts +17 -10
- package/src/api/utils/config.ts +15 -15
- package/src/constants.ts +1 -1
- package/src/index.tsx +69 -64
- package/src/initialDauthState.ts +35 -56
- package/src/reducer/dauth.actions.ts +54 -16
- package/src/reducer/dauth.reducer.ts +19 -19
- package/src/reducer/dauth.types.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import React, { useReducer, useEffect, useCallback, useMemo, createContext, useContext } from 'react';
|
|
2
2
|
|
|
3
3
|
function _regeneratorRuntime() {
|
|
4
4
|
_regeneratorRuntime = function () {
|
|
@@ -346,42 +346,9 @@ function _extends() {
|
|
|
346
346
|
return _extends.apply(this, arguments);
|
|
347
347
|
}
|
|
348
348
|
|
|
349
|
-
// export interface IDauthDomain {
|
|
350
|
-
// name: string
|
|
351
|
-
// loginRedirect: string
|
|
352
|
-
// allowedOrigins: string[]
|
|
353
|
-
// }
|
|
354
|
-
// export interface IDauthState {
|
|
355
|
-
// user: IDauthUser
|
|
356
|
-
// domain: IDauthDomain
|
|
357
|
-
// isLoading: boolean
|
|
358
|
-
// isAuthenticated: boolean
|
|
359
|
-
// loginWithRedirect: () => void
|
|
360
|
-
// logout: () => void
|
|
361
|
-
// getAccessToken: () => void
|
|
362
|
-
// }
|
|
363
|
-
// const userState = {
|
|
364
|
-
// _id: "",
|
|
365
|
-
// ssid: "",
|
|
366
|
-
// name: "",
|
|
367
|
-
// lastname: "",
|
|
368
|
-
// nickname: "",
|
|
369
|
-
// email: "",
|
|
370
|
-
// is_verified: false,
|
|
371
|
-
// language: "",
|
|
372
|
-
// avatar: "",
|
|
373
|
-
// createdAt: new Date(),
|
|
374
|
-
// updatedAt: new Date(),
|
|
375
|
-
// last_login: new Date(),
|
|
376
|
-
// }
|
|
377
|
-
var domainState = {
|
|
378
|
-
name: "",
|
|
379
|
-
loginRedirect: "",
|
|
380
|
-
allowedOrigins: [""]
|
|
381
|
-
};
|
|
382
349
|
var initialDauthState = {
|
|
383
350
|
user: {},
|
|
384
|
-
domain:
|
|
351
|
+
domain: {},
|
|
385
352
|
isLoading: true,
|
|
386
353
|
isAuthenticated: false,
|
|
387
354
|
loginWithRedirect: function loginWithRedirect() {},
|
|
@@ -411,9 +378,9 @@ function userReducer(state, action) {
|
|
|
411
378
|
}
|
|
412
379
|
}
|
|
413
380
|
|
|
414
|
-
var isLocalhost = /*#__PURE__*/Boolean(window.location.hostname ===
|
|
415
|
-
var apiVersion =
|
|
416
|
-
var serverDomain =
|
|
381
|
+
var isLocalhost = /*#__PURE__*/Boolean(window.location.hostname === 'localhost' || window.location.hostname === '[::1]' || /*#__PURE__*/window.location.hostname.match(/(192)\.(168)\.(1)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm) || /*#__PURE__*/window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
|
|
382
|
+
var apiVersion = 'v1';
|
|
383
|
+
var serverDomain = 'dauth.ovh';
|
|
417
384
|
function getServerBasePath(_ref) {
|
|
418
385
|
var domainName = _ref.domainName;
|
|
419
386
|
var serverPort = 4012;
|
|
@@ -438,10 +405,10 @@ var getTenantUserAPI = /*#__PURE__*/function () {
|
|
|
438
405
|
while (1) switch (_context.prev = _context.next) {
|
|
439
406
|
case 0:
|
|
440
407
|
params = {
|
|
441
|
-
method:
|
|
408
|
+
method: 'GET',
|
|
442
409
|
headers: {
|
|
443
410
|
Authorization: "" + token,
|
|
444
|
-
|
|
411
|
+
'Content-Type': 'application/json'
|
|
445
412
|
}
|
|
446
413
|
};
|
|
447
414
|
_context.next = 3;
|
|
@@ -631,18 +598,15 @@ function _setLogoutAction() {
|
|
|
631
598
|
return _setLogoutAction.apply(this, arguments);
|
|
632
599
|
}
|
|
633
600
|
|
|
634
|
-
// Delete me
|
|
635
|
-
var Thing = function Thing() {
|
|
636
|
-
return createElement("div", null, "the snozzberries taste like snozzberries");
|
|
637
|
-
};
|
|
638
601
|
var DauthProvider = function DauthProvider(props) {
|
|
639
602
|
var domainName = props.domainName,
|
|
640
603
|
sid = props.sid,
|
|
641
604
|
ssid = props.ssid,
|
|
642
605
|
children = props.children;
|
|
643
606
|
var _useReducer = useReducer(userReducer, initialDauthState),
|
|
644
|
-
|
|
607
|
+
ds = _useReducer[0],
|
|
645
608
|
dispatch = _useReducer[1];
|
|
609
|
+
var dauthState = ds;
|
|
646
610
|
// Catch login redirect
|
|
647
611
|
useEffect(function () {
|
|
648
612
|
var queryString = window.location.search;
|
|
@@ -686,12 +650,12 @@ var DauthProvider = function DauthProvider(props) {
|
|
|
686
650
|
logout: logout
|
|
687
651
|
});
|
|
688
652
|
}, [dauthState, loginWithRedirect, logout]);
|
|
689
|
-
return createElement(DauthContext.Provider, {
|
|
653
|
+
return React.createElement(DauthContext.Provider, {
|
|
690
654
|
value: memoProvider
|
|
691
655
|
}, children);
|
|
692
656
|
};
|
|
693
657
|
var DauthContext = /*#__PURE__*/createContext(initialDauthState);
|
|
694
|
-
var
|
|
658
|
+
var useDauth = function useDauth() {
|
|
695
659
|
var context = useContext(DauthContext);
|
|
696
660
|
if (!context) {
|
|
697
661
|
throw new Error('useMyContext debe ser utilizado dentro de un MyContextProvider');
|
|
@@ -699,5 +663,5 @@ var useDauthContext = function useDauthContext() {
|
|
|
699
663
|
return context;
|
|
700
664
|
};
|
|
701
665
|
|
|
702
|
-
export { DauthProvider,
|
|
666
|
+
export { DauthProvider, useDauth };
|
|
703
667
|
//# sourceMappingURL=dauth-context-react.esm.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dauth-context-react.esm.js","sources":["../src/initialDauthState.ts","../src/reducer/dauth.types.ts","../src/reducer/dauth.reducer.ts","../src/api/utils/config.ts","../src/api/dauth.api.ts","../src/constants.ts","../src/reducer/dauth.actions.ts","../src/index.tsx"],"sourcesContent":["export interface IDauthUser {\n\t_id: string\n\tssid: string\n\tname: string\n\tlastname: string\n\tnickname: string\n\temail: string\n\tis_verified: boolean\n\tlanguage: string\n\tavatar: string\n\tcreatedAt: Date\n\tupdatedAt: Date\n\tlast_login: Date\n}\n\n// export interface IDauthDomain {\n// \tname: string\n// \tloginRedirect: string\n// \tallowedOrigins: string[]\n// }\n\n// export interface IDauthState {\n// \tuser: IDauthUser\n// \tdomain: IDauthDomain\n// \tisLoading: boolean\n// \tisAuthenticated: boolean\n// \tloginWithRedirect: () => void\n// \tlogout: () => void\n// \tgetAccessToken: () => void\n// }\n\n// const userState = {\n// \t_id: \"\",\n// \tssid: \"\",\n// \tname: \"\",\n// \tlastname: \"\",\n// \tnickname: \"\",\n// \temail: \"\",\n// \tis_verified: false,\n// \tlanguage: \"\",\n// \tavatar: \"\",\n// \tcreatedAt: new Date(),\n// \tupdatedAt: new Date(),\n// \tlast_login: new Date(),\n// }\n\nconst domainState = {\n\tname: \"\",\n\tloginRedirect: \"\",\n\tallowedOrigins: [\"\"],\n}\n\nconst initialDauthState = {\n\tuser: {} as IDauthUser,\n\tdomain: domainState,\n\tisLoading: true,\n\tisAuthenticated: false,\n\tloginWithRedirect: () => { },\n\tlogout: () => { },\n\tgetAccessToken: () => { },\n}\n\nexport default initialDauthState","export const LOGIN = 'LOGIN';\nexport const SET_IS_LOADING = 'SET_IS_LOADING';","import * as DauthTypes from './dauth.types'\n\nexport default function userReducer(state: any, action: any) {\n\tconst { type, payload } = action;\n\n\tswitch (type) {\n\t\tcase DauthTypes.LOGIN:\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\tuser: payload.user,\n\t\t\t\tdomain: payload.domain,\n\t\t\t\tisAuthenticated: payload.isAuthenticated,\n\t\t\t}\n\n\t\tcase DauthTypes.SET_IS_LOADING:\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\tisLoading: payload.isLoading,\n\t\t\t}\n\n\t\tdefault:\n\t\t\treturn state\n\t}\n}","const isLocalhost = Boolean(\n window.location.hostname === \"localhost\" ||\n window.location.hostname === \"[::1]\" ||\n window.location.hostname.match(\n /(192)\\.(168)\\.(1)\\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm\n ) ||\n window.location.hostname.match(\n /^127(?:\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/\n )\n);\nexport const apiVersion = \"v1\";\nexport const serverDomain = \"dauth.ovh\";\n\nexport function getServerBasePath({ domainName }: { domainName: string }) {\n const serverPort = 4012;\n const serverLocalUrl = `${window.location.protocol}//${window.location.hostname}:${serverPort}/api/${apiVersion}`\n const serverProdUrl = `https://${domainName}.${serverDomain}/api/${apiVersion}`\n const serverBasePath = isLocalhost ? serverLocalUrl : serverProdUrl;\n return serverBasePath;\n}\n\nexport function getClientBasePath({ domainName }: { domainName: string }) {\n const clientPort = 5185;\n const clientLocalUrl = `${window.location.protocol}//${window.location.hostname}:${clientPort}`\n const clientProdUrl = `https://${domainName}.${serverDomain}`\n const clientBasePath = isLocalhost ? clientLocalUrl : clientProdUrl;\n return clientBasePath;\n}","import { getServerBasePath } from \"./utils/config\";\n\nexport const getTenantUserAPI = async (domainName: string, ssid: string, token: string): Promise<any> => {\n const params = {\n method: \"GET\",\n headers: {\n Authorization: `${token}`,\n \"Content-Type\": \"application/json\",\n }\n }\n const response = await fetch(`${getServerBasePath({ domainName })}/get-tenant-user/${ssid}`, params)\n const data = await response.json()\n return { response, data }\n}","export const DAUTH_STATE = 'dauth_state';","import { getTenantUserAPI } from '../api/dauth.api';\nimport { DAUTH_STATE } from '../constants';\nimport * as DauthTypes from './dauth.types'\n\nexport async function setDauthStateAction({ dispatch, dauth_state, domainName, ssid }: { dispatch: any, dauth_state: string, domainName: string, ssid: string }) {\n dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } })\n try {\n const getUserFetch = await getTenantUserAPI(domainName, ssid, dauth_state);\n if (getUserFetch.response.status === 200) {\n dispatch({\n type: DauthTypes.LOGIN,\n payload: {\n user: getUserFetch.data.user,\n domain: getUserFetch.data.domain,\n isAuthenticated: true,\n },\n })\n window.history.replaceState({}, document.title, getUserFetch.data.domain.loginRedirect);\n return localStorage.setItem(DAUTH_STATE, dauth_state);\n } else {\n return localStorage.removeItem(DAUTH_STATE);\n }\n } catch (error) {\n localStorage.removeItem(DAUTH_STATE);\n console.log(error);\n } finally {\n dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: false } })\n }\n}\n\nexport async function setAutoLoginAction({ dispatch, dauth_state_ls, domainName, ssid }: { dispatch: any, dauth_state_ls: string, domainName: string, ssid: string }) {\n dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } })\n try {\n const getUserFetch = await getTenantUserAPI(domainName, ssid, dauth_state_ls);\n if (getUserFetch.response.status === 200) {\n dispatch({\n type: DauthTypes.LOGIN,\n payload: {\n user: getUserFetch.data.user,\n domain: getUserFetch.data.domain,\n isAuthenticated: true,\n },\n })\n localStorage.setItem(DAUTH_STATE, dauth_state_ls);\n } else {\n localStorage.removeItem(DAUTH_STATE);\n }\n\n } catch (error) {\n localStorage.removeItem(DAUTH_STATE);\n console.log(error);\n } finally {\n dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: false } })\n }\n}\n\nexport async function setLogoutAction({ dispatch }: { dispatch: any }) {\n dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } })\n dispatch({\n type: DauthTypes.LOGIN,\n payload: {\n user: {},\n domain: {},\n isAuthenticated: false,\n },\n })\n localStorage.removeItem(DAUTH_STATE);\n return dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: false } })\n}","import * as React from 'react';\nimport { useReducer, useMemo, useEffect, useCallback } from 'react'\nimport initialDauthState from './initialDauthState'\nimport userReducer from './reducer/dauth.reducer'\nimport * as action from \"./reducer/dauth.actions\"\nimport { getClientBasePath } from './api/utils/config'\nimport { DAUTH_STATE } from './constants'\n\n// Delete me\nexport const Thing = () => {\n return <div>the snozzberries taste like snozzberries</div>;\n};\n\ninterface DauthProviderProps {\n domainName: string\n sid: string\n ssid: string\n children: React.ReactNode\n}\n\nexport const DauthProvider: React.FC<DauthProviderProps> = (props: DauthProviderProps) => {\n\tconst { domainName, sid, ssid, children } = props\n\tconst [dauthState, dispatch] = useReducer(userReducer, initialDauthState)\n\n\t// Catch login redirect\n\tuseEffect(() => {\n\t\tconst queryString = window.location.search;\n\t\tif (!queryString) return\n\t\tconst urlParams = new URLSearchParams(queryString);\n\t\tconst dauth_state = urlParams.get(DAUTH_STATE);\n\t\tif (dauth_state && !dauthState.isAuthenticated) {\n\t\t\taction.setDauthStateAction({ dispatch, dauth_state, domainName, ssid })\n\t\t}\n\t}, [dauthState.isAuthenticated, domainName, ssid])\n\n\t// Auto Login\n\tuseEffect(() => {\n\t\tconst dauth_state_ls = localStorage.getItem(DAUTH_STATE);\n\t\tif (dauth_state_ls && !dauthState.isAuthenticated) {\n\t\t\taction.setAutoLoginAction({ dispatch, dauth_state_ls, domainName, ssid })\n\t\t}\n\t}, [dauthState.isAuthenticated, domainName, ssid])\n\n\tconst loginWithRedirect = useCallback(() => {\n\t\treturn window.location.replace(`${getClientBasePath({ domainName })}/t-sign/${sid}`);\n\t}, [domainName, sid])\n\n\tconst logout = useCallback(() => {\n\t\treturn action.setLogoutAction({ dispatch })\n\t}, [])\n\n\tconst memoProvider = useMemo(\n\t\t() => ({\n\t\t\t...dauthState,\n\t\t\tloginWithRedirect,\n\t\t\tlogout\n\t\t}), [\n\t\tdauthState,\n\t\tloginWithRedirect,\n\t\tlogout\n\t])\n\n\n\treturn (\n\t\t<DauthContext.Provider value={memoProvider}>\n\t\t\t{children}\n\t\t</DauthContext.Provider>\n\t)\n}\n\nconst DauthContext = React.createContext(initialDauthState);\n\nexport const useDauthContext = () => {\n const context = React.useContext(DauthContext);\n if (!context) {\n throw new Error('useMyContext debe ser utilizado dentro de un MyContextProvider');\n }\n return context;\n};"],"names":["domainState","name","loginRedirect","allowedOrigins","initialDauthState","user","domain","isLoading","isAuthenticated","loginWithRedirect","logout","getAccessToken","LOGIN","SET_IS_LOADING","userReducer","state","action","type","payload","DauthTypes","_extends","isLocalhost","Boolean","window","location","hostname","match","apiVersion","serverDomain","getServerBasePath","_ref","domainName","serverPort","serverLocalUrl","protocol","serverProdUrl","serverBasePath","getClientBasePath","_ref2","clientPort","clientLocalUrl","clientProdUrl","clientBasePath","getTenantUserAPI","_asyncToGenerator","_regeneratorRuntime","mark","_callee","ssid","token","params","response","data","wrap","_callee$","_context","prev","next","method","headers","Authorization","fetch","sent","json","abrupt","stop","_x","_x2","_x3","apply","arguments","DAUTH_STATE","setDauthStateAction","_setDauthStateAction","dispatch","dauth_state","getUserFetch","status","history","replaceState","document","title","localStorage","setItem","removeItem","t0","console","log","finish","setAutoLoginAction","_setAutoLoginAction","_callee2","dauth_state_ls","_callee2$","_context2","setLogoutAction","_setLogoutAction","_callee3","_ref3","_callee3$","_context3","Thing","React","DauthProvider","props","sid","children","_useReducer","useReducer","dauthState","useEffect","queryString","search","urlParams","URLSearchParams","get","getItem","useCallback","replace","memoProvider","useMemo","DauthContext","Provider","value","useDauthContext","context","Error"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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iB,EAAjBA,iBAAiB;MACjBC,MAAM,EAANA;;GACC,EAAE,CACJmG,UAAU,EACVpG,iBAAiB,EACjBC,MAAM,CACN,CAAC;EAGF,OACC4F,cAACmB,YAAY,CAACC,QAAQ;IAACC,KAAK,EAAEJ;KAC5Bb,QAAQ,CACc;AAE1B,CAAC;AAED,IAAMe,YAAY,gBAAGnB,aAAmB,CAAClG,iBAAiB,CAAC;AAE3D,IAAawH,eAAe,GAAG,SAAlBA,eAAeA;EAC1B,IAAMC,OAAO,GAAGvB,UAAgB,CAACmB,YAAY,CAAC;EAC9C,IAAI,CAACI,OAAO,EAAE;IACZ,MAAM,IAAIC,KAAK,CAAC,gEAAgE,CAAC;;EAEnF,OAAOD,OAAO;AAChB,CAAC;;;;"}
|
|
1
|
+
{"version":3,"file":"dauth-context-react.esm.js","sources":["../src/initialDauthState.ts","../src/reducer/dauth.types.ts","../src/reducer/dauth.reducer.ts","../src/api/utils/config.ts","../src/api/dauth.api.ts","../src/constants.ts","../src/reducer/dauth.actions.ts","../src/index.tsx"],"sourcesContent":["export interface IDauthUser {\n _id: string;\n ssid: string;\n name: string;\n lastname: string;\n nickname: string;\n email: string;\n is_verified: boolean;\n language: string;\n avatar: string;\n createdAt: Date;\n updatedAt: Date;\n last_login: Date;\n}\n\nexport interface IDauthDomain {\n name: string;\n loginRedirect: string;\n allowedOrigins: string[];\n}\n\nexport interface IDauthState {\n user: IDauthUser;\n domain: IDauthDomain;\n isLoading: boolean;\n isAuthenticated: boolean;\n loginWithRedirect: () => void;\n logout: () => void;\n getAccessToken: () => void;\n}\n\nconst initialDauthState: IDauthState = {\n user: {} as IDauthUser,\n domain: {} as IDauthDomain,\n isLoading: true,\n isAuthenticated: false,\n loginWithRedirect: () => {},\n logout: () => {},\n getAccessToken: () => {},\n};\n\nexport default initialDauthState;\n","export const LOGIN = 'LOGIN';\nexport const SET_IS_LOADING = 'SET_IS_LOADING';\n","import * as DauthTypes from './dauth.types';\n\nexport default function userReducer(state: any, action: any) {\n const { type, payload } = action;\n\n switch (type) {\n case DauthTypes.LOGIN:\n return {\n ...state,\n user: payload.user,\n domain: payload.domain,\n isAuthenticated: payload.isAuthenticated,\n };\n\n case DauthTypes.SET_IS_LOADING:\n return {\n ...state,\n isLoading: payload.isLoading,\n };\n\n default:\n return state;\n }\n}\n","const isLocalhost = Boolean(\n window.location.hostname === 'localhost' ||\n window.location.hostname === '[::1]' ||\n window.location.hostname.match(\n /(192)\\.(168)\\.(1)\\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm\n ) ||\n window.location.hostname.match(\n /^127(?:\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/\n )\n);\nexport const apiVersion = 'v1';\nexport const serverDomain = 'dauth.ovh';\n\nexport function getServerBasePath({ domainName }: { domainName: string }) {\n const serverPort = 4012;\n const serverLocalUrl = `${window.location.protocol}//${window.location.hostname}:${serverPort}/api/${apiVersion}`;\n const serverProdUrl = `https://${domainName}.${serverDomain}/api/${apiVersion}`;\n const serverBasePath = isLocalhost ? serverLocalUrl : serverProdUrl;\n return serverBasePath;\n}\n\nexport function getClientBasePath({ domainName }: { domainName: string }) {\n const clientPort = 5185;\n const clientLocalUrl = `${window.location.protocol}//${window.location.hostname}:${clientPort}`;\n const clientProdUrl = `https://${domainName}.${serverDomain}`;\n const clientBasePath = isLocalhost ? clientLocalUrl : clientProdUrl;\n return clientBasePath;\n}\n","import { getServerBasePath } from './utils/config';\n\nexport const getTenantUserAPI = async (\n domainName: string,\n ssid: string,\n token: string\n): Promise<any> => {\n const params = {\n method: 'GET',\n headers: {\n Authorization: `${token}`,\n 'Content-Type': 'application/json',\n },\n };\n const response = await fetch(\n `${getServerBasePath({ domainName })}/get-tenant-user/${ssid}`,\n params\n );\n const data = await response.json();\n return { response, data };\n};\n","export const DAUTH_STATE = 'dauth_state';\n","import { getTenantUserAPI } from '../api/dauth.api';\nimport { DAUTH_STATE } from '../constants';\nimport * as DauthTypes from './dauth.types';\n\ntype TSetDauthStateAction = {\n dispatch: any;\n dauth_state: string;\n domainName: string;\n ssid: string;\n};\nexport async function setDauthStateAction({\n dispatch,\n dauth_state,\n domainName,\n ssid,\n}: TSetDauthStateAction) {\n dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });\n try {\n const getUserFetch = await getTenantUserAPI(domainName, ssid, dauth_state);\n if (getUserFetch.response.status === 200) {\n dispatch({\n type: DauthTypes.LOGIN,\n payload: {\n user: getUserFetch.data.user,\n domain: getUserFetch.data.domain,\n isAuthenticated: true,\n },\n });\n window.history.replaceState(\n {},\n document.title,\n getUserFetch.data.domain.loginRedirect\n );\n return localStorage.setItem(DAUTH_STATE, dauth_state);\n } else {\n return localStorage.removeItem(DAUTH_STATE);\n }\n } catch (error) {\n localStorage.removeItem(DAUTH_STATE);\n console.log(error);\n } finally {\n dispatch({\n type: DauthTypes.SET_IS_LOADING,\n payload: { isLoading: false },\n });\n }\n}\n\ntype TSetAutoLoginAction = {\n dispatch: any;\n dauth_state_ls: string;\n domainName: string;\n ssid: string;\n};\nexport async function setAutoLoginAction({\n dispatch,\n dauth_state_ls,\n domainName,\n ssid,\n}: TSetAutoLoginAction) {\n dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });\n try {\n const getUserFetch = await getTenantUserAPI(\n domainName,\n ssid,\n dauth_state_ls\n );\n if (getUserFetch.response.status === 200) {\n dispatch({\n type: DauthTypes.LOGIN,\n payload: {\n user: getUserFetch.data.user,\n domain: getUserFetch.data.domain,\n isAuthenticated: true,\n },\n });\n localStorage.setItem(DAUTH_STATE, dauth_state_ls);\n } else {\n localStorage.removeItem(DAUTH_STATE);\n }\n } catch (error) {\n localStorage.removeItem(DAUTH_STATE);\n console.log(error);\n } finally {\n dispatch({\n type: DauthTypes.SET_IS_LOADING,\n payload: { isLoading: false },\n });\n }\n}\n\nexport async function setLogoutAction({ dispatch }: { dispatch: any }) {\n dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });\n dispatch({\n type: DauthTypes.LOGIN,\n payload: {\n user: {},\n domain: {},\n isAuthenticated: false,\n },\n });\n localStorage.removeItem(DAUTH_STATE);\n return dispatch({\n type: DauthTypes.SET_IS_LOADING,\n payload: { isLoading: false },\n });\n}\n","import React, {\n useReducer,\n useMemo,\n useEffect,\n useCallback,\n createContext,\n useContext,\n} from 'react';\nimport initialDauthState, { IDauthState } from './initialDauthState';\nimport userReducer from './reducer/dauth.reducer';\nimport * as action from './reducer/dauth.actions';\nimport { getClientBasePath } from './api/utils/config';\nimport { DAUTH_STATE } from './constants';\n\ninterface DauthProviderProps {\n domainName: string;\n sid: string;\n ssid: string;\n children: React.ReactNode;\n}\n\nexport const DauthProvider: React.FC<DauthProviderProps> = (\n props: DauthProviderProps\n) => {\n const { domainName, sid, ssid, children } = props;\n const [ds, dispatch] = useReducer(userReducer, initialDauthState);\n const dauthState = ds as IDauthState;\n\n // Catch login redirect\n useEffect(() => {\n const queryString = window.location.search;\n if (!queryString) return;\n const urlParams = new URLSearchParams(queryString);\n const dauth_state = urlParams.get(DAUTH_STATE);\n if (dauth_state && !dauthState.isAuthenticated) {\n action.setDauthStateAction({ dispatch, dauth_state, domainName, ssid });\n }\n }, [dauthState.isAuthenticated, domainName, ssid]);\n\n // Auto Login\n useEffect(() => {\n const dauth_state_ls = localStorage.getItem(DAUTH_STATE);\n if (dauth_state_ls && !dauthState.isAuthenticated) {\n action.setAutoLoginAction({ dispatch, dauth_state_ls, domainName, ssid });\n }\n }, [dauthState.isAuthenticated, domainName, ssid]);\n\n const loginWithRedirect = useCallback(() => {\n return window.location.replace(\n `${getClientBasePath({ domainName })}/t-sign/${sid}`\n );\n }, [domainName, sid]);\n\n const logout = useCallback(() => {\n return action.setLogoutAction({ dispatch });\n }, []);\n\n const memoProvider = useMemo(\n () => ({\n ...dauthState,\n loginWithRedirect,\n logout,\n }),\n [dauthState, loginWithRedirect, logout]\n );\n\n return (\n <DauthContext.Provider value={memoProvider}>\n {children}\n </DauthContext.Provider>\n );\n};\n\nconst DauthContext = createContext(initialDauthState);\n\nexport const useDauth = () => {\n const context = useContext(DauthContext);\n if (!context) {\n throw new Error(\n 'useMyContext debe ser utilizado dentro de un MyContextProvider'\n );\n }\n return context;\n};\n"],"names":["initialDauthState","user","domain","isLoading","isAuthenticated","loginWithRedirect","logout","getAccessToken","LOGIN","SET_IS_LOADING","userReducer","state","action","type","payload","DauthTypes","_extends","isLocalhost","Boolean","window","location","hostname","match","apiVersion","serverDomain","getServerBasePath","_ref","domainName","serverPort","serverLocalUrl","protocol","serverProdUrl","serverBasePath","getClientBasePath","_ref2","clientPort","clientLocalUrl","clientProdUrl","clientBasePath","getTenantUserAPI","_asyncToGenerator","_regeneratorRuntime","mark","_callee","ssid","token","params","response","data","wrap","_callee$","_context","prev","next","method","headers","Authorization","fetch","sent","json","abrupt","stop","_x","_x2","_x3","apply","arguments","DAUTH_STATE","setDauthStateAction","_setDauthStateAction","dispatch","dauth_state","getUserFetch","status","history","replaceState","document","title","loginRedirect","localStorage","setItem","removeItem","t0","console","log","finish","setAutoLoginAction","_setAutoLoginAction","_callee2","dauth_state_ls","_callee2$","_context2","setLogoutAction","_setLogoutAction","_callee3","_ref3","_callee3$","_context3","DauthProvider","props","sid","children","_useReducer","useReducer","ds","dauthState","useEffect","queryString","search","urlParams","URLSearchParams","get","getItem","useCallback","replace","memoProvider","useMemo","React","DauthContext","Provider","value","createContext","useDauth","context","useContext","Error"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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ED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { IDauthState } from './initialDauthState';
|
|
3
3
|
interface DauthProviderProps {
|
|
4
4
|
domainName: string;
|
|
5
5
|
sid: string;
|
|
@@ -7,30 +7,5 @@ interface DauthProviderProps {
|
|
|
7
7
|
children: React.ReactNode;
|
|
8
8
|
}
|
|
9
9
|
export declare const DauthProvider: React.FC<DauthProviderProps>;
|
|
10
|
-
export declare const
|
|
11
|
-
user: {
|
|
12
|
-
_id: string;
|
|
13
|
-
ssid: string;
|
|
14
|
-
name: string;
|
|
15
|
-
lastname: string;
|
|
16
|
-
nickname: string;
|
|
17
|
-
email: string;
|
|
18
|
-
is_verified: boolean;
|
|
19
|
-
language: string;
|
|
20
|
-
avatar: string;
|
|
21
|
-
createdAt: Date;
|
|
22
|
-
updatedAt: Date;
|
|
23
|
-
last_login: Date;
|
|
24
|
-
};
|
|
25
|
-
domain: {
|
|
26
|
-
name: string;
|
|
27
|
-
loginRedirect: string;
|
|
28
|
-
allowedOrigins: string[];
|
|
29
|
-
};
|
|
30
|
-
isLoading: boolean;
|
|
31
|
-
isAuthenticated: boolean;
|
|
32
|
-
loginWithRedirect: () => void;
|
|
33
|
-
logout: () => void;
|
|
34
|
-
getAccessToken: () => void;
|
|
35
|
-
};
|
|
10
|
+
export declare const useDauth: () => IDauthState;
|
|
36
11
|
export {};
|
|
@@ -12,17 +12,19 @@ export interface IDauthUser {
|
|
|
12
12
|
updatedAt: Date;
|
|
13
13
|
last_login: Date;
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
export interface IDauthDomain {
|
|
16
|
+
name: string;
|
|
17
|
+
loginRedirect: string;
|
|
18
|
+
allowedOrigins: string[];
|
|
19
|
+
}
|
|
20
|
+
export interface IDauthState {
|
|
16
21
|
user: IDauthUser;
|
|
17
|
-
domain:
|
|
18
|
-
name: string;
|
|
19
|
-
loginRedirect: string;
|
|
20
|
-
allowedOrigins: string[];
|
|
21
|
-
};
|
|
22
|
+
domain: IDauthDomain;
|
|
22
23
|
isLoading: boolean;
|
|
23
24
|
isAuthenticated: boolean;
|
|
24
25
|
loginWithRedirect: () => void;
|
|
25
26
|
logout: () => void;
|
|
26
27
|
getAccessToken: () => void;
|
|
27
|
-
}
|
|
28
|
+
}
|
|
29
|
+
declare const initialDauthState: IDauthState;
|
|
28
30
|
export default initialDauthState;
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
declare type TSetDauthStateAction = {
|
|
2
2
|
dispatch: any;
|
|
3
3
|
dauth_state: string;
|
|
4
4
|
domainName: string;
|
|
5
5
|
ssid: string;
|
|
6
|
-
}
|
|
7
|
-
export declare function
|
|
6
|
+
};
|
|
7
|
+
export declare function setDauthStateAction({ dispatch, dauth_state, domainName, ssid, }: TSetDauthStateAction): Promise<void>;
|
|
8
|
+
declare type TSetAutoLoginAction = {
|
|
8
9
|
dispatch: any;
|
|
9
10
|
dauth_state_ls: string;
|
|
10
11
|
domainName: string;
|
|
11
12
|
ssid: string;
|
|
12
|
-
}
|
|
13
|
+
};
|
|
14
|
+
export declare function setAutoLoginAction({ dispatch, dauth_state_ls, domainName, ssid, }: TSetAutoLoginAction): Promise<void>;
|
|
13
15
|
export declare function setLogoutAction({ dispatch }: {
|
|
14
16
|
dispatch: any;
|
|
15
17
|
}): Promise<any>;
|
|
18
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1 +1,61 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": "0.1.4",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"typings": "dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=10"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"start": "tsdx watch",
|
|
15
|
+
"build": "tsdx build",
|
|
16
|
+
"test": "tsdx test --passWithNoTests",
|
|
17
|
+
"lint": "tsdx lint",
|
|
18
|
+
"prepare": "tsdx build",
|
|
19
|
+
"size": "size-limit",
|
|
20
|
+
"analyze": "size-limit --why"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": ">=16"
|
|
24
|
+
},
|
|
25
|
+
"husky": {
|
|
26
|
+
"hooks": {
|
|
27
|
+
"pre-commit": "tsdx lint"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"prettier": {
|
|
31
|
+
"printWidth": 80,
|
|
32
|
+
"semi": true,
|
|
33
|
+
"singleQuote": true,
|
|
34
|
+
"trailingComma": "es5"
|
|
35
|
+
},
|
|
36
|
+
"name": "dauth-context-react",
|
|
37
|
+
"author": "David T. Pizarro Frick",
|
|
38
|
+
"module": "dist/dauth-context-react.esm.js",
|
|
39
|
+
"size-limit": [
|
|
40
|
+
{
|
|
41
|
+
"path": "dist/dauth-context-react.cjs.production.min.js",
|
|
42
|
+
"limit": "10 KB"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"path": "dist/dauth-context-react.esm.js",
|
|
46
|
+
"limit": "10 KB"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@size-limit/preset-small-lib": "^11.0.2",
|
|
51
|
+
"@types/react": "^18.2.55",
|
|
52
|
+
"@types/react-dom": "^18.2.19",
|
|
53
|
+
"husky": "^9.0.10",
|
|
54
|
+
"react": "^18.2.0",
|
|
55
|
+
"react-dom": "^18.2.0",
|
|
56
|
+
"size-limit": "^11.0.2",
|
|
57
|
+
"tsdx": "^0.14.1",
|
|
58
|
+
"tslib": "^2.6.2",
|
|
59
|
+
"typescript": "^3.9.10"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/api/dauth.api.ts
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
import { getServerBasePath } from
|
|
1
|
+
import { getServerBasePath } from './utils/config';
|
|
2
2
|
|
|
3
|
-
export const getTenantUserAPI = async (
|
|
3
|
+
export const getTenantUserAPI = async (
|
|
4
|
+
domainName: string,
|
|
5
|
+
ssid: string,
|
|
6
|
+
token: string
|
|
7
|
+
): Promise<any> => {
|
|
4
8
|
const params = {
|
|
5
|
-
method:
|
|
9
|
+
method: 'GET',
|
|
6
10
|
headers: {
|
|
7
11
|
Authorization: `${token}`,
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
const response = await fetch(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
'Content-Type': 'application/json',
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
const response = await fetch(
|
|
16
|
+
`${getServerBasePath({ domainName })}/get-tenant-user/${ssid}`,
|
|
17
|
+
params
|
|
18
|
+
);
|
|
19
|
+
const data = await response.json();
|
|
20
|
+
return { response, data };
|
|
21
|
+
};
|
package/src/api/utils/config.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
const isLocalhost = Boolean(
|
|
2
|
-
window.location.hostname ===
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
window.location.hostname === 'localhost' ||
|
|
3
|
+
window.location.hostname === '[::1]' ||
|
|
4
|
+
window.location.hostname.match(
|
|
5
|
+
/(192)\.(168)\.(1)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm
|
|
6
|
+
) ||
|
|
7
|
+
window.location.hostname.match(
|
|
8
|
+
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
|
9
|
+
)
|
|
10
10
|
);
|
|
11
|
-
export const apiVersion =
|
|
12
|
-
export const serverDomain =
|
|
11
|
+
export const apiVersion = 'v1';
|
|
12
|
+
export const serverDomain = 'dauth.ovh';
|
|
13
13
|
|
|
14
14
|
export function getServerBasePath({ domainName }: { domainName: string }) {
|
|
15
15
|
const serverPort = 4012;
|
|
16
|
-
const serverLocalUrl = `${window.location.protocol}//${window.location.hostname}:${serverPort}/api/${apiVersion}
|
|
17
|
-
const serverProdUrl = `https://${domainName}.${serverDomain}/api/${apiVersion}
|
|
16
|
+
const serverLocalUrl = `${window.location.protocol}//${window.location.hostname}:${serverPort}/api/${apiVersion}`;
|
|
17
|
+
const serverProdUrl = `https://${domainName}.${serverDomain}/api/${apiVersion}`;
|
|
18
18
|
const serverBasePath = isLocalhost ? serverLocalUrl : serverProdUrl;
|
|
19
19
|
return serverBasePath;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export function getClientBasePath({ domainName }: { domainName: string }) {
|
|
23
23
|
const clientPort = 5185;
|
|
24
|
-
const clientLocalUrl = `${window.location.protocol}//${window.location.hostname}:${clientPort}
|
|
25
|
-
const clientProdUrl = `https://${domainName}.${serverDomain}
|
|
24
|
+
const clientLocalUrl = `${window.location.protocol}//${window.location.hostname}:${clientPort}`;
|
|
25
|
+
const clientProdUrl = `https://${domainName}.${serverDomain}`;
|
|
26
26
|
const clientBasePath = isLocalhost ? clientLocalUrl : clientProdUrl;
|
|
27
27
|
return clientBasePath;
|
|
28
|
-
}
|
|
28
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const DAUTH_STATE = 'dauth_state';
|
|
1
|
+
export const DAUTH_STATE = 'dauth_state';
|
package/src/index.tsx
CHANGED
|
@@ -1,79 +1,84 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
1
|
+
import React, {
|
|
2
|
+
useReducer,
|
|
3
|
+
useMemo,
|
|
4
|
+
useEffect,
|
|
5
|
+
useCallback,
|
|
6
|
+
createContext,
|
|
7
|
+
useContext,
|
|
8
|
+
} from 'react';
|
|
9
|
+
import initialDauthState, { IDauthState } from './initialDauthState';
|
|
10
|
+
import userReducer from './reducer/dauth.reducer';
|
|
11
|
+
import * as action from './reducer/dauth.actions';
|
|
12
|
+
import { getClientBasePath } from './api/utils/config';
|
|
13
|
+
import { DAUTH_STATE } from './constants';
|
|
13
14
|
|
|
14
15
|
interface DauthProviderProps {
|
|
15
|
-
domainName: string
|
|
16
|
-
sid: string
|
|
17
|
-
ssid: string
|
|
18
|
-
children: React.ReactNode
|
|
16
|
+
domainName: string;
|
|
17
|
+
sid: string;
|
|
18
|
+
ssid: string;
|
|
19
|
+
children: React.ReactNode;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const queryString = window.location.search;
|
|
28
|
-
if (!queryString) return
|
|
29
|
-
const urlParams = new URLSearchParams(queryString);
|
|
30
|
-
const dauth_state = urlParams.get(DAUTH_STATE);
|
|
31
|
-
if (dauth_state && !dauthState.isAuthenticated) {
|
|
32
|
-
action.setDauthStateAction({ dispatch, dauth_state, domainName, ssid })
|
|
33
|
-
}
|
|
34
|
-
}, [dauthState.isAuthenticated, domainName, ssid])
|
|
22
|
+
export const DauthProvider: React.FC<DauthProviderProps> = (
|
|
23
|
+
props: DauthProviderProps
|
|
24
|
+
) => {
|
|
25
|
+
const { domainName, sid, ssid, children } = props;
|
|
26
|
+
const [ds, dispatch] = useReducer(userReducer, initialDauthState);
|
|
27
|
+
const dauthState = ds as IDauthState;
|
|
35
28
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
29
|
+
// Catch login redirect
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const queryString = window.location.search;
|
|
32
|
+
if (!queryString) return;
|
|
33
|
+
const urlParams = new URLSearchParams(queryString);
|
|
34
|
+
const dauth_state = urlParams.get(DAUTH_STATE);
|
|
35
|
+
if (dauth_state && !dauthState.isAuthenticated) {
|
|
36
|
+
action.setDauthStateAction({ dispatch, dauth_state, domainName, ssid });
|
|
37
|
+
}
|
|
38
|
+
}, [dauthState.isAuthenticated, domainName, ssid]);
|
|
43
39
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
// Auto Login
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
const dauth_state_ls = localStorage.getItem(DAUTH_STATE);
|
|
43
|
+
if (dauth_state_ls && !dauthState.isAuthenticated) {
|
|
44
|
+
action.setAutoLoginAction({ dispatch, dauth_state_ls, domainName, ssid });
|
|
45
|
+
}
|
|
46
|
+
}, [dauthState.isAuthenticated, domainName, ssid]);
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const loginWithRedirect = useCallback(() => {
|
|
49
|
+
return window.location.replace(
|
|
50
|
+
`${getClientBasePath({ domainName })}/t-sign/${sid}`
|
|
51
|
+
);
|
|
52
|
+
}, [domainName, sid]);
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
loginWithRedirect,
|
|
56
|
-
logout
|
|
57
|
-
}), [
|
|
58
|
-
dauthState,
|
|
59
|
-
loginWithRedirect,
|
|
60
|
-
logout
|
|
61
|
-
])
|
|
54
|
+
const logout = useCallback(() => {
|
|
55
|
+
return action.setLogoutAction({ dispatch });
|
|
56
|
+
}, []);
|
|
62
57
|
|
|
58
|
+
const memoProvider = useMemo(
|
|
59
|
+
() => ({
|
|
60
|
+
...dauthState,
|
|
61
|
+
loginWithRedirect,
|
|
62
|
+
logout,
|
|
63
|
+
}),
|
|
64
|
+
[dauthState, loginWithRedirect, logout]
|
|
65
|
+
);
|
|
63
66
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
67
|
+
return (
|
|
68
|
+
<DauthContext.Provider value={memoProvider}>
|
|
69
|
+
{children}
|
|
70
|
+
</DauthContext.Provider>
|
|
71
|
+
);
|
|
72
|
+
};
|
|
70
73
|
|
|
71
|
-
const DauthContext =
|
|
74
|
+
const DauthContext = createContext(initialDauthState);
|
|
72
75
|
|
|
73
|
-
export const
|
|
74
|
-
const context =
|
|
76
|
+
export const useDauth = () => {
|
|
77
|
+
const context = useContext(DauthContext);
|
|
75
78
|
if (!context) {
|
|
76
|
-
throw new Error(
|
|
79
|
+
throw new Error(
|
|
80
|
+
'useMyContext debe ser utilizado dentro de un MyContextProvider'
|
|
81
|
+
);
|
|
77
82
|
}
|
|
78
83
|
return context;
|
|
79
|
-
};
|
|
84
|
+
};
|