@airsoko/auth 0.0.1
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/__generated__/globalTypes.d.ts +590 -0
- package/dist/__generated__/globalTypes.d.ts.map +1 -0
- package/dist/__generated__/globalTypes.js +607 -0
- package/dist/src/constants/endpoints.d.ts +5 -0
- package/dist/src/constants/endpoints.d.ts.map +1 -0
- package/dist/src/constants/endpoints.js +7 -0
- package/dist/src/constants/index.d.ts +6 -0
- package/dist/src/constants/index.d.ts.map +1 -0
- package/dist/src/constants/index.js +8 -0
- package/dist/src/core/AuthContext.d.ts +5 -0
- package/dist/src/core/AuthContext.d.ts.map +1 -0
- package/dist/src/core/AuthContext.js +10 -0
- package/dist/src/core/client.d.ts +108 -0
- package/dist/src/core/client.d.ts.map +1 -0
- package/dist/src/core/client.js +256 -0
- package/dist/src/core/old/SampleAuthProvider.d.ts +37 -0
- package/dist/src/core/old/SampleAuthProvider.d.ts.map +1 -0
- package/dist/src/core/old/SampleAuthProvider.js +123 -0
- package/dist/src/core/user/context.d.ts +56 -0
- package/dist/src/core/user/context.d.ts.map +1 -0
- package/dist/src/core/user/context.js +46 -0
- package/dist/src/core/user/hooks.d.ts +2 -0
- package/dist/src/core/user/hooks.d.ts.map +1 -0
- package/dist/src/core/user/hooks.js +5 -0
- package/dist/src/core/user/index.d.ts +4 -0
- package/dist/src/core/user/index.d.ts.map +1 -0
- package/dist/src/core/user/index.js +9 -0
- package/dist/src/core/user/provider.d.ts +19 -0
- package/dist/src/core/user/provider.d.ts.map +1 -0
- package/dist/src/core/user/provider.js +71 -0
- package/dist/src/data/checkDeviceSatus.d.ts +5 -0
- package/dist/src/data/checkDeviceSatus.d.ts.map +1 -0
- package/dist/src/data/checkDeviceSatus.js +58 -0
- package/dist/src/data/getMerchantUser.d.ts +3 -0
- package/dist/src/data/getMerchantUser.d.ts.map +1 -0
- package/dist/src/data/getMerchantUser.js +20 -0
- package/dist/src/data/getRegularUser.d.ts +3 -0
- package/dist/src/data/getRegularUser.d.ts.map +1 -0
- package/dist/src/data/getRegularUser.js +20 -0
- package/dist/src/graphql/__generated__/Logout.d.ts +4 -0
- package/dist/src/graphql/__generated__/Logout.d.ts.map +1 -0
- package/dist/src/graphql/__generated__/Logout.js +6 -0
- package/dist/src/graphql/__generated__/onboarding.d.ts +29 -0
- package/dist/src/graphql/__generated__/onboarding.d.ts.map +1 -0
- package/dist/src/graphql/__generated__/onboarding.js +6 -0
- package/dist/src/graphql/__generated__/onboardingShop.d.ts +66 -0
- package/dist/src/graphql/__generated__/onboardingShop.d.ts.map +1 -0
- package/dist/src/graphql/__generated__/onboardingShop.js +6 -0
- package/dist/src/graphql/queries.d.ts +10 -0
- package/dist/src/graphql/queries.d.ts.map +1 -0
- package/dist/src/graphql/queries.js +68 -0
- package/dist/src/graphql/typings.d.ts +66 -0
- package/dist/src/graphql/typings.d.ts.map +1 -0
- package/dist/src/graphql/typings.js +2 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +25 -0
- package/dist/src/utils/currentPageHasParam.d.ts +13 -0
- package/dist/src/utils/currentPageHasParam.d.ts.map +1 -0
- package/dist/src/utils/currentPageHasParam.js +23 -0
- package/dist/src/utils/getCookie.d.ts +7 -0
- package/dist/src/utils/getCookie.d.ts.map +1 -0
- package/dist/src/utils/getCookie.js +19 -0
- package/dist/src/utils/getMainDomain.d.ts +45 -0
- package/dist/src/utils/getMainDomain.d.ts.map +1 -0
- package/dist/src/utils/getMainDomain.js +73 -0
- package/dist/src/utils/getProtocol.d.ts +8 -0
- package/dist/src/utils/getProtocol.d.ts.map +1 -0
- package/dist/src/utils/getProtocol.js +13 -0
- package/dist/src/utils/getURL.d.ts +26 -0
- package/dist/src/utils/getURL.d.ts.map +1 -0
- package/dist/src/utils/getURL.js +56 -0
- package/dist/src/utils/index.d.ts +8 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/index.js +17 -0
- package/dist/src/utils/jwt/decode.d.ts +3 -0
- package/dist/src/utils/jwt/decode.d.ts.map +1 -0
- package/dist/src/utils/jwt/decode.js +32 -0
- package/dist/src/utils/jwt/encode.d.ts +3 -0
- package/dist/src/utils/jwt/encode.d.ts.map +1 -0
- package/dist/src/utils/jwt/encode.js +25 -0
- package/dist/src/utils/jwt/index.d.ts +3 -0
- package/dist/src/utils/jwt/index.d.ts.map +1 -0
- package/dist/src/utils/jwt/index.js +7 -0
- package/dist/src/utils/redirectToPathWithParams.d.ts +9 -0
- package/dist/src/utils/redirectToPathWithParams.d.ts.map +1 -0
- package/dist/src/utils/redirectToPathWithParams.js +22 -0
- package/dist/src/utils/setSharedCookie.d.ts +8 -0
- package/dist/src/utils/setSharedCookie.d.ts.map +1 -0
- package/dist/src/utils/setSharedCookie.js +21 -0
- package/package.json +35 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthProvider = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const user_1 = require("./user");
|
|
6
|
+
/** The authentication provider component. */
|
|
7
|
+
const AuthProvider = ({ children, domain, clientId, redirectUri, audience, }) => {
|
|
8
|
+
return (0, jsx_runtime_1.jsx)(user_1.UserAuthProvider, { domain, clientId, redirectUri, audience, children: children });
|
|
9
|
+
};
|
|
10
|
+
exports.AuthProvider = AuthProvider;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { AuthAudience, AuthUserProperties } from "../graphql/typings";
|
|
2
|
+
export declare class AuthClient {
|
|
3
|
+
private domain;
|
|
4
|
+
private clientId;
|
|
5
|
+
private redirectUri;
|
|
6
|
+
private audience;
|
|
7
|
+
private authToken;
|
|
8
|
+
constructor(domain: string, clientId: string, redirectUri: string, audience: AuthAudience);
|
|
9
|
+
/**
|
|
10
|
+
* Logins the user and redirects
|
|
11
|
+
* @param token
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
signIn(token: string): Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* Logs the user out by removing the authentication token.
|
|
17
|
+
*/
|
|
18
|
+
logout(): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Get the authentication token.
|
|
21
|
+
* @returns {string | null} The authentication token, or null if not available.
|
|
22
|
+
*/
|
|
23
|
+
getAuthToken(): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Determines whether the user is currently authenticated.
|
|
26
|
+
* @returns A boolean indicating whether the user is authenticated.
|
|
27
|
+
*/
|
|
28
|
+
isAuthenticated(): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Store the user authentication token.
|
|
31
|
+
* @param {string} token - The authentication token to store.
|
|
32
|
+
* @returns {boolean} True if the token is successfully stored.
|
|
33
|
+
*/
|
|
34
|
+
storeUserAuthToken(token: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Clear the merchant authentication session.
|
|
37
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
38
|
+
*/
|
|
39
|
+
clearMerchantAuthSession(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Clear the uneccesary authentication session.
|
|
42
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
43
|
+
*/
|
|
44
|
+
private clearPreviousSessionAfterLogin;
|
|
45
|
+
/**
|
|
46
|
+
* Clear the uneccesary authentication session after logout.
|
|
47
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
48
|
+
*/
|
|
49
|
+
private clearPreviousSessionAfterLogout;
|
|
50
|
+
/**
|
|
51
|
+
* Clear the merchant temporary session.
|
|
52
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
53
|
+
*/
|
|
54
|
+
clearMerchantTemporarySession(): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Clear the merchant pre-login session.
|
|
57
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
58
|
+
*/
|
|
59
|
+
clearMerchantPreLoginSession(): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves the authenticated user's information.
|
|
62
|
+
* @async
|
|
63
|
+
* @returns A Promise that resolves to the user's information if authenticated, null otherwise.
|
|
64
|
+
* @throws An error if there is a problem querying the server.
|
|
65
|
+
*/
|
|
66
|
+
getUser(): Promise<AuthUserProperties>;
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves the authenticated user's information.
|
|
69
|
+
* @async
|
|
70
|
+
* @returns A Promise that resolves to the user's information if authenticated, null otherwise.
|
|
71
|
+
* @throws An error if there is a problem querying the server.
|
|
72
|
+
*/
|
|
73
|
+
storeUserIdentifiers(props: AuthUserProperties): Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* Retrieves the authenticated user's information.
|
|
76
|
+
* @async
|
|
77
|
+
* @returns A Promise that resolves to the user's information if authenticated, null otherwise.
|
|
78
|
+
* @throws An error if there is a problem querying the server.
|
|
79
|
+
*/
|
|
80
|
+
clearUserIdentifiers(): Promise<boolean>;
|
|
81
|
+
/**
|
|
82
|
+
* Sets the authentication token in the state and stores it in localStorage.
|
|
83
|
+
* @param token - The authentication token to set.
|
|
84
|
+
*/
|
|
85
|
+
setAuthToken(token: string): void;
|
|
86
|
+
/**
|
|
87
|
+
* Returns a JWT token obtained from the server using the current authenticated user's token.
|
|
88
|
+
* Throws an error if the user is not authenticated or if there is a problem getting the token from the server.
|
|
89
|
+
* @returns {Promise<string>} - A promise that resolves with the JWT token.
|
|
90
|
+
* @throws {Error} - Throws an error if the user is not authenticated or if there is a problem getting the token from the server.
|
|
91
|
+
*/
|
|
92
|
+
getTokenSilently(): Promise<string>;
|
|
93
|
+
/**
|
|
94
|
+
* Redirects the user to a stored or predefined URL after login.
|
|
95
|
+
* If certain query parameters exist in the URL, it handles redirection accordingly.
|
|
96
|
+
*/
|
|
97
|
+
redirectToStoredUrl(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Stores the current URL in a cookie before redirecting the user to the login page
|
|
100
|
+
*/
|
|
101
|
+
redirectToLogin({ selectAccount, }?: {
|
|
102
|
+
loginShop?: boolean;
|
|
103
|
+
selectAccount?: boolean;
|
|
104
|
+
selectStore?: boolean;
|
|
105
|
+
}): void;
|
|
106
|
+
private emptyUser;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/core/client.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAQtE,qBAAa,UAAU;IAInB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,QAAQ;IANlB,OAAO,CAAC,SAAS,CAAuB;gBAG9B,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,YAAY;IAShC;;;;OAIG;IACU,MAAM,CAAC,KAAK,EAAE,MAAM;IAMjC;;OAEG;IACU,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAWpC;;;OAGG;IACI,YAAY,IAAI,MAAM,GAAG,IAAI;IAIpC;;;OAGG;IACI,eAAe,IAAI,OAAO;IAIjC;;;;OAIG;IACI,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAKjD;;;OAGG;IACI,wBAAwB,IAAI,OAAO;IAI1C;;;OAGG;IACH,OAAO,CAAC,8BAA8B;IAItC;;;OAGG;IACH,OAAO,CAAC,+BAA+B;IAMvC;;;OAGG;IACI,6BAA6B,IAAI,OAAO;IAK/C;;;OAGG;IACI,4BAA4B,IAAI,OAAO;IAM9C;;;;;OAKG;IAEU,OAAO,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAcnD;;;;;OAKG;IAEU,oBAAoB,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,OAAO,CAAC;IAW9E;;;;;OAKG;IAEU,oBAAoB,IAAI,OAAO,CAAC,OAAO,CAAC;IAOrD;;;OAGG;IACI,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAOxC;;;;;OAKG;IACU,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;IAehD;;;OAGG;IACI,mBAAmB,IAAI,IAAI;IA+ClC;;OAEG;IACI,eAAe,CAAC,EACrB,aAAqB,GACtB,GAAE;QAAE,SAAS,CAAC,EAAE,OAAO,CAAC;QAAC,aAAa,CAAC,EAAE,OAAO,CAAC;QAAC,WAAW,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,IAAI;IAetF,OAAO,CAAC,SAAS,CAMf;CACH"}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AuthClient = void 0;
|
|
7
|
+
/* eslint-disable no-console */
|
|
8
|
+
const js_cookie_1 = __importDefault(require("js-cookie"));
|
|
9
|
+
const globalTypes_1 = require("../../__generated__/globalTypes");
|
|
10
|
+
const constants_1 = require("../constants");
|
|
11
|
+
const getRegularUser_1 = require("../data/getRegularUser");
|
|
12
|
+
const queries_1 = require("../graphql/queries");
|
|
13
|
+
const utils_1 = require("../utils");
|
|
14
|
+
const jwt_1 = require("../utils/jwt");
|
|
15
|
+
class AuthClient {
|
|
16
|
+
constructor(domain, clientId, redirectUri, audience) {
|
|
17
|
+
this.domain = domain;
|
|
18
|
+
this.clientId = clientId;
|
|
19
|
+
this.redirectUri = redirectUri;
|
|
20
|
+
this.audience = audience;
|
|
21
|
+
this.authToken = null;
|
|
22
|
+
this.emptyUser = {
|
|
23
|
+
user: null,
|
|
24
|
+
shop: null,
|
|
25
|
+
currencyCode: globalTypes_1.CurrencyCode.USD,
|
|
26
|
+
countryCode: globalTypes_1.CountryCode.KE,
|
|
27
|
+
staffMember: null,
|
|
28
|
+
};
|
|
29
|
+
const storedAuthToken = js_cookie_1.default.get(constants_1.AUTH_TOKEN_KEY);
|
|
30
|
+
if (storedAuthToken) {
|
|
31
|
+
this.authToken = storedAuthToken;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Logins the user and redirects
|
|
36
|
+
* @param token
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
async signIn(token) {
|
|
40
|
+
await this.setAuthToken(token);
|
|
41
|
+
await this.redirectToStoredUrl();
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Logs the user out by removing the authentication token.
|
|
46
|
+
*/
|
|
47
|
+
async logout() {
|
|
48
|
+
if (this.audience == "MERCHANT") {
|
|
49
|
+
await (0, queries_1.LogoutUser)();
|
|
50
|
+
}
|
|
51
|
+
js_cookie_1.default.remove(constants_1.AUTH_TOKEN_KEY);
|
|
52
|
+
this.clearUserIdentifiers();
|
|
53
|
+
this.clearPreviousSessionAfterLogout();
|
|
54
|
+
this.authToken = null;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get the authentication token.
|
|
58
|
+
* @returns {string | null} The authentication token, or null if not available.
|
|
59
|
+
*/
|
|
60
|
+
getAuthToken() {
|
|
61
|
+
return this.authToken;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Determines whether the user is currently authenticated.
|
|
65
|
+
* @returns A boolean indicating whether the user is authenticated.
|
|
66
|
+
*/
|
|
67
|
+
isAuthenticated() {
|
|
68
|
+
return !!this.getAuthToken();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Store the user authentication token.
|
|
72
|
+
* @param {string} token - The authentication token to store.
|
|
73
|
+
* @returns {boolean} True if the token is successfully stored.
|
|
74
|
+
*/
|
|
75
|
+
storeUserAuthToken(token) {
|
|
76
|
+
(0, utils_1.setSharedCookie)(constants_1.AUTH_TOKEN_KEY, token, 60 * 60 * 60); // Assuming AUTH_TOKEN_KEY is defined elsewhere
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Clear the merchant authentication session.
|
|
81
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
82
|
+
*/
|
|
83
|
+
clearMerchantAuthSession() {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Clear the uneccesary authentication session.
|
|
88
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
89
|
+
*/
|
|
90
|
+
clearPreviousSessionAfterLogin() {
|
|
91
|
+
js_cookie_1.default.remove(constants_1.CUSTOMER_REDIRECT_URL_COOKIE_KEY);
|
|
92
|
+
js_cookie_1.default.remove(constants_1.REDIRECT_CUSTOMER);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Clear the uneccesary authentication session after logout.
|
|
96
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
97
|
+
*/
|
|
98
|
+
clearPreviousSessionAfterLogout() {
|
|
99
|
+
js_cookie_1.default.remove(constants_1.CUSTOMER_REDIRECT_URL_COOKIE_KEY);
|
|
100
|
+
js_cookie_1.default.remove(constants_1.REDIRECT_CUSTOMER);
|
|
101
|
+
// Cookie.remove(CLIENT_SESSION_ID_KEY);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Clear the merchant temporary session.
|
|
105
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
106
|
+
*/
|
|
107
|
+
clearMerchantTemporarySession() {
|
|
108
|
+
// Cookie.remove(CLIENT_SESSION_ID_KEY); // Assuming CLIENT_SESSION_ID_KEY is defined elsewhere
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Clear the merchant pre-login session.
|
|
113
|
+
* @returns {boolean} True if the session is successfully cleared.
|
|
114
|
+
*/
|
|
115
|
+
clearMerchantPreLoginSession() {
|
|
116
|
+
document.cookie = "loginShop=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
117
|
+
document.cookie = "shopRedirectUrl=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Retrieves the authenticated user's information.
|
|
122
|
+
* @async
|
|
123
|
+
* @returns A Promise that resolves to the user's information if authenticated, null otherwise.
|
|
124
|
+
* @throws An error if there is a problem querying the server.
|
|
125
|
+
*/
|
|
126
|
+
async getUser() {
|
|
127
|
+
const authToken = this.getAuthToken();
|
|
128
|
+
try {
|
|
129
|
+
const regularUser = await (0, getRegularUser_1.getRegularUser)(authToken);
|
|
130
|
+
if (regularUser)
|
|
131
|
+
return regularUser;
|
|
132
|
+
this.logout();
|
|
133
|
+
return this.emptyUser;
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
console.error("Error in getUser:", error);
|
|
137
|
+
this.logout();
|
|
138
|
+
return this.emptyUser;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Retrieves the authenticated user's information.
|
|
143
|
+
* @async
|
|
144
|
+
* @returns A Promise that resolves to the user's information if authenticated, null otherwise.
|
|
145
|
+
* @throws An error if there is a problem querying the server.
|
|
146
|
+
*/
|
|
147
|
+
async storeUserIdentifiers(props) {
|
|
148
|
+
const { user, currencyCode } = props;
|
|
149
|
+
const { id, avatar, email, phone, username, name } = user;
|
|
150
|
+
const idToken = (0, jwt_1.createJWT)(id, email, name, "xxx", 1000);
|
|
151
|
+
(0, utils_1.setSharedCookie)("userId", id);
|
|
152
|
+
(0, utils_1.setSharedCookie)("client", idToken);
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Retrieves the authenticated user's information.
|
|
157
|
+
* @async
|
|
158
|
+
* @returns A Promise that resolves to the user's information if authenticated, null otherwise.
|
|
159
|
+
* @throws An error if there is a problem querying the server.
|
|
160
|
+
*/
|
|
161
|
+
async clearUserIdentifiers() {
|
|
162
|
+
(0, utils_1.setSharedCookie)("client", "");
|
|
163
|
+
(0, utils_1.setSharedCookie)("userId", "");
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Sets the authentication token in the state and stores it in localStorage.
|
|
168
|
+
* @param token - The authentication token to set.
|
|
169
|
+
*/
|
|
170
|
+
setAuthToken(token) {
|
|
171
|
+
this.authToken = token;
|
|
172
|
+
this.clearMerchantAuthSession();
|
|
173
|
+
this.clearMerchantTemporarySession();
|
|
174
|
+
this.storeUserAuthToken(token);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Returns a JWT token obtained from the server using the current authenticated user's token.
|
|
178
|
+
* Throws an error if the user is not authenticated or if there is a problem getting the token from the server.
|
|
179
|
+
* @returns {Promise<string>} - A promise that resolves with the JWT token.
|
|
180
|
+
* @throws {Error} - Throws an error if the user is not authenticated or if there is a problem getting the token from the server.
|
|
181
|
+
*/
|
|
182
|
+
async getTokenSilently() {
|
|
183
|
+
const authToken = this.getAuthToken();
|
|
184
|
+
if (!authToken) {
|
|
185
|
+
throw new Error("Not authenticated");
|
|
186
|
+
}
|
|
187
|
+
const response = await fetch("/api/token", {
|
|
188
|
+
headers: { Authorization: `Bearer ${authToken}` },
|
|
189
|
+
});
|
|
190
|
+
if (!response.ok) {
|
|
191
|
+
throw new Error("Failed to get token");
|
|
192
|
+
}
|
|
193
|
+
const { token } = await response.json();
|
|
194
|
+
return token;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Redirects the user to a stored or predefined URL after login.
|
|
198
|
+
* If certain query parameters exist in the URL, it handles redirection accordingly.
|
|
199
|
+
*/
|
|
200
|
+
redirectToStoredUrl() {
|
|
201
|
+
// Function to check if a query parameter exists in the current URL
|
|
202
|
+
function getCurrentPageQueryParam(paramName) {
|
|
203
|
+
const urlSearchParams = new URLSearchParams(window.location.search);
|
|
204
|
+
return urlSearchParams.get(paramName);
|
|
205
|
+
}
|
|
206
|
+
// Check if the "sourceUrl" query parameter exists
|
|
207
|
+
if (getCurrentPageQueryParam("sourceUrl")) {
|
|
208
|
+
// Redirect to the URL specified in the "sourceUrl" query parameter
|
|
209
|
+
window.location.href = getCurrentPageQueryParam("sourceUrl");
|
|
210
|
+
}
|
|
211
|
+
// Check if the "rid" query parameter exists
|
|
212
|
+
if ((0, utils_1.currentPageHasParam)("rid")) {
|
|
213
|
+
// Redirect to the "/auth/redirect" path
|
|
214
|
+
return (0, utils_1.redirectToPathWithParams)("/auth/redirect");
|
|
215
|
+
}
|
|
216
|
+
// Get the stored redirect URL from a cookie
|
|
217
|
+
const redirectUrl = js_cookie_1.default.get(constants_1.CUSTOMER_REDIRECT_URL_COOKIE_KEY);
|
|
218
|
+
// Get the previous shop redirect URL from a cookie
|
|
219
|
+
const prevShopRedirectUrl = js_cookie_1.default.get(constants_1.REDIRECT_CUSTOMER);
|
|
220
|
+
// Clear any previous session data after login
|
|
221
|
+
this.clearPreviousSessionAfterLogin();
|
|
222
|
+
// Check if a previous shop redirect URL exists
|
|
223
|
+
if (prevShopRedirectUrl) {
|
|
224
|
+
// Redirect to the previous shop redirect URL
|
|
225
|
+
window.location.href = prevShopRedirectUrl;
|
|
226
|
+
}
|
|
227
|
+
// Check if a redirect URL exists
|
|
228
|
+
if (redirectUrl) {
|
|
229
|
+
// Redirect to the stored URL
|
|
230
|
+
window.location.href = redirectUrl;
|
|
231
|
+
}
|
|
232
|
+
else if (this.redirectUri) {
|
|
233
|
+
// Check if a custom redirect URI is defined
|
|
234
|
+
window.location.href = (0, utils_1.getURL)(this.redirectUri, "");
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
// If no URL was stored or provided, redirect to the home page
|
|
238
|
+
window.location.href = "/";
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Stores the current URL in a cookie before redirecting the user to the login page
|
|
243
|
+
*/
|
|
244
|
+
redirectToLogin({ selectAccount = false, } = {}) {
|
|
245
|
+
const currentUrl = window.location.href;
|
|
246
|
+
const prevShopRedirectUrl = js_cookie_1.default.get(constants_1.REDIRECT_CUSTOMER);
|
|
247
|
+
(0, utils_1.setSharedCookie)(constants_1.CUSTOMER_REDIRECT_URL_COOKIE_KEY, currentUrl);
|
|
248
|
+
(0, utils_1.setSharedCookie)(constants_1.REDIRECT_CUSTOMER, prevShopRedirectUrl ? prevShopRedirectUrl : currentUrl);
|
|
249
|
+
this.domain = window.location.host;
|
|
250
|
+
if (selectAccount) {
|
|
251
|
+
window.location.href = (0, utils_1.getURL)(this.domain, `/`);
|
|
252
|
+
}
|
|
253
|
+
window.location.href = (0, utils_1.getURL)(this.domain, `/auth/login`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
exports.AuthClient = AuthClient;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { AuthAudience } from "../../graphql/typings";
|
|
3
|
+
import { AuthClient } from "../client";
|
|
4
|
+
/** Represents the authentication context. */
|
|
5
|
+
interface AuthWithActions {
|
|
6
|
+
/** Whether the user is authenticated or not. */
|
|
7
|
+
isAuthenticated?: boolean;
|
|
8
|
+
/** The user object returned by the authentication service. */
|
|
9
|
+
user?: any;
|
|
10
|
+
/** A function to log out the user. */
|
|
11
|
+
logout: () => Promise<void>;
|
|
12
|
+
/** The authentication client used by this context. */
|
|
13
|
+
auth?: AuthClient | null;
|
|
14
|
+
/**sign in the user to the application andredirect */
|
|
15
|
+
signIn: AuthClient["signIn"];
|
|
16
|
+
/**redirect user to login */
|
|
17
|
+
goLogin: AuthClient["redirectToLogin"];
|
|
18
|
+
}
|
|
19
|
+
/** Creates a new authentication context. */
|
|
20
|
+
export declare const AuthContext: React.Context<AuthWithActions | null>;
|
|
21
|
+
/** Represents the authentication provider properties. */
|
|
22
|
+
interface AuthProviderProps {
|
|
23
|
+
/** The child components. */
|
|
24
|
+
children: React.ReactNode;
|
|
25
|
+
/** The authentication service domain. */
|
|
26
|
+
domain: string;
|
|
27
|
+
/** The client ID used to authenticate the app. */
|
|
28
|
+
clientId: string;
|
|
29
|
+
/** The URI to which the authentication service redirects after successful authentication. */
|
|
30
|
+
redirectUri: string;
|
|
31
|
+
/** The target audience for the authentication service. */
|
|
32
|
+
audience: AuthAudience;
|
|
33
|
+
}
|
|
34
|
+
/** The authentication provider component. */
|
|
35
|
+
export declare const AuthProvider: React.FC<AuthProviderProps>;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=SampleAuthProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SampleAuthProvider.d.ts","sourceRoot":"","sources":["../../../../src/core/old/SampleAuthProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAoD,MAAM,OAAO,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,6CAA6C;AAC7C,UAAU,eAAe;IACvB,gDAAgD;IAChD,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,8DAA8D;IAC9D,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,sCAAsC;IACtC,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,sDAAsD;IACtD,IAAI,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IACzB,sDAAsD;IACtD,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;IAC7B,4BAA4B;IAC5B,OAAO,EAAE,UAAU,CAAC,iBAAiB,CAAC,CAAC;CACxC;AAED,4CAA4C;AAC5C,eAAO,MAAM,WAAW,uCAAoD,CAAC;AAE7E,yDAAyD;AACzD,UAAU,iBAAiB;IACzB,4BAA4B;IAC5B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,yCAAyC;IACzC,MAAM,EAAE,MAAM,CAAC;IACf,kDAAkD;IAClD,QAAQ,EAAE,MAAM,CAAC;IACjB,6FAA6F;IAC7F,WAAW,EAAE,MAAM,CAAC;IACpB,0DAA0D;IAC1D,QAAQ,EAAE,YAAY,CAAC;CACxB;AAOD,6CAA6C;AAC7C,eAAO,MAAM,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,CAwGpD,CAAC"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.AuthProvider = exports.AuthContext = void 0;
|
|
27
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
28
|
+
const react_1 = __importStar(require("react"));
|
|
29
|
+
const client_1 = require("../client");
|
|
30
|
+
/** Creates a new authentication context. */
|
|
31
|
+
exports.AuthContext = react_1.default.createContext(null);
|
|
32
|
+
/** Initializes the authentication client. */
|
|
33
|
+
const initialAuthClient = (domain, clientId, redirectUri, audience) => {
|
|
34
|
+
return new client_1.AuthClient(domain, clientId, redirectUri, audience);
|
|
35
|
+
};
|
|
36
|
+
/** The authentication provider component. */
|
|
37
|
+
const AuthProvider = ({ children, domain, clientId, redirectUri, audience, }) => {
|
|
38
|
+
const authClient = initialAuthClient(domain, clientId, redirectUri, audience);
|
|
39
|
+
const [isAuthenticated, setIsAuthenticated] = (0, react_1.useState)(false);
|
|
40
|
+
const [sessionLoading, setSessionLoading] = (0, react_1.useState)(false);
|
|
41
|
+
const [user, setUser] = (0, react_1.useState)(undefined);
|
|
42
|
+
(0, react_1.useEffect)(() => {
|
|
43
|
+
/** Initializes the authentication client and updates the state accordingly. */
|
|
44
|
+
async function initializeAuth0() {
|
|
45
|
+
try {
|
|
46
|
+
const isAuthenticated = await authClient.isAuthenticated();
|
|
47
|
+
setIsAuthenticated(isAuthenticated);
|
|
48
|
+
if (isAuthenticated) {
|
|
49
|
+
const user = await authClient.getUser();
|
|
50
|
+
setUser(user);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
// Handle any potential errors here if needed
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
setSessionLoading(false);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
initializeAuth0();
|
|
61
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
62
|
+
}, []);
|
|
63
|
+
/**
|
|
64
|
+
* Logs out the user and updates the state accordingly
|
|
65
|
+
*/
|
|
66
|
+
const logout = (0, react_1.useCallback)(async () => {
|
|
67
|
+
try {
|
|
68
|
+
await authClient.logout();
|
|
69
|
+
setUser(undefined);
|
|
70
|
+
setIsAuthenticated(false);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
setUser(undefined);
|
|
74
|
+
setIsAuthenticated(false);
|
|
75
|
+
}
|
|
76
|
+
}, [authClient]);
|
|
77
|
+
/**
|
|
78
|
+
* Signs in the user with the provided token.
|
|
79
|
+
* @param {string} token - The authentication token.
|
|
80
|
+
* @returns {Promise<boolean>} A promise that resolves to `true` if the sign-in is successful, or `false` if it fails.
|
|
81
|
+
*/
|
|
82
|
+
const signIn = (0, react_1.useCallback)(async (token) => {
|
|
83
|
+
try {
|
|
84
|
+
setSessionLoading(true);
|
|
85
|
+
// Log out the current user (if any)
|
|
86
|
+
await authClient.logout();
|
|
87
|
+
setIsAuthenticated(false);
|
|
88
|
+
// Set the authentication token
|
|
89
|
+
await authClient.setAuthToken(token);
|
|
90
|
+
// Get the user information
|
|
91
|
+
const user = await authClient.getUser();
|
|
92
|
+
// Check if user information includes a shop or staff member or is currently logged in
|
|
93
|
+
if (user.shop || user.staffMember || user.user) {
|
|
94
|
+
setUser(user);
|
|
95
|
+
setIsAuthenticated(true);
|
|
96
|
+
}
|
|
97
|
+
setSessionLoading(false);
|
|
98
|
+
// Redirect to the stored URL (if any)
|
|
99
|
+
await authClient.redirectToStoredUrl();
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
setSessionLoading(false);
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}, [authClient]);
|
|
107
|
+
const segregatedValue = (0, react_1.useMemo)(() => {
|
|
108
|
+
return {
|
|
109
|
+
sessionLoading,
|
|
110
|
+
isAuthenticated,
|
|
111
|
+
user,
|
|
112
|
+
auth: authClient,
|
|
113
|
+
logout,
|
|
114
|
+
signIn,
|
|
115
|
+
goLogin() {
|
|
116
|
+
return authClient.redirectToLogin();
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
120
|
+
}, [domain, clientId, redirectUri, audience, authClient, sessionLoading]);
|
|
121
|
+
return (0, jsx_runtime_1.jsx)(exports.AuthContext.Provider, { value: segregatedValue, children: children });
|
|
122
|
+
};
|
|
123
|
+
exports.AuthProvider = AuthProvider;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { AuthUserProperties } from "../../graphql/typings";
|
|
3
|
+
import { AuthClient } from "../client";
|
|
4
|
+
/** Represents the authentication context. */
|
|
5
|
+
interface AuthWithActions extends StoreContextValue {
|
|
6
|
+
/** The user object returned by the authentication service. */
|
|
7
|
+
/** A function to log out the user. */
|
|
8
|
+
logout: () => Promise<void>;
|
|
9
|
+
/** The authentication client used by this context. */
|
|
10
|
+
auth?: AuthClient | null;
|
|
11
|
+
/** sign in the user to the application and redirect */
|
|
12
|
+
signIn: (token: string) => Promise<void>;
|
|
13
|
+
/** redirect user to login */
|
|
14
|
+
goLogin: AuthClient["redirectToLogin"];
|
|
15
|
+
}
|
|
16
|
+
type AuthAction = {
|
|
17
|
+
type: "SET_IS_AUTHENTICATED";
|
|
18
|
+
payload: boolean;
|
|
19
|
+
} | {
|
|
20
|
+
type: "SET_SESSION_LOADING";
|
|
21
|
+
payload: boolean;
|
|
22
|
+
} | {
|
|
23
|
+
type: "SET_USER";
|
|
24
|
+
payload: AuthUserProperties;
|
|
25
|
+
} | {
|
|
26
|
+
type: "SIGN_IN";
|
|
27
|
+
payload: {
|
|
28
|
+
token: string;
|
|
29
|
+
};
|
|
30
|
+
} | {
|
|
31
|
+
type: "LOGOUT";
|
|
32
|
+
payload: any;
|
|
33
|
+
} | {
|
|
34
|
+
type: "RESOLVE";
|
|
35
|
+
payload: {
|
|
36
|
+
sessionLoading: boolean;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Context for providing device type information throughout the app.
|
|
41
|
+
*/
|
|
42
|
+
export declare const UserAuthContext: import("react").Context<(AuthWithActions & AuthState) | null>;
|
|
43
|
+
export declare const useAuth: () => AuthWithActions & AuthState;
|
|
44
|
+
export declare function useUserAuth(): AuthWithActions & AuthState;
|
|
45
|
+
export declare const AuthState: AuthState;
|
|
46
|
+
interface AuthState {
|
|
47
|
+
/** Whether the user is authenticated or not. */
|
|
48
|
+
isAuthenticated: boolean;
|
|
49
|
+
/**is current session loading */
|
|
50
|
+
sessionLoading: boolean;
|
|
51
|
+
}
|
|
52
|
+
export interface StoreContextValue extends AuthUserProperties, AuthState {
|
|
53
|
+
}
|
|
54
|
+
export declare const AuthReducer: (state: StoreContextValue, action: AuthAction) => StoreContextValue;
|
|
55
|
+
export {};
|
|
56
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../../src/core/user/context.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,6CAA6C;AAC7C,UAAU,eAAgB,SAAQ,iBAAiB;IAEjD,8DAA8D;IAE9D,sCAAsC;IACtC,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,sDAAsD;IACtD,IAAI,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IACzB,uDAAuD;IACvD,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACzC,6BAA6B;IAC7B,OAAO,EAAE,UAAU,CAAC,iBAAiB,CAAC,CAAC;CAIxC;AAED,KAAK,UAAU,GACX;IAAE,IAAI,EAAE,sBAAsB,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAClD;IAAE,IAAI,EAAE,qBAAqB,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GACjD;IACE,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,EAAE,kBAAkB,CAAC;CAC7B,GACD;IACE,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE;QACP,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;CACH,GACD;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,EAAE,GAAG,CAAC;CACd,GACD;IACE,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE;QACP,cAAc,EAAE,OAAO,CAAC;KACzB,CAAC;CACH,CAAC;AAEN;;GAEG;AAEH,eAAO,MAAM,eAAe,+DAA4D,CAAC;AAEzF,eAAO,MAAM,OAAO,mCAQnB,CAAC;AAEF,wBAAgB,WAAW,gCAM1B;AAED,eAAO,MAAM,SAAS,EAAE,SAGvB,CAAC;AAEF,UAAU,SAAS;IACjB,gDAAgD;IAChD,eAAe,EAAE,OAAO,CAAC;IACzB,gCAAgC;IAChC,cAAc,EAAE,OAAO,CAAC;CAEzB;AAED,MAAM,WAAW,iBAAkB,SAAQ,kBAAkB,EAAE,SAAS;CAAG;AAE3E,eAAO,MAAM,WAAW,UAAW,iBAAiB,UAAU,UAAU,KAAG,iBA+B1E,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthReducer = exports.AuthState = exports.useUserAuth = exports.useAuth = exports.UserAuthContext = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* Context for providing device type information throughout the app.
|
|
7
|
+
*/
|
|
8
|
+
// export const UserAuthContext = createContext<UserAuthContextProps | undefined>(undefined)
|
|
9
|
+
exports.UserAuthContext = (0, react_1.createContext)(null);
|
|
10
|
+
const useAuth = () => {
|
|
11
|
+
const context = (0, react_1.useContext)(exports.UserAuthContext);
|
|
12
|
+
if (!context) {
|
|
13
|
+
throw new Error("useAuth must be used within an AuthProvider");
|
|
14
|
+
}
|
|
15
|
+
return context;
|
|
16
|
+
};
|
|
17
|
+
exports.useAuth = useAuth;
|
|
18
|
+
function useUserAuth() {
|
|
19
|
+
const context = (0, react_1.useContext)(exports.UserAuthContext);
|
|
20
|
+
if (!context) {
|
|
21
|
+
throw new Error("useAuth must be used within a UserAuthContextProvider");
|
|
22
|
+
}
|
|
23
|
+
return context;
|
|
24
|
+
}
|
|
25
|
+
exports.useUserAuth = useUserAuth;
|
|
26
|
+
exports.AuthState = {
|
|
27
|
+
isAuthenticated: false,
|
|
28
|
+
sessionLoading: true,
|
|
29
|
+
};
|
|
30
|
+
const AuthReducer = (state, action) => {
|
|
31
|
+
switch (action.type) {
|
|
32
|
+
case "SET_IS_AUTHENTICATED":
|
|
33
|
+
return Object.assign(Object.assign({}, state), { isAuthenticated: action.payload });
|
|
34
|
+
case "SET_SESSION_LOADING":
|
|
35
|
+
return Object.assign(Object.assign({}, state), { sessionLoading: action.payload });
|
|
36
|
+
case "SET_USER":
|
|
37
|
+
return Object.assign(Object.assign(Object.assign({}, state), action === null || action === void 0 ? void 0 : action.payload), { isAuthenticated: !!action.payload.user });
|
|
38
|
+
case "LOGOUT":
|
|
39
|
+
return Object.assign(Object.assign({}, state), { isAuthenticated: false });
|
|
40
|
+
case "RESOLVE":
|
|
41
|
+
return Object.assign(Object.assign({}, state), { sessionLoading: action.payload.sessionLoading });
|
|
42
|
+
default:
|
|
43
|
+
return state;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
exports.AuthReducer = AuthReducer;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../../src/core/user/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA"}
|