react-oauth-providers 1.0.0 → 1.1.0
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/index.d.mts +45 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +591 -0
- package/dist/index.mjs +565 -0
- package/package.json +8 -8
- package/src/AuthContext.tsx +0 -4
- package/src/AuthProvider.tsx +0 -103
- package/src/actions.ts +0 -156
- package/src/authConfig.ts +0 -56
- package/src/google/index.ts +0 -157
- package/src/index.ts +0 -5
- package/src/types.ts +0 -57
- package/src/useAuth.tsx +0 -13
- package/src/utils/constants.ts +0 -1
- package/src/utils/http.ts +0 -51
- package/src/utils/others.ts +0 -13
- package/src/utils/pkce.ts +0 -15
- package/src/utils/session.ts +0 -33
- package/src/utils/storage.ts +0 -18
- package/tsconfig.json +0 -15
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import react__default, { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
interface IAuthContext {
|
|
5
|
+
loginInProgress: boolean;
|
|
6
|
+
signinWithGoogle: () => Promise<void>;
|
|
7
|
+
logOut: ILogoutFunc;
|
|
8
|
+
user?: any;
|
|
9
|
+
error?: string | null;
|
|
10
|
+
}
|
|
11
|
+
interface IAuthProvider {
|
|
12
|
+
authConfig: IAuthConfig;
|
|
13
|
+
children: ReactNode;
|
|
14
|
+
}
|
|
15
|
+
type IAuthStorageType = "session" | "local";
|
|
16
|
+
type IAuthGoogleProviderType = "spa";
|
|
17
|
+
interface IAuthGoogleProvider {
|
|
18
|
+
clientId: string;
|
|
19
|
+
type?: IAuthGoogleProviderType;
|
|
20
|
+
scope?: string;
|
|
21
|
+
}
|
|
22
|
+
type IAuthConfig = {
|
|
23
|
+
googleProvider?: IAuthGoogleProvider;
|
|
24
|
+
spaCallbackUri?: string;
|
|
25
|
+
logoutRedirect?: string;
|
|
26
|
+
preLogin?: () => void;
|
|
27
|
+
postLogin?: () => void;
|
|
28
|
+
onLogInError?: (err: any) => void;
|
|
29
|
+
storageKeyPrefix?: string;
|
|
30
|
+
storage?: IAuthStorageType;
|
|
31
|
+
error?: string | null;
|
|
32
|
+
};
|
|
33
|
+
type ILogoutFunc = (args?: {
|
|
34
|
+
logoutHint?: string;
|
|
35
|
+
redirect?: boolean;
|
|
36
|
+
redirectUri?: string;
|
|
37
|
+
}) => void;
|
|
38
|
+
|
|
39
|
+
declare const AuthContext: react.Context<IAuthContext>;
|
|
40
|
+
|
|
41
|
+
declare const AuthProvider: react__default.FC<IAuthProvider>;
|
|
42
|
+
|
|
43
|
+
declare function useAuth(): IAuthContext;
|
|
44
|
+
|
|
45
|
+
export { AuthContext, AuthProvider, IAuthConfig, IAuthContext, IAuthProvider, useAuth };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import react__default, { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
interface IAuthContext {
|
|
5
|
+
loginInProgress: boolean;
|
|
6
|
+
signinWithGoogle: () => Promise<void>;
|
|
7
|
+
logOut: ILogoutFunc;
|
|
8
|
+
user?: any;
|
|
9
|
+
error?: string | null;
|
|
10
|
+
}
|
|
11
|
+
interface IAuthProvider {
|
|
12
|
+
authConfig: IAuthConfig;
|
|
13
|
+
children: ReactNode;
|
|
14
|
+
}
|
|
15
|
+
type IAuthStorageType = "session" | "local";
|
|
16
|
+
type IAuthGoogleProviderType = "spa";
|
|
17
|
+
interface IAuthGoogleProvider {
|
|
18
|
+
clientId: string;
|
|
19
|
+
type?: IAuthGoogleProviderType;
|
|
20
|
+
scope?: string;
|
|
21
|
+
}
|
|
22
|
+
type IAuthConfig = {
|
|
23
|
+
googleProvider?: IAuthGoogleProvider;
|
|
24
|
+
spaCallbackUri?: string;
|
|
25
|
+
logoutRedirect?: string;
|
|
26
|
+
preLogin?: () => void;
|
|
27
|
+
postLogin?: () => void;
|
|
28
|
+
onLogInError?: (err: any) => void;
|
|
29
|
+
storageKeyPrefix?: string;
|
|
30
|
+
storage?: IAuthStorageType;
|
|
31
|
+
error?: string | null;
|
|
32
|
+
};
|
|
33
|
+
type ILogoutFunc = (args?: {
|
|
34
|
+
logoutHint?: string;
|
|
35
|
+
redirect?: boolean;
|
|
36
|
+
redirectUri?: string;
|
|
37
|
+
}) => void;
|
|
38
|
+
|
|
39
|
+
declare const AuthContext: react.Context<IAuthContext>;
|
|
40
|
+
|
|
41
|
+
declare const AuthProvider: react__default.FC<IAuthProvider>;
|
|
42
|
+
|
|
43
|
+
declare function useAuth(): IAuthContext;
|
|
44
|
+
|
|
45
|
+
export { AuthContext, AuthProvider, IAuthConfig, IAuthContext, IAuthProvider, useAuth };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __export = (target, all) => {
|
|
24
|
+
for (var name in all)
|
|
25
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
|
+
};
|
|
27
|
+
var __copyProps = (to, from, except, desc) => {
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from))
|
|
30
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
var __async = (__this, __arguments, generator) => {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
var fulfilled = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.next(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var rejected = (value) => {
|
|
46
|
+
try {
|
|
47
|
+
step(generator.throw(value));
|
|
48
|
+
} catch (e) {
|
|
49
|
+
reject(e);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
53
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/index.ts
|
|
58
|
+
var src_exports = {};
|
|
59
|
+
__export(src_exports, {
|
|
60
|
+
AuthContext: () => AuthContext,
|
|
61
|
+
AuthProvider: () => AuthProvider,
|
|
62
|
+
useAuth: () => useAuth
|
|
63
|
+
});
|
|
64
|
+
module.exports = __toCommonJS(src_exports);
|
|
65
|
+
|
|
66
|
+
// src/AuthContext.tsx
|
|
67
|
+
var import_react = require("react");
|
|
68
|
+
var AuthContext = (0, import_react.createContext)({});
|
|
69
|
+
|
|
70
|
+
// src/AuthProvider.tsx
|
|
71
|
+
var import_react2 = require("react");
|
|
72
|
+
|
|
73
|
+
// src/utils/session.ts
|
|
74
|
+
function setSessionItem(key, value, ttlMs) {
|
|
75
|
+
const data = {
|
|
76
|
+
value,
|
|
77
|
+
expires: Date.now() + ttlMs
|
|
78
|
+
};
|
|
79
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
|
80
|
+
}
|
|
81
|
+
function getSessionItem(key) {
|
|
82
|
+
const dataStr = sessionStorage.getItem(key);
|
|
83
|
+
if (!dataStr)
|
|
84
|
+
return null;
|
|
85
|
+
try {
|
|
86
|
+
const data = JSON.parse(dataStr);
|
|
87
|
+
if (Date.now() > data.expires) {
|
|
88
|
+
sessionStorage.removeItem(key);
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
return data.value;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function removeSessionItem(key) {
|
|
97
|
+
try {
|
|
98
|
+
sessionStorage.removeItem(key);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/utils/http.ts
|
|
105
|
+
function request(url, options) {
|
|
106
|
+
return __async(this, null, function* () {
|
|
107
|
+
var _a;
|
|
108
|
+
let body;
|
|
109
|
+
const headers = __spreadValues({}, (_a = options.headers) != null ? _a : {});
|
|
110
|
+
try {
|
|
111
|
+
body = options.body;
|
|
112
|
+
headers["Content-Type"] = headers["Content-Type"] || "application/json";
|
|
113
|
+
const response = yield fetch(url, {
|
|
114
|
+
method: options.method,
|
|
115
|
+
headers,
|
|
116
|
+
body
|
|
117
|
+
});
|
|
118
|
+
if (!response.ok) {
|
|
119
|
+
const error = yield response.json().catch(() => ({}));
|
|
120
|
+
const expectedError = response.status >= 400 && response.status < 500;
|
|
121
|
+
if (!expectedError) {
|
|
122
|
+
console.log({ error });
|
|
123
|
+
console.log("Unexpected error occurs");
|
|
124
|
+
}
|
|
125
|
+
return Promise.reject(__spreadValues({ status: response.status }, error));
|
|
126
|
+
}
|
|
127
|
+
return { data: yield response.json() };
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.log("Network or unexpected error:", error);
|
|
130
|
+
return Promise.reject(error);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
var http_default = {
|
|
135
|
+
get: (url, options) => request(url, __spreadProps(__spreadValues({}, options), { method: "GET" })),
|
|
136
|
+
post: (url, body, options) => request(url, __spreadProps(__spreadValues({}, options), { method: "POST", body })),
|
|
137
|
+
put: (url, body, options) => request(url, __spreadProps(__spreadValues({}, options), { method: "PUT", body })),
|
|
138
|
+
delete: (url, options) => request(url, __spreadProps(__spreadValues({}, options), { method: "DELETE" }))
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/utils/pkce.ts
|
|
142
|
+
var generatePkce = () => __async(void 0, null, function* () {
|
|
143
|
+
const array = new Uint8Array(32);
|
|
144
|
+
crypto.getRandomValues(array);
|
|
145
|
+
const verifier = base64UrlEncode(array);
|
|
146
|
+
const buffer = new TextEncoder().encode(verifier);
|
|
147
|
+
const digest = yield crypto.subtle.digest("SHA-256", buffer);
|
|
148
|
+
const challenge = base64UrlEncode(new Uint8Array(digest));
|
|
149
|
+
return {
|
|
150
|
+
verifier,
|
|
151
|
+
challenge,
|
|
152
|
+
algorithm: "S256"
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
var base64UrlEncode = (array) => {
|
|
156
|
+
let str = "";
|
|
157
|
+
const chunkSize = 32768;
|
|
158
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
|
159
|
+
const chunk = array.subarray(i, i + chunkSize);
|
|
160
|
+
str += String.fromCharCode.apply(null, Array.from(chunk));
|
|
161
|
+
}
|
|
162
|
+
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/utils/others.ts
|
|
166
|
+
var toURL = (input) => {
|
|
167
|
+
try {
|
|
168
|
+
return new URL(input);
|
|
169
|
+
} catch (e) {
|
|
170
|
+
return new URL(input, window.location.origin);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var makeKey = (key, prefix) => {
|
|
174
|
+
return prefix ? `${prefix}_${key}` : key;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// src/authConfig.ts
|
|
178
|
+
var config = null;
|
|
179
|
+
var getConfig = () => config;
|
|
180
|
+
function createInternalConfig(passedConfig) {
|
|
181
|
+
const {
|
|
182
|
+
preLogin = () => null,
|
|
183
|
+
postLogin = () => null,
|
|
184
|
+
onLogInError = () => null,
|
|
185
|
+
storage = "local",
|
|
186
|
+
spaCallbackUri = passedConfig.spaCallbackUri ? toURL(passedConfig.spaCallbackUri).toString() : window.location.origin,
|
|
187
|
+
logoutRedirect = passedConfig.logoutRedirect ? toURL(passedConfig.logoutRedirect).toString() : window.location.origin,
|
|
188
|
+
storageKeyPrefix = "auth_",
|
|
189
|
+
googleProvider
|
|
190
|
+
} = passedConfig;
|
|
191
|
+
config = __spreadProps(__spreadValues({}, passedConfig), {
|
|
192
|
+
preLogin,
|
|
193
|
+
postLogin,
|
|
194
|
+
onLogInError,
|
|
195
|
+
storage,
|
|
196
|
+
storageKeyPrefix,
|
|
197
|
+
spaCallbackUri,
|
|
198
|
+
logoutRedirect,
|
|
199
|
+
keys: createAuthKeys(storageKeyPrefix),
|
|
200
|
+
googleProvider: googleProvider ? __spreadValues({ type: "spa", scope: "openid email profile" }, googleProvider) : void 0
|
|
201
|
+
});
|
|
202
|
+
return config;
|
|
203
|
+
}
|
|
204
|
+
function createAuthKeys(prefix) {
|
|
205
|
+
return {
|
|
206
|
+
PROVIDER_TYPE: makeKey("provider_type", prefix),
|
|
207
|
+
PROVIDER: makeKey("provider", prefix),
|
|
208
|
+
VERIFIER: makeKey("verifier", prefix),
|
|
209
|
+
ACCESS_TOKEN: makeKey("access_token", prefix),
|
|
210
|
+
EXPIRES_IN: makeKey("expires_in", prefix),
|
|
211
|
+
REFRESH_TOKEN: makeKey("refresh_token", prefix),
|
|
212
|
+
TOKEN_TYPE: makeKey("token_type", prefix),
|
|
213
|
+
ID_TOKEN: makeKey("id_token", prefix)
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/utils/constants.ts
|
|
218
|
+
var AUTH_PROVIDERS = { google: "google" };
|
|
219
|
+
|
|
220
|
+
// src/utils/storage.ts
|
|
221
|
+
var keepToStorage = (type, key, value) => {
|
|
222
|
+
if (type === "local")
|
|
223
|
+
localStorage.setItem(key, value);
|
|
224
|
+
else if (type === "session")
|
|
225
|
+
sessionStorage.setItem(key, value);
|
|
226
|
+
};
|
|
227
|
+
var removeFromStorage = (type, key) => {
|
|
228
|
+
if (type === "local")
|
|
229
|
+
localStorage.removeItem(key);
|
|
230
|
+
else if (type === "session")
|
|
231
|
+
sessionStorage.removeItem(key);
|
|
232
|
+
};
|
|
233
|
+
var getFromStorage = (type, key) => {
|
|
234
|
+
var _a, _b;
|
|
235
|
+
if (type === "local")
|
|
236
|
+
return (_a = localStorage.getItem(key)) != null ? _a : "";
|
|
237
|
+
else if (type === "session")
|
|
238
|
+
return (_b = sessionStorage.getItem(key)) != null ? _b : "";
|
|
239
|
+
return "";
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// src/google/index.ts
|
|
243
|
+
var signinWithGoogleSpa = () => __async(void 0, null, function* () {
|
|
244
|
+
const config2 = getConfig();
|
|
245
|
+
if (!config2)
|
|
246
|
+
return;
|
|
247
|
+
const { googleProvider, keys } = config2;
|
|
248
|
+
const { verifier, challenge, algorithm } = yield generatePkce();
|
|
249
|
+
setSessionItem(keys.VERIFIER, verifier, 3 * 60 * 1e3);
|
|
250
|
+
setSessionItem(keys.PROVIDER, AUTH_PROVIDERS.google, 3 * 60 * 1e3);
|
|
251
|
+
setSessionItem(keys.PROVIDER_TYPE, googleProvider == null ? void 0 : googleProvider.type, 3 * 60 * 1e3);
|
|
252
|
+
const params = new URLSearchParams({
|
|
253
|
+
client_id: googleProvider == null ? void 0 : googleProvider.clientId,
|
|
254
|
+
redirect_uri: config2 == null ? void 0 : config2.spaCallbackUri,
|
|
255
|
+
response_type: "code",
|
|
256
|
+
scope: (googleProvider == null ? void 0 : googleProvider.scope) || "",
|
|
257
|
+
code_challenge: challenge,
|
|
258
|
+
code_challenge_method: algorithm,
|
|
259
|
+
access_type: "offline",
|
|
260
|
+
prompt: "consent"
|
|
261
|
+
});
|
|
262
|
+
window.location.href = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
|
|
263
|
+
});
|
|
264
|
+
var signinWithGoogleCallback = (code, verifier) => __async(void 0, null, function* () {
|
|
265
|
+
if (!code || !verifier)
|
|
266
|
+
return;
|
|
267
|
+
const config2 = getConfig();
|
|
268
|
+
if (!config2)
|
|
269
|
+
return;
|
|
270
|
+
const { googleProvider, keys } = config2;
|
|
271
|
+
const type = "spa";
|
|
272
|
+
try {
|
|
273
|
+
const data = new URLSearchParams({
|
|
274
|
+
client_id: googleProvider == null ? void 0 : googleProvider.clientId,
|
|
275
|
+
grant_type: "authorization_code",
|
|
276
|
+
code,
|
|
277
|
+
redirect_uri: config2 == null ? void 0 : config2.spaCallbackUri,
|
|
278
|
+
code_verifier: verifier
|
|
279
|
+
});
|
|
280
|
+
const res = yield http_default.post(
|
|
281
|
+
"https://oauth2.googleapis.com/token",
|
|
282
|
+
data.toString(),
|
|
283
|
+
{
|
|
284
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
285
|
+
}
|
|
286
|
+
);
|
|
287
|
+
const tokens = res.data;
|
|
288
|
+
if (tokens == null ? void 0 : tokens.access_token) {
|
|
289
|
+
removeSessionItem(keys.PROVIDER);
|
|
290
|
+
removeSessionItem(keys.VERIFIER);
|
|
291
|
+
removeSessionItem(keys.PROVIDER_TYPE);
|
|
292
|
+
keepToStorage(config2.storage, keys.PROVIDER, AUTH_PROVIDERS.google);
|
|
293
|
+
keepToStorage(config2.storage, keys.PROVIDER_TYPE, type);
|
|
294
|
+
keepToStorage(config2.storage, keys.ACCESS_TOKEN, tokens.access_token);
|
|
295
|
+
keepToStorage(
|
|
296
|
+
config2.storage,
|
|
297
|
+
keys.EXPIRES_IN,
|
|
298
|
+
String(Date.now() + tokens.expires_in * 1e3)
|
|
299
|
+
);
|
|
300
|
+
keepToStorage(config2.storage, keys.REFRESH_TOKEN, tokens.refresh_token);
|
|
301
|
+
keepToStorage(config2.storage, keys.TOKEN_TYPE, tokens.token_type);
|
|
302
|
+
keepToStorage(config2.storage, keys.ID_TOKEN, tokens.id_token);
|
|
303
|
+
}
|
|
304
|
+
} catch (error) {
|
|
305
|
+
return Promise.reject(error);
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
var getGoogleSessionUser = () => __async(void 0, null, function* () {
|
|
309
|
+
const config2 = getConfig();
|
|
310
|
+
if (!config2)
|
|
311
|
+
return;
|
|
312
|
+
const { keys } = config2;
|
|
313
|
+
try {
|
|
314
|
+
const accessToken = getFromStorage(config2.storage, keys.ACCESS_TOKEN);
|
|
315
|
+
if (!accessToken)
|
|
316
|
+
return;
|
|
317
|
+
const res = yield http_default.get(
|
|
318
|
+
`https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=${accessToken}`
|
|
319
|
+
);
|
|
320
|
+
const user = res.data;
|
|
321
|
+
return user;
|
|
322
|
+
} catch (error) {
|
|
323
|
+
return Promise.reject(error);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
var refreshGoogleAccessToken = () => __async(void 0, null, function* () {
|
|
327
|
+
const config2 = getConfig();
|
|
328
|
+
if (!config2)
|
|
329
|
+
return;
|
|
330
|
+
const { googleProvider, keys } = config2;
|
|
331
|
+
const refreshToken = getFromStorage(config2.storage, keys.REFRESH_TOKEN);
|
|
332
|
+
if (!refreshToken)
|
|
333
|
+
return;
|
|
334
|
+
try {
|
|
335
|
+
const data = new URLSearchParams({
|
|
336
|
+
client_id: googleProvider == null ? void 0 : googleProvider.clientId,
|
|
337
|
+
grant_type: "refresh_token",
|
|
338
|
+
refresh_token: refreshToken
|
|
339
|
+
});
|
|
340
|
+
const res = yield http_default.post(
|
|
341
|
+
"https://oauth2.googleapis.com/token",
|
|
342
|
+
data.toString(),
|
|
343
|
+
{
|
|
344
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
345
|
+
}
|
|
346
|
+
);
|
|
347
|
+
const tokens = res.data;
|
|
348
|
+
if (!tokens.access_token) {
|
|
349
|
+
console.error("Failed to refresh access token", tokens);
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
keepToStorage(config2.storage, keys.ACCESS_TOKEN, tokens.access_token);
|
|
353
|
+
if (tokens.expires_in) {
|
|
354
|
+
keepToStorage(
|
|
355
|
+
config2.storage,
|
|
356
|
+
keys.EXPIRES_IN,
|
|
357
|
+
String(Date.now() + tokens.expires_in * 1e3)
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
if (tokens.id_token)
|
|
361
|
+
keepToStorage(config2.storage, keys.ID_TOKEN, tokens.id_token);
|
|
362
|
+
if (tokens.token_type)
|
|
363
|
+
keepToStorage(config2.storage, keys.TOKEN_TYPE, tokens.token_type);
|
|
364
|
+
} catch (error) {
|
|
365
|
+
return Promise.reject(error);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
// src/actions.ts
|
|
370
|
+
var refreshTimeout = null;
|
|
371
|
+
var handleCallback = () => __async(void 0, null, function* () {
|
|
372
|
+
const config2 = getConfig();
|
|
373
|
+
if (!config2)
|
|
374
|
+
return;
|
|
375
|
+
const keys = config2 == null ? void 0 : config2.keys;
|
|
376
|
+
const authMode = getSessionItem(keys.PROVIDER);
|
|
377
|
+
const verifier = getSessionItem(keys.VERIFIER);
|
|
378
|
+
if (!authMode || !verifier)
|
|
379
|
+
return;
|
|
380
|
+
try {
|
|
381
|
+
switch (authMode) {
|
|
382
|
+
case AUTH_PROVIDERS.google: {
|
|
383
|
+
const searchQuery = new URLSearchParams(window.location.search);
|
|
384
|
+
const code = searchQuery.get("code");
|
|
385
|
+
if (!code)
|
|
386
|
+
return;
|
|
387
|
+
yield signinWithGoogleCallback(code, verifier);
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
default:
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
} catch (error) {
|
|
394
|
+
return Promise.reject(error);
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
var handleFetchSessionUser = (retry = true) => __async(void 0, null, function* () {
|
|
398
|
+
const config2 = getConfig();
|
|
399
|
+
if (!config2)
|
|
400
|
+
return;
|
|
401
|
+
const keys = config2 == null ? void 0 : config2.keys;
|
|
402
|
+
const authMode = getFromStorage(config2.storage, keys.PROVIDER);
|
|
403
|
+
if (!authMode)
|
|
404
|
+
return;
|
|
405
|
+
let sessionUser = null;
|
|
406
|
+
try {
|
|
407
|
+
switch (authMode) {
|
|
408
|
+
case AUTH_PROVIDERS.google: {
|
|
409
|
+
sessionUser = yield getGoogleSessionUser();
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
default:
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
if (sessionUser)
|
|
416
|
+
handleScheduleTokenRefresh();
|
|
417
|
+
return sessionUser;
|
|
418
|
+
} catch (error) {
|
|
419
|
+
console.log("Fetch Session User Error:", error);
|
|
420
|
+
if (!retry)
|
|
421
|
+
return null;
|
|
422
|
+
switch (authMode) {
|
|
423
|
+
case AUTH_PROVIDERS.google: {
|
|
424
|
+
yield refreshGoogleAccessToken();
|
|
425
|
+
yield handleFetchSessionUser(false);
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
default:
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
var handleScheduleTokenRefresh = () => {
|
|
435
|
+
var _a;
|
|
436
|
+
const config2 = getConfig();
|
|
437
|
+
if (!config2)
|
|
438
|
+
return;
|
|
439
|
+
const keys = config2 == null ? void 0 : config2.keys;
|
|
440
|
+
const authMode = getFromStorage(config2.storage, keys.PROVIDER);
|
|
441
|
+
if (!authMode)
|
|
442
|
+
return;
|
|
443
|
+
const expiresIn = Number(
|
|
444
|
+
(_a = getFromStorage(config2.storage, keys.EXPIRES_IN)) != null ? _a : 0
|
|
445
|
+
);
|
|
446
|
+
if (!expiresIn)
|
|
447
|
+
return;
|
|
448
|
+
clearTokenRefresh();
|
|
449
|
+
const refreshTime = expiresIn - 60 * 1e3;
|
|
450
|
+
refreshTimeout = setTimeout(() => __async(void 0, null, function* () {
|
|
451
|
+
switch (authMode) {
|
|
452
|
+
case AUTH_PROVIDERS.google: {
|
|
453
|
+
yield refreshGoogleAccessToken();
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
default:
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
}), refreshTime);
|
|
460
|
+
};
|
|
461
|
+
var clearTokenRefresh = () => {
|
|
462
|
+
if (refreshTimeout) {
|
|
463
|
+
clearTimeout(refreshTimeout);
|
|
464
|
+
refreshTimeout = null;
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
var logout = () => {
|
|
468
|
+
clearTokens();
|
|
469
|
+
clearTokenRefresh();
|
|
470
|
+
};
|
|
471
|
+
var clearTokens = () => {
|
|
472
|
+
const config2 = getConfig();
|
|
473
|
+
if (!config2)
|
|
474
|
+
return;
|
|
475
|
+
const keys = config2 == null ? void 0 : config2.keys;
|
|
476
|
+
const {
|
|
477
|
+
PROVIDER,
|
|
478
|
+
ACCESS_TOKEN,
|
|
479
|
+
EXPIRES_IN,
|
|
480
|
+
REFRESH_TOKEN,
|
|
481
|
+
TOKEN_TYPE,
|
|
482
|
+
ID_TOKEN,
|
|
483
|
+
PROVIDER_TYPE
|
|
484
|
+
} = keys;
|
|
485
|
+
removeFromStorage(config2.storage, PROVIDER);
|
|
486
|
+
removeFromStorage(config2.storage, PROVIDER_TYPE);
|
|
487
|
+
removeFromStorage(config2.storage, ACCESS_TOKEN);
|
|
488
|
+
removeFromStorage(config2.storage, EXPIRES_IN);
|
|
489
|
+
removeFromStorage(config2.storage, REFRESH_TOKEN);
|
|
490
|
+
removeFromStorage(config2.storage, TOKEN_TYPE);
|
|
491
|
+
removeFromStorage(config2.storage, ID_TOKEN);
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// src/AuthProvider.tsx
|
|
495
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
496
|
+
var AuthProvider = ({
|
|
497
|
+
authConfig,
|
|
498
|
+
children
|
|
499
|
+
}) => {
|
|
500
|
+
const config2 = (0, import_react2.useMemo)(
|
|
501
|
+
() => createInternalConfig(authConfig),
|
|
502
|
+
[authConfig]
|
|
503
|
+
);
|
|
504
|
+
const tmpAuthProviderType = (0, import_react2.useMemo)(
|
|
505
|
+
() => getSessionItem(config2.keys.PROVIDER_TYPE),
|
|
506
|
+
[config2]
|
|
507
|
+
);
|
|
508
|
+
const [loginInProgress, setLoginInProgress] = (0, import_react2.useState)(true);
|
|
509
|
+
const [user, setUser] = (0, import_react2.useState)();
|
|
510
|
+
(0, import_react2.useEffect)(() => {
|
|
511
|
+
handleFetchSessionUser().then((user2) => {
|
|
512
|
+
setUser(user2);
|
|
513
|
+
setLoginInProgress(false);
|
|
514
|
+
});
|
|
515
|
+
}, []);
|
|
516
|
+
const signinWithGoogle = (0, import_react2.useCallback)(() => __async(void 0, null, function* () {
|
|
517
|
+
var _a, _b, _c;
|
|
518
|
+
if (!((_a = config2.googleProvider) == null ? void 0 : _a.clientId)) {
|
|
519
|
+
throw Error(
|
|
520
|
+
"'clientId' must be set in the 'googleProvider' object in the AuthProvider Config"
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
(_b = config2.preLogin) == null ? void 0 : _b.call(config2);
|
|
524
|
+
try {
|
|
525
|
+
if (config2.googleProvider.type === "spa") {
|
|
526
|
+
yield signinWithGoogleSpa();
|
|
527
|
+
}
|
|
528
|
+
} catch (error) {
|
|
529
|
+
(_c = config2.onLogInError) == null ? void 0 : _c.call(config2, error);
|
|
530
|
+
}
|
|
531
|
+
}), []);
|
|
532
|
+
(0, import_react2.useEffect)(() => {
|
|
533
|
+
if (!config2 || !tmpAuthProviderType)
|
|
534
|
+
return;
|
|
535
|
+
if (window.location.pathname !== new URL(config2.spaCallbackUri).pathname) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
const callback = () => __async(void 0, null, function* () {
|
|
539
|
+
var _a, _b;
|
|
540
|
+
try {
|
|
541
|
+
yield handleCallback();
|
|
542
|
+
(_a = config2.postLogin) == null ? void 0 : _a.call(config2);
|
|
543
|
+
} catch (error) {
|
|
544
|
+
(_b = config2.onLogInError) == null ? void 0 : _b.call(config2, error);
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
callback();
|
|
548
|
+
}, [tmpAuthProviderType, config2]);
|
|
549
|
+
const logOut = (0, import_react2.useCallback)(
|
|
550
|
+
({ logoutHint, redirect = true, redirectUri } = {}) => {
|
|
551
|
+
logout();
|
|
552
|
+
if (!redirect)
|
|
553
|
+
return;
|
|
554
|
+
const baseUrl = redirectUri || config2.logoutRedirect || "/";
|
|
555
|
+
const params = new URLSearchParams();
|
|
556
|
+
if (logoutHint)
|
|
557
|
+
params.append("logout_hint", logoutHint);
|
|
558
|
+
const url = params.toString() ? `${baseUrl}?${params.toString()}` : baseUrl;
|
|
559
|
+
window.location.assign(url);
|
|
560
|
+
},
|
|
561
|
+
[]
|
|
562
|
+
);
|
|
563
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
564
|
+
AuthContext.Provider,
|
|
565
|
+
{
|
|
566
|
+
value: {
|
|
567
|
+
loginInProgress,
|
|
568
|
+
signinWithGoogle,
|
|
569
|
+
user,
|
|
570
|
+
logOut
|
|
571
|
+
},
|
|
572
|
+
children
|
|
573
|
+
}
|
|
574
|
+
);
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
// src/useAuth.tsx
|
|
578
|
+
var import_react3 = require("react");
|
|
579
|
+
function useAuth() {
|
|
580
|
+
const ctx = (0, import_react3.useContext)(AuthContext);
|
|
581
|
+
if (!ctx) {
|
|
582
|
+
throw new Error("useAuth must be used within an AuthProvider");
|
|
583
|
+
}
|
|
584
|
+
return ctx;
|
|
585
|
+
}
|
|
586
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
587
|
+
0 && (module.exports = {
|
|
588
|
+
AuthContext,
|
|
589
|
+
AuthProvider,
|
|
590
|
+
useAuth
|
|
591
|
+
});
|