@propelauth/nextjs 0.0.60
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +215 -0
- package/dist/client/index.d.ts +95 -0
- package/dist/client/index.js +543 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/index.mjs +500 -0
- package/dist/client/index.mjs.map +1 -0
- package/dist/server/index.d.ts +94 -0
- package/dist/server/index.js +622 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +584 -0
- package/dist/server/index.mjs.map +1 -0
- package/package.json +47 -0
@@ -0,0 +1,500 @@
|
|
1
|
+
"use client";
|
2
|
+
var __async = (__this, __arguments, generator) => {
|
3
|
+
return new Promise((resolve, reject) => {
|
4
|
+
var fulfilled = (value) => {
|
5
|
+
try {
|
6
|
+
step(generator.next(value));
|
7
|
+
} catch (e) {
|
8
|
+
reject(e);
|
9
|
+
}
|
10
|
+
};
|
11
|
+
var rejected = (value) => {
|
12
|
+
try {
|
13
|
+
step(generator.throw(value));
|
14
|
+
} catch (e) {
|
15
|
+
reject(e);
|
16
|
+
}
|
17
|
+
};
|
18
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
19
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
20
|
+
});
|
21
|
+
};
|
22
|
+
|
23
|
+
// src/client/AuthProvider.tsx
|
24
|
+
import React, { useCallback, useEffect, useReducer } from "react";
|
25
|
+
|
26
|
+
// src/user.ts
|
27
|
+
var User = class {
|
28
|
+
constructor(userId, email, orgIdToOrgMemberInfo, firstName, lastName, username, legacyUserId, impersonatorUserId) {
|
29
|
+
this.userId = userId;
|
30
|
+
this.orgIdToOrgMemberInfo = orgIdToOrgMemberInfo;
|
31
|
+
this.email = email;
|
32
|
+
this.firstName = firstName;
|
33
|
+
this.lastName = lastName;
|
34
|
+
this.username = username;
|
35
|
+
this.legacyUserId = legacyUserId;
|
36
|
+
this.impersonatorUserId = impersonatorUserId;
|
37
|
+
}
|
38
|
+
getOrg(orgId) {
|
39
|
+
if (!this.orgIdToOrgMemberInfo) {
|
40
|
+
return void 0;
|
41
|
+
}
|
42
|
+
return this.orgIdToOrgMemberInfo[orgId];
|
43
|
+
}
|
44
|
+
getOrgByName(orgName) {
|
45
|
+
if (!this.orgIdToOrgMemberInfo) {
|
46
|
+
return void 0;
|
47
|
+
}
|
48
|
+
const urlSafeOrgName = orgName.toLowerCase().replace(/ /g, "-");
|
49
|
+
for (const orgId in this.orgIdToOrgMemberInfo) {
|
50
|
+
const orgMemberInfo = this.orgIdToOrgMemberInfo[orgId];
|
51
|
+
if (orgMemberInfo.urlSafeOrgName === urlSafeOrgName) {
|
52
|
+
return orgMemberInfo;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
return void 0;
|
56
|
+
}
|
57
|
+
getOrgs() {
|
58
|
+
if (!this.orgIdToOrgMemberInfo) {
|
59
|
+
return [];
|
60
|
+
}
|
61
|
+
return Object.values(this.orgIdToOrgMemberInfo);
|
62
|
+
}
|
63
|
+
isImpersonating() {
|
64
|
+
return !!this.impersonatorUserId;
|
65
|
+
}
|
66
|
+
static fromJSON(json) {
|
67
|
+
const obj = JSON.parse(json);
|
68
|
+
const orgIdToOrgMemberInfo = {};
|
69
|
+
for (const orgId in obj.orgIdToOrgMemberInfo) {
|
70
|
+
orgIdToOrgMemberInfo[orgId] = OrgMemberInfo.fromJSON(
|
71
|
+
JSON.stringify(obj.orgIdToOrgMemberInfo[orgId])
|
72
|
+
);
|
73
|
+
}
|
74
|
+
return new User(
|
75
|
+
obj.userId,
|
76
|
+
obj.email,
|
77
|
+
orgIdToOrgMemberInfo,
|
78
|
+
obj.firstName,
|
79
|
+
obj.lastName,
|
80
|
+
obj.username,
|
81
|
+
obj.legacyUserId,
|
82
|
+
obj.impersonatorUserId
|
83
|
+
);
|
84
|
+
}
|
85
|
+
};
|
86
|
+
var OrgMemberInfo = class {
|
87
|
+
constructor(orgId, orgName, orgMetadata, urlSafeOrgName, userAssignedRole, userInheritedRolesPlusCurrentRole, userPermissions) {
|
88
|
+
this.orgId = orgId;
|
89
|
+
this.orgName = orgName;
|
90
|
+
this.orgMetadata = orgMetadata;
|
91
|
+
this.urlSafeOrgName = urlSafeOrgName;
|
92
|
+
this.userAssignedRole = userAssignedRole;
|
93
|
+
this.userInheritedRolesPlusCurrentRole = userInheritedRolesPlusCurrentRole;
|
94
|
+
this.userPermissions = userPermissions;
|
95
|
+
}
|
96
|
+
// validation methods
|
97
|
+
isRole(role) {
|
98
|
+
return this.userAssignedRole === role;
|
99
|
+
}
|
100
|
+
isAtLeastRole(role) {
|
101
|
+
return this.userInheritedRolesPlusCurrentRole.includes(role);
|
102
|
+
}
|
103
|
+
hasPermission(permission) {
|
104
|
+
return this.userPermissions.includes(permission);
|
105
|
+
}
|
106
|
+
hasAllPermissions(permissions) {
|
107
|
+
return permissions.every((permission) => this.hasPermission(permission));
|
108
|
+
}
|
109
|
+
static fromJSON(json) {
|
110
|
+
const obj = JSON.parse(json);
|
111
|
+
return new OrgMemberInfo(
|
112
|
+
obj.orgId,
|
113
|
+
obj.orgName,
|
114
|
+
obj.orgMetadata,
|
115
|
+
obj.urlSafeOrgName,
|
116
|
+
obj.userAssignedRole,
|
117
|
+
obj.userInheritedRolesPlusCurrentRole,
|
118
|
+
obj.userPermissions
|
119
|
+
);
|
120
|
+
}
|
121
|
+
// getters for the private fields
|
122
|
+
get assignedRole() {
|
123
|
+
return this.userAssignedRole;
|
124
|
+
}
|
125
|
+
get inheritedRolesPlusCurrentRole() {
|
126
|
+
return this.userInheritedRolesPlusCurrentRole;
|
127
|
+
}
|
128
|
+
get permissions() {
|
129
|
+
return this.userPermissions;
|
130
|
+
}
|
131
|
+
};
|
132
|
+
|
133
|
+
// src/client/utils.ts
|
134
|
+
var USER_INFO_KEY = "__PROPEL_AUTH_USER_INFO";
|
135
|
+
function hasWindow() {
|
136
|
+
return typeof window !== "undefined";
|
137
|
+
}
|
138
|
+
function saveUserToLocalStorage(user) {
|
139
|
+
if (user) {
|
140
|
+
localStorage.setItem(USER_INFO_KEY, JSON.stringify(user));
|
141
|
+
} else {
|
142
|
+
localStorage.setItem(USER_INFO_KEY, "{}");
|
143
|
+
}
|
144
|
+
}
|
145
|
+
function doesLocalStorageMatch(newValue, user) {
|
146
|
+
if (!newValue) {
|
147
|
+
return false;
|
148
|
+
} else if (!user) {
|
149
|
+
return newValue === "{}";
|
150
|
+
}
|
151
|
+
const parsed = JSON.parse(newValue);
|
152
|
+
if (!parsed) {
|
153
|
+
return false;
|
154
|
+
}
|
155
|
+
return isEqual(parsed, user);
|
156
|
+
}
|
157
|
+
function isEqual(a, b) {
|
158
|
+
if (typeof a !== typeof b) {
|
159
|
+
return false;
|
160
|
+
}
|
161
|
+
if (Array.isArray(a) !== Array.isArray(b)) {
|
162
|
+
return false;
|
163
|
+
}
|
164
|
+
if (Array.isArray(a)) {
|
165
|
+
const aArray = a;
|
166
|
+
const bArray = b;
|
167
|
+
if (aArray.length !== bArray.length) {
|
168
|
+
return false;
|
169
|
+
}
|
170
|
+
for (let i = 0; i < aArray.length; i++) {
|
171
|
+
if (!isEqual(aArray[i], bArray[i])) {
|
172
|
+
return false;
|
173
|
+
}
|
174
|
+
}
|
175
|
+
return true;
|
176
|
+
}
|
177
|
+
if (typeof a === "object") {
|
178
|
+
const aKeys = Object.keys(a);
|
179
|
+
const bKeys = Object.keys(b);
|
180
|
+
if (aKeys.length !== bKeys.length) {
|
181
|
+
return false;
|
182
|
+
}
|
183
|
+
for (const key of aKeys) {
|
184
|
+
if (!isEqual(a[key], b[key])) {
|
185
|
+
return false;
|
186
|
+
}
|
187
|
+
}
|
188
|
+
return true;
|
189
|
+
} else {
|
190
|
+
return a === b;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
// src/client/AuthProvider.tsx
|
195
|
+
import { useRouter } from "next/navigation";
|
196
|
+
var AuthContext = React.createContext(void 0);
|
197
|
+
var initialAuthState = {
|
198
|
+
loading: true,
|
199
|
+
user: void 0,
|
200
|
+
authChangeDetected: false
|
201
|
+
};
|
202
|
+
function authStateReducer(_state, action) {
|
203
|
+
const authChangeDetected = !_state.loading && !isEqual(action.user, _state.user);
|
204
|
+
if (!action.user) {
|
205
|
+
return {
|
206
|
+
loading: false,
|
207
|
+
user: void 0,
|
208
|
+
authChangeDetected
|
209
|
+
};
|
210
|
+
} else if (_state.loading) {
|
211
|
+
return {
|
212
|
+
loading: false,
|
213
|
+
user: action.user,
|
214
|
+
authChangeDetected
|
215
|
+
};
|
216
|
+
} else {
|
217
|
+
return {
|
218
|
+
loading: false,
|
219
|
+
user: action.user,
|
220
|
+
authChangeDetected
|
221
|
+
};
|
222
|
+
}
|
223
|
+
}
|
224
|
+
var AuthProvider = (props) => {
|
225
|
+
const [authState, dispatchInner] = useReducer(authStateReducer, initialAuthState);
|
226
|
+
const router = useRouter();
|
227
|
+
const dispatch = useCallback((action) => {
|
228
|
+
dispatchInner(action);
|
229
|
+
saveUserToLocalStorage(action.user);
|
230
|
+
}, [dispatchInner]);
|
231
|
+
useEffect(() => {
|
232
|
+
if (authState.authChangeDetected) {
|
233
|
+
router.refresh();
|
234
|
+
}
|
235
|
+
}, [authState.authChangeDetected, router]);
|
236
|
+
useEffect(() => {
|
237
|
+
let didCancel = false;
|
238
|
+
function refreshAuthInfo2() {
|
239
|
+
return __async(this, null, function* () {
|
240
|
+
const { user } = yield apiGetUserInfo();
|
241
|
+
if (!didCancel) {
|
242
|
+
dispatch({ user });
|
243
|
+
}
|
244
|
+
});
|
245
|
+
}
|
246
|
+
refreshAuthInfo2();
|
247
|
+
return () => {
|
248
|
+
didCancel = true;
|
249
|
+
};
|
250
|
+
}, []);
|
251
|
+
useEffect(() => {
|
252
|
+
let didCancel = false;
|
253
|
+
function refreshToken() {
|
254
|
+
return __async(this, null, function* () {
|
255
|
+
const { user } = yield apiGetUserInfo();
|
256
|
+
if (!didCancel) {
|
257
|
+
dispatch({ user });
|
258
|
+
}
|
259
|
+
});
|
260
|
+
}
|
261
|
+
function onStorageEvent(event) {
|
262
|
+
return __async(this, null, function* () {
|
263
|
+
if (event.key === USER_INFO_KEY && !doesLocalStorageMatch(event.newValue, authState.user)) {
|
264
|
+
yield refreshToken();
|
265
|
+
}
|
266
|
+
});
|
267
|
+
}
|
268
|
+
const interval = setInterval(refreshToken, 5 * 60 * 1e3);
|
269
|
+
if (hasWindow()) {
|
270
|
+
window.addEventListener("storage", onStorageEvent);
|
271
|
+
window.addEventListener("online", refreshToken);
|
272
|
+
window.addEventListener("focus", refreshToken);
|
273
|
+
}
|
274
|
+
return () => {
|
275
|
+
didCancel = true;
|
276
|
+
clearInterval(interval);
|
277
|
+
if (hasWindow()) {
|
278
|
+
window.removeEventListener("storage", onStorageEvent);
|
279
|
+
window.removeEventListener("online", refreshToken);
|
280
|
+
window.removeEventListener("focus", refreshToken);
|
281
|
+
}
|
282
|
+
};
|
283
|
+
}, [dispatch, authState.user]);
|
284
|
+
const logout = useCallback(() => __async(void 0, null, function* () {
|
285
|
+
yield fetch("/api/auth/logout", {
|
286
|
+
method: "POST",
|
287
|
+
headers: {
|
288
|
+
"Content-Type": "application/json"
|
289
|
+
},
|
290
|
+
credentials: "include"
|
291
|
+
});
|
292
|
+
dispatch({ user: void 0 });
|
293
|
+
}), [dispatch]);
|
294
|
+
const getLoginPageUrl = () => "/api/auth/login";
|
295
|
+
const getSignupPageUrl = () => "/api/auth/signup";
|
296
|
+
const getAccountPageUrl = useCallback(() => {
|
297
|
+
return `${props.authUrl}/account`;
|
298
|
+
}, [props.authUrl]);
|
299
|
+
const getOrgPageUrl = useCallback(
|
300
|
+
(orgId) => {
|
301
|
+
if (orgId) {
|
302
|
+
return `${props.authUrl}/org?id=${orgId}`;
|
303
|
+
} else {
|
304
|
+
return `${props.authUrl}/org`;
|
305
|
+
}
|
306
|
+
},
|
307
|
+
[props.authUrl]
|
308
|
+
);
|
309
|
+
const getCreateOrgPageUrl = useCallback(() => {
|
310
|
+
return `${props.authUrl}/create_org`;
|
311
|
+
}, [props.authUrl]);
|
312
|
+
const getSetupSAMLPageUrl = useCallback(
|
313
|
+
(orgId) => {
|
314
|
+
return `${props.authUrl}/saml?id=${orgId}`;
|
315
|
+
},
|
316
|
+
[props.authUrl]
|
317
|
+
);
|
318
|
+
const redirectTo = (url) => {
|
319
|
+
window.location.href = url;
|
320
|
+
};
|
321
|
+
const redirectToLoginPage = () => redirectTo(getLoginPageUrl());
|
322
|
+
const redirectToSignupPage = () => redirectTo(getSignupPageUrl());
|
323
|
+
const redirectToAccountPage = () => redirectTo(getAccountPageUrl());
|
324
|
+
const redirectToOrgPage = (orgId) => redirectTo(getOrgPageUrl(orgId));
|
325
|
+
const redirectToCreateOrgPage = () => redirectTo(getCreateOrgPageUrl());
|
326
|
+
const redirectToSetupSAMLPage = (orgId) => redirectTo(getSetupSAMLPageUrl(orgId));
|
327
|
+
const refreshAuthInfo = () => __async(void 0, null, function* () {
|
328
|
+
const { user } = yield apiGetUserInfo();
|
329
|
+
dispatch({ user });
|
330
|
+
return user;
|
331
|
+
});
|
332
|
+
const value = {
|
333
|
+
loading: authState.loading,
|
334
|
+
user: authState.user,
|
335
|
+
logout,
|
336
|
+
redirectToLoginPage,
|
337
|
+
redirectToSignupPage,
|
338
|
+
redirectToAccountPage,
|
339
|
+
redirectToOrgPage,
|
340
|
+
redirectToCreateOrgPage,
|
341
|
+
redirectToSetupSAMLPage,
|
342
|
+
getLoginPageUrl,
|
343
|
+
getSignupPageUrl,
|
344
|
+
getAccountPageUrl,
|
345
|
+
getOrgPageUrl,
|
346
|
+
getCreateOrgPageUrl,
|
347
|
+
getSetupSAMLPageUrl,
|
348
|
+
refreshAuthInfo
|
349
|
+
};
|
350
|
+
return /* @__PURE__ */ React.createElement(AuthContext.Provider, { value }, props.children);
|
351
|
+
};
|
352
|
+
function apiGetUserInfo() {
|
353
|
+
return __async(this, null, function* () {
|
354
|
+
try {
|
355
|
+
const userInfoResponse = yield fetch("/api/auth/userinfo", {
|
356
|
+
method: "GET",
|
357
|
+
headers: {
|
358
|
+
"Content-Type": "application/json"
|
359
|
+
},
|
360
|
+
credentials: "include"
|
361
|
+
});
|
362
|
+
if (userInfoResponse.ok) {
|
363
|
+
const userJson = yield userInfoResponse.text();
|
364
|
+
const user = User.fromJSON(userJson);
|
365
|
+
return { user };
|
366
|
+
} else if (userInfoResponse.status === 401) {
|
367
|
+
return { user: void 0 };
|
368
|
+
} else {
|
369
|
+
console.log("Failed to refresh token", userInfoResponse);
|
370
|
+
}
|
371
|
+
} catch (e) {
|
372
|
+
console.log("Failed to refresh token", e);
|
373
|
+
}
|
374
|
+
throw new Error("Failed to refresh token");
|
375
|
+
});
|
376
|
+
}
|
377
|
+
|
378
|
+
// src/client/useUser.tsx
|
379
|
+
import { useContext } from "react";
|
380
|
+
function useUser() {
|
381
|
+
const context = useContext(AuthContext);
|
382
|
+
if (context === void 0) {
|
383
|
+
throw new Error("useUser must be used within an AuthProvider");
|
384
|
+
}
|
385
|
+
const { loading, user } = context;
|
386
|
+
if (loading) {
|
387
|
+
return {
|
388
|
+
loading: true,
|
389
|
+
isLoggedIn: void 0,
|
390
|
+
user: void 0
|
391
|
+
};
|
392
|
+
} else if (user) {
|
393
|
+
return {
|
394
|
+
loading: false,
|
395
|
+
isLoggedIn: true,
|
396
|
+
user
|
397
|
+
};
|
398
|
+
} else {
|
399
|
+
return {
|
400
|
+
loading: false,
|
401
|
+
isLoggedIn: false,
|
402
|
+
user: void 0
|
403
|
+
};
|
404
|
+
}
|
405
|
+
}
|
406
|
+
|
407
|
+
// src/client/useHostedPageUrls.tsx
|
408
|
+
import { useContext as useContext2 } from "react";
|
409
|
+
function useHostedPageUrls() {
|
410
|
+
const context = useContext2(AuthContext);
|
411
|
+
if (context === void 0) {
|
412
|
+
throw new Error("useHostedPageUrls must be used within an AuthProvider");
|
413
|
+
}
|
414
|
+
const {
|
415
|
+
getLoginPageUrl,
|
416
|
+
getSignupPageUrl,
|
417
|
+
getAccountPageUrl,
|
418
|
+
getOrgPageUrl,
|
419
|
+
getCreateOrgPageUrl,
|
420
|
+
getSetupSAMLPageUrl
|
421
|
+
} = context;
|
422
|
+
return {
|
423
|
+
getLoginPageUrl,
|
424
|
+
getSignupPageUrl,
|
425
|
+
getAccountPageUrl,
|
426
|
+
getOrgPageUrl,
|
427
|
+
getCreateOrgPageUrl,
|
428
|
+
getSetupSAMLPageUrl
|
429
|
+
};
|
430
|
+
}
|
431
|
+
|
432
|
+
// src/client/useLogoutFunction.ts
|
433
|
+
import { useContext as useContext3 } from "react";
|
434
|
+
function useLogoutFunction() {
|
435
|
+
const context = useContext3(AuthContext);
|
436
|
+
if (context === void 0) {
|
437
|
+
throw new Error("useLogoutFunction must be used within an AuthProvider");
|
438
|
+
}
|
439
|
+
const { logout } = context;
|
440
|
+
return logout;
|
441
|
+
}
|
442
|
+
|
443
|
+
// src/client/useRedirectFunctions.tsx
|
444
|
+
import React2, { useContext as useContext4, useEffect as useEffect2 } from "react";
|
445
|
+
function useRedirectFunctions() {
|
446
|
+
const context = useContext4(AuthContext);
|
447
|
+
if (context === void 0) {
|
448
|
+
throw new Error("useRedirectFunctions must be used within an AuthProvider");
|
449
|
+
}
|
450
|
+
const {
|
451
|
+
redirectToAccountPage,
|
452
|
+
redirectToSignupPage,
|
453
|
+
redirectToLoginPage,
|
454
|
+
redirectToOrgPage,
|
455
|
+
redirectToCreateOrgPage
|
456
|
+
} = context;
|
457
|
+
return {
|
458
|
+
redirectToSignupPage,
|
459
|
+
redirectToLoginPage,
|
460
|
+
redirectToAccountPage,
|
461
|
+
redirectToOrgPage,
|
462
|
+
redirectToCreateOrgPage
|
463
|
+
};
|
464
|
+
}
|
465
|
+
function RedirectToSignup({ children }) {
|
466
|
+
const { redirectToSignupPage } = useRedirectFunctions();
|
467
|
+
useEffect2(() => {
|
468
|
+
redirectToSignupPage();
|
469
|
+
}, []);
|
470
|
+
return /* @__PURE__ */ React2.createElement(React2.Fragment, null, children);
|
471
|
+
}
|
472
|
+
function RedirectToLogin({ children }) {
|
473
|
+
const { redirectToLoginPage } = useRedirectFunctions();
|
474
|
+
useEffect2(() => {
|
475
|
+
redirectToLoginPage();
|
476
|
+
}, []);
|
477
|
+
return /* @__PURE__ */ React2.createElement(React2.Fragment, null, children);
|
478
|
+
}
|
479
|
+
|
480
|
+
// src/client/useRefreshAuth.ts
|
481
|
+
import { useContext as useContext5 } from "react";
|
482
|
+
function useRefreshAuth() {
|
483
|
+
const context = useContext5(AuthContext);
|
484
|
+
if (context === void 0) {
|
485
|
+
throw new Error("useRefreshAuth must be used within an AuthProvider");
|
486
|
+
}
|
487
|
+
const { refreshAuthInfo } = context;
|
488
|
+
return refreshAuthInfo;
|
489
|
+
}
|
490
|
+
export {
|
491
|
+
AuthProvider,
|
492
|
+
RedirectToLogin,
|
493
|
+
RedirectToSignup,
|
494
|
+
useHostedPageUrls,
|
495
|
+
useLogoutFunction,
|
496
|
+
useRedirectFunctions,
|
497
|
+
useRefreshAuth,
|
498
|
+
useUser
|
499
|
+
};
|
500
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/client/AuthProvider.tsx","../../src/user.ts","../../src/client/utils.ts","../../src/client/useUser.tsx","../../src/client/useHostedPageUrls.tsx","../../src/client/useLogoutFunction.ts","../../src/client/useRedirectFunctions.tsx","../../src/client/useRefreshAuth.ts"],"sourcesContent":["'use client'\n\nimport React, {useCallback, useEffect, useReducer} from \"react\"\nimport {User} from \"../user\"\nimport {doesLocalStorageMatch, hasWindow, isEqual, saveUserToLocalStorage, USER_INFO_KEY} from \"./utils\";\nimport {useRouter} from \"next/navigation\";\n\ninterface InternalAuthState {\n loading: boolean\n user?: User\n\n logout: () => Promise<void>\n\n redirectToLoginPage: () => void\n redirectToSignupPage: () => void\n redirectToAccountPage: () => void\n redirectToOrgPage: (orgId?: string) => void\n redirectToCreateOrgPage: () => void\n redirectToSetupSAMLPage: (orgId: string) => void\n\n getSignupPageUrl(): string\n\n getLoginPageUrl(): string\n\n getAccountPageUrl(): string\n\n getOrgPageUrl(orgId?: string): string\n\n getCreateOrgPageUrl(): string\n\n getSetupSAMLPageUrl(orgId: string): string\n\n refreshAuthInfo: () => Promise<User | undefined>\n}\n\nexport type AuthProviderProps = {\n authUrl: string\n children?: React.ReactNode\n}\n\nexport const AuthContext = React.createContext<InternalAuthState | undefined>(undefined)\n\ntype AuthState = {\n loading: boolean\n user?: User\n\n // There's no good way to trigger server components to reload outside of router.refresh()\n // This is our workaround until the app router has something better\n authChangeDetected: boolean\n}\n\nconst initialAuthState = {\n loading: true,\n user: undefined,\n authChangeDetected: false,\n}\n\ntype AuthStateAction = {\n user?: User\n}\n\nfunction authStateReducer(_state: AuthState, action: AuthStateAction): AuthState {\n const authChangeDetected = !_state.loading && !isEqual(action.user, _state.user)\n\n if (!action.user) {\n return {\n loading: false,\n user: undefined,\n authChangeDetected,\n }\n } else if (_state.loading) {\n return {\n loading: false,\n user: action.user,\n authChangeDetected,\n }\n } else {\n return {\n loading: false,\n user: action.user,\n authChangeDetected\n }\n }\n}\n\nexport const AuthProvider = (props: AuthProviderProps) => {\n const [authState, dispatchInner] = useReducer(authStateReducer, initialAuthState)\n const router = useRouter()\n\n const dispatch = useCallback((action: AuthStateAction) => {\n dispatchInner(action)\n saveUserToLocalStorage(action.user)\n }, [dispatchInner])\n\n // This is because we don't have a good way to trigger server components to reload outside of router.refresh()\n // Once server actions isn't alpha, we can hopefully use that instead\n useEffect(() => {\n if (authState.authChangeDetected) {\n router.refresh()\n }\n }, [authState.authChangeDetected, router])\n\n // Trigger an initial refresh\n useEffect(() => {\n let didCancel = false\n\n async function refreshAuthInfo() {\n const {user} = await apiGetUserInfo()\n if (!didCancel) {\n dispatch({user})\n }\n }\n\n refreshAuthInfo()\n return () => {\n didCancel = true\n }\n }, [])\n\n\n // Periodically refresh the token\n useEffect(() => {\n let didCancel = false\n\n async function refreshToken() {\n const {user} = await apiGetUserInfo()\n if (!didCancel) {\n dispatch({user})\n }\n }\n\n async function onStorageEvent(event: StorageEvent) {\n if (event.key === USER_INFO_KEY && !doesLocalStorageMatch(event.newValue, authState.user)) {\n await refreshToken()\n }\n }\n\n // TODO: Retry logic if the request fails\n const interval = setInterval(refreshToken, 5 * 60 * 1000)\n\n if (hasWindow()) {\n window.addEventListener(\"storage\", onStorageEvent)\n window.addEventListener(\"online\", refreshToken)\n window.addEventListener(\"focus\", refreshToken)\n }\n\n return () => {\n didCancel = true\n clearInterval(interval)\n if (hasWindow()) {\n window.removeEventListener(\"storage\", onStorageEvent)\n window.removeEventListener(\"online\", refreshToken)\n window.removeEventListener(\"focus\", refreshToken)\n }\n }\n }, [dispatch, authState.user])\n\n\n const logout = useCallback(async () => {\n await fetch(\"/api/auth/logout\", {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n credentials: \"include\",\n })\n dispatch({user: undefined})\n }, [dispatch])\n\n const getLoginPageUrl = () => \"/api/auth/login\"\n const getSignupPageUrl = () => \"/api/auth/signup\"\n const getAccountPageUrl = useCallback(() => {\n return `${props.authUrl}/account`\n }, [props.authUrl])\n const getOrgPageUrl = useCallback(\n (orgId?: string) => {\n if (orgId) {\n return `${props.authUrl}/org?id=${orgId}`\n } else {\n return `${props.authUrl}/org`\n }\n },\n [props.authUrl]\n )\n const getCreateOrgPageUrl = useCallback(() => {\n return `${props.authUrl}/create_org`\n }, [props.authUrl])\n\n const getSetupSAMLPageUrl = useCallback(\n (orgId: string) => {\n return `${props.authUrl}/saml?id=${orgId}`\n },\n [props.authUrl]\n )\n\n const redirectTo = (url: string) => {\n window.location.href = url\n }\n\n const redirectToLoginPage = () => redirectTo(getLoginPageUrl())\n const redirectToSignupPage = () => redirectTo(getSignupPageUrl())\n const redirectToAccountPage = () => redirectTo(getAccountPageUrl())\n const redirectToOrgPage = (orgId?: string) => redirectTo(getOrgPageUrl(orgId))\n const redirectToCreateOrgPage = () => redirectTo(getCreateOrgPageUrl())\n const redirectToSetupSAMLPage = (orgId: string) => redirectTo(getSetupSAMLPageUrl(orgId))\n\n const refreshAuthInfo = async () => {\n const {user} = await apiGetUserInfo()\n dispatch({user})\n return user\n }\n\n const value = {\n loading: authState.loading,\n user: authState.user,\n logout,\n redirectToLoginPage,\n redirectToSignupPage,\n redirectToAccountPage,\n redirectToOrgPage,\n redirectToCreateOrgPage,\n redirectToSetupSAMLPage,\n getLoginPageUrl,\n getSignupPageUrl,\n getAccountPageUrl,\n getOrgPageUrl,\n getCreateOrgPageUrl,\n getSetupSAMLPageUrl,\n refreshAuthInfo,\n }\n return <AuthContext.Provider value={value}>{props.children}</AuthContext.Provider>\n}\n\ntype UserInfoResponse = { user?: User }\n\nasync function apiGetUserInfo(): Promise<UserInfoResponse> {\n try {\n const userInfoResponse = await fetch(\"/api/auth/userinfo\", {\n method: \"GET\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n credentials: \"include\",\n })\n\n if (userInfoResponse.ok) {\n const userJson = await userInfoResponse.text()\n const user = User.fromJSON(userJson)\n return {user}\n } else if (userInfoResponse.status === 401) {\n return {user: undefined}\n } else {\n console.log(\"Failed to refresh token\", userInfoResponse)\n }\n } catch (e) {\n console.log(\"Failed to refresh token\", e)\n }\n throw new Error(\"Failed to refresh token\")\n}\n","export class User {\n public userId: string\n public orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo\n\n // Metadata about the user\n public email: string\n public firstName?: string\n public lastName?: string\n public username?: string\n\n // If you used our migration APIs to migrate this user from a different system,\n // this is their original ID from that system.\n public legacyUserId?: string\n public impersonatorUserId?: string\n\n constructor(\n userId: string,\n email: string,\n orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo,\n firstName?: string,\n lastName?: string,\n username?: string,\n legacyUserId?: string,\n impersonatorUserId?: string\n ) {\n this.userId = userId\n this.orgIdToOrgMemberInfo = orgIdToOrgMemberInfo\n\n this.email = email\n this.firstName = firstName\n this.lastName = lastName\n this.username = username\n\n this.legacyUserId = legacyUserId\n this.impersonatorUserId = impersonatorUserId\n }\n\n public getOrg(orgId: string): OrgMemberInfo | undefined {\n if (!this.orgIdToOrgMemberInfo) {\n return undefined\n }\n\n return this.orgIdToOrgMemberInfo[orgId]\n }\n\n public getOrgByName(orgName: string): OrgMemberInfo | undefined {\n if (!this.orgIdToOrgMemberInfo) {\n return undefined\n }\n\n const urlSafeOrgName = orgName.toLowerCase().replace(/ /g, \"-\")\n for (const orgId in this.orgIdToOrgMemberInfo) {\n const orgMemberInfo = this.orgIdToOrgMemberInfo[orgId]\n if (orgMemberInfo.urlSafeOrgName === urlSafeOrgName) {\n return orgMemberInfo\n }\n }\n\n return undefined\n }\n\n public getOrgs(): OrgMemberInfo[] {\n if (!this.orgIdToOrgMemberInfo) {\n return []\n }\n\n return Object.values(this.orgIdToOrgMemberInfo)\n }\n\n public isImpersonating(): boolean {\n return !!this.impersonatorUserId\n }\n\n public static fromJSON(json: string): User {\n const obj = JSON.parse(json)\n const orgIdToOrgMemberInfo: OrgIdToOrgMemberInfo = {}\n for (const orgId in obj.orgIdToOrgMemberInfo) {\n orgIdToOrgMemberInfo[orgId] = OrgMemberInfo.fromJSON(\n JSON.stringify(obj.orgIdToOrgMemberInfo[orgId])\n )\n }\n return new User(\n obj.userId,\n obj.email,\n orgIdToOrgMemberInfo,\n obj.firstName,\n obj.lastName,\n obj.username,\n obj.legacyUserId,\n obj.impersonatorUserId\n )\n }\n}\n\nexport type OrgIdToOrgMemberInfo = {\n [orgId: string]: OrgMemberInfo\n}\n\nexport class OrgMemberInfo {\n public orgId: string\n public orgName: string\n public orgMetadata: { [key: string]: any }\n public urlSafeOrgName: string\n\n private userAssignedRole: string\n private userInheritedRolesPlusCurrentRole: string[]\n private userPermissions: string[]\n\n constructor(\n orgId: string,\n orgName: string,\n orgMetadata: { [key: string]: any },\n urlSafeOrgName: string,\n userAssignedRole: string,\n userInheritedRolesPlusCurrentRole: string[],\n userPermissions: string[]\n ) {\n this.orgId = orgId\n this.orgName = orgName\n this.orgMetadata = orgMetadata\n this.urlSafeOrgName = urlSafeOrgName\n\n this.userAssignedRole = userAssignedRole\n this.userInheritedRolesPlusCurrentRole = userInheritedRolesPlusCurrentRole\n this.userPermissions = userPermissions\n }\n\n // validation methods\n\n public isRole(role: string): boolean {\n return this.userAssignedRole === role\n }\n\n public isAtLeastRole(role: string): boolean {\n return this.userInheritedRolesPlusCurrentRole.includes(role)\n }\n\n public hasPermission(permission: string): boolean {\n return this.userPermissions.includes(permission)\n }\n\n public hasAllPermissions(permissions: string[]): boolean {\n return permissions.every((permission) => this.hasPermission(permission))\n }\n\n public static fromJSON(json: string): OrgMemberInfo {\n const obj = JSON.parse(json)\n return new OrgMemberInfo(\n obj.orgId,\n obj.orgName,\n obj.orgMetadata,\n obj.urlSafeOrgName,\n obj.userAssignedRole,\n obj.userInheritedRolesPlusCurrentRole,\n obj.userPermissions\n )\n }\n\n // getters for the private fields\n\n get assignedRole(): string {\n return this.userAssignedRole\n }\n\n get inheritedRolesPlusCurrentRole(): string[] {\n return this.userInheritedRolesPlusCurrentRole\n }\n\n get permissions(): string[] {\n return this.userPermissions\n }\n}\n\n// These Internal types exist since the server returns snake case, but typescript/javascript\n// convention is camelCase.\nexport type InternalOrgMemberInfo = {\n org_id: string\n org_name: string\n org_metadata: { [key: string]: any }\n url_safe_org_name: string\n user_role: string\n inherited_user_roles_plus_current_role: string[]\n user_permissions: string[]\n}\nexport type InternalUser = {\n user_id: string\n org_id_to_org_member_info?: { [org_id: string]: InternalOrgMemberInfo }\n\n email: string\n first_name?: string\n last_name?: string\n username?: string\n\n // If you used our migration APIs to migrate this user from a different system, this is their original ID from that system.\n legacy_user_id?: string\n impersonatorUserId?: string\n}\n\nexport function toUser(snake_case: InternalUser): User {\n return new User(\n snake_case.user_id,\n snake_case.email,\n toOrgIdToOrgMemberInfo(snake_case.org_id_to_org_member_info),\n snake_case.first_name,\n snake_case.last_name,\n snake_case.username,\n snake_case.legacy_user_id,\n snake_case.impersonatorUserId\n )\n}\n\nexport function toOrgIdToOrgMemberInfo(snake_case?: {\n [org_id: string]: InternalOrgMemberInfo\n}): OrgIdToOrgMemberInfo | undefined {\n if (snake_case === undefined) {\n return undefined\n }\n const camelCase: OrgIdToOrgMemberInfo = {}\n\n for (const key of Object.keys(snake_case)) {\n const snakeCaseValue = snake_case[key]\n if (snakeCaseValue) {\n camelCase[key] = new OrgMemberInfo(\n snakeCaseValue.org_id,\n snakeCaseValue.org_name,\n snakeCaseValue.org_metadata,\n snakeCaseValue.url_safe_org_name,\n snakeCaseValue.user_role,\n snakeCaseValue.inherited_user_roles_plus_current_role,\n snakeCaseValue.user_permissions\n )\n }\n }\n\n return camelCase\n}\n","import {User} from \"../user\";\n\nexport const USER_INFO_KEY = \"__PROPEL_AUTH_USER_INFO\"\n\nexport function hasWindow(): boolean {\n return typeof window !== \"undefined\"\n}\n\nexport function saveUserToLocalStorage(user: User | undefined) {\n if (user) {\n localStorage.setItem(USER_INFO_KEY, JSON.stringify(user))\n } else {\n localStorage.setItem(USER_INFO_KEY, \"{}\")\n }\n}\n\nexport function doesLocalStorageMatch(newValue: string | null, user: User | undefined): boolean {\n if (!newValue) {\n return false\n } else if (!user) {\n return newValue === \"{}\"\n }\n\n const parsed = JSON.parse(newValue)\n if (!parsed) {\n return false\n }\n\n return isEqual(parsed, user)\n}\n\n\nexport function isEqual(a: any, b: any): boolean {\n if (typeof a !== typeof b) {\n return false\n }\n\n if (Array.isArray(a) !== Array.isArray(b)) {\n return false\n }\n\n if (Array.isArray(a)) {\n const aArray = a as any[]\n const bArray = b as any[]\n if (aArray.length !== bArray.length) {\n return false\n }\n\n for (let i = 0; i < aArray.length; i++) {\n if (!isEqual(aArray[i], bArray[i])) {\n return false\n }\n }\n\n return true\n }\n\n if (typeof a === \"object\") {\n const aKeys = Object.keys(a)\n const bKeys = Object.keys(b)\n if (aKeys.length !== bKeys.length) {\n return false\n }\n\n for (const key of aKeys) {\n if (!isEqual(a[key], b[key])) {\n return false\n }\n }\n\n return true\n } else {\n return a === b\n }\n}","'use client'\n\nimport {useContext} from \"react\"\nimport {User} from \"../user\"\nimport {AuthContext} from \"./AuthProvider\"\n\nexport type UseUserLoading = {\n loading: true\n isLoggedIn: never\n user: never\n}\n\nexport type UseUserLoggedIn = {\n loading: false\n isLoggedIn: true\n user: User\n}\n\nexport type UseUserNotLoggedIn = {\n loading: false\n isLoggedIn: false\n user: undefined\n}\n\nexport type UseUser = UseUserLoading | UseUserLoggedIn | UseUserNotLoggedIn\n\nexport function useUser(): UseUser {\n const context = useContext(AuthContext)\n if (context === undefined) {\n throw new Error(\"useUser must be used within an AuthProvider\")\n }\n\n const {loading, user} = context\n if (loading) {\n return {\n loading: true,\n isLoggedIn: undefined as never,\n user: undefined as never,\n }\n } else if (user) {\n return {\n loading: false,\n isLoggedIn: true,\n user,\n }\n } else {\n return {\n loading: false,\n isLoggedIn: false,\n user: undefined,\n }\n }\n}","import { useContext } from \"react\"\nimport { AuthContext } from \"./AuthProvider\"\n\nexport function useHostedPageUrls() {\n const context = useContext(AuthContext)\n if (context === undefined) {\n throw new Error(\"useHostedPageUrls must be used within an AuthProvider\")\n }\n const {\n getLoginPageUrl,\n getSignupPageUrl,\n getAccountPageUrl,\n getOrgPageUrl,\n getCreateOrgPageUrl,\n getSetupSAMLPageUrl,\n } = context\n return {\n getLoginPageUrl,\n getSignupPageUrl,\n getAccountPageUrl,\n getOrgPageUrl,\n getCreateOrgPageUrl,\n getSetupSAMLPageUrl,\n }\n}\n","import { useContext } from \"react\"\nimport { AuthContext } from \"./AuthProvider\"\n\nexport function useLogoutFunction() {\n const context = useContext(AuthContext)\n if (context === undefined) {\n throw new Error(\"useLogoutFunction must be used within an AuthProvider\")\n }\n const { logout } = context\n return logout\n}\n","import React, { useContext, useEffect } from \"react\"\nimport { AuthContext } from \"./AuthProvider\"\n\nexport function useRedirectFunctions() {\n const context = useContext(AuthContext)\n if (context === undefined) {\n throw new Error(\"useRedirectFunctions must be used within an AuthProvider\")\n }\n const {\n redirectToAccountPage,\n redirectToSignupPage,\n redirectToLoginPage,\n redirectToOrgPage,\n redirectToCreateOrgPage,\n } = context\n return {\n redirectToSignupPage,\n redirectToLoginPage,\n redirectToAccountPage,\n redirectToOrgPage,\n redirectToCreateOrgPage,\n }\n}\n\nexport interface RedirectProps {\n children?: React.ReactNode\n}\n\nexport function RedirectToSignup({ children }: RedirectProps) {\n const { redirectToSignupPage } = useRedirectFunctions()\n\n useEffect(() => {\n redirectToSignupPage()\n }, [])\n\n return <>{children}</>\n}\n\nexport function RedirectToLogin({ children }: RedirectProps) {\n const { redirectToLoginPage } = useRedirectFunctions()\n useEffect(() => {\n redirectToLoginPage()\n }, [])\n return <>{children}</>\n}\n","import { useContext } from \"react\"\nimport { AuthContext } from \"./AuthProvider\"\n\nexport function useRefreshAuth() {\n const context = useContext(AuthContext)\n if (context === undefined) {\n throw new Error(\"useRefreshAuth must be used within an AuthProvider\")\n }\n const { refreshAuthInfo } = context\n return refreshAuthInfo\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAEA,OAAO,SAAQ,aAAa,WAAW,kBAAiB;;;ACFjD,IAAM,OAAN,MAAW;AAAA,EAed,YACI,QACA,OACA,sBACA,WACA,UACA,UACA,cACA,oBACF;AACE,SAAK,SAAS;AACd,SAAK,uBAAuB;AAE5B,SAAK,QAAQ;AACb,SAAK,YAAY;AACjB,SAAK,WAAW;AAChB,SAAK,WAAW;AAEhB,SAAK,eAAe;AACpB,SAAK,qBAAqB;AAAA,EAC9B;AAAA,EAEO,OAAO,OAA0C;AACpD,QAAI,CAAC,KAAK,sBAAsB;AAC5B,aAAO;AAAA,IACX;AAEA,WAAO,KAAK,qBAAqB,KAAK;AAAA,EAC1C;AAAA,EAEO,aAAa,SAA4C;AAC5D,QAAI,CAAC,KAAK,sBAAsB;AAC5B,aAAO;AAAA,IACX;AAEA,UAAM,iBAAiB,QAAQ,YAAY,EAAE,QAAQ,MAAM,GAAG;AAC9D,eAAW,SAAS,KAAK,sBAAsB;AAC3C,YAAM,gBAAgB,KAAK,qBAAqB,KAAK;AACrD,UAAI,cAAc,mBAA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WAAU,MAAM;AACZ,yBAAqB;AAAA,EACzB,GAAG,CAAC,CAAC;AAEL,SAAO,gBAAAC,OAAA,cAAAA,OAAA,gBAAG,QAAS;AACvB;AAEO,SAAS,gBAAgB,EAAE,SAAS,GAAkB;AACzD,QAAM,EAAE,oBAAoB,IAAI,qBAAqB;AACrD,EAAAD,WAAU,MAAM;AACZ,wBAAoB;AAAA,EACxB,GAAG,CAAC,CAAC;AACL,SAAO,gBAAAC,OAAA,cAAAA,OAAA,gBAAG,QAAS;AACvB;;;AC5CA,SAAS,cAAAC,mBAAkB;AAGpB,SAAS,iBAAiB;AAC7B,QAAM,UAAUC,YAAW,WAAW;AACtC,MAAI,YAAY,QAAW;AACvB,UAAM,IAAI,MAAM,oDAAoD;AAAA,EACxE;AACA,QAAM,EAAE,gBAAgB,IAAI;AAC5B,SAAO;AACX;","names":["refreshAuthInfo","useContext","useContext","useContext","useContext","React","useContext","useEffect","useContext","useEffect","React","useContext","useContext"]}
|
@@ -0,0 +1,94 @@
|
|
1
|
+
import { NextRequest } from 'next/server';
|
2
|
+
|
3
|
+
declare class User {
|
4
|
+
userId: string;
|
5
|
+
orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo;
|
6
|
+
email: string;
|
7
|
+
firstName?: string;
|
8
|
+
lastName?: string;
|
9
|
+
username?: string;
|
10
|
+
legacyUserId?: string;
|
11
|
+
impersonatorUserId?: string;
|
12
|
+
constructor(userId: string, email: string, orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo, firstName?: string, lastName?: string, username?: string, legacyUserId?: string, impersonatorUserId?: string);
|
13
|
+
getOrg(orgId: string): OrgMemberInfo | undefined;
|
14
|
+
getOrgByName(orgName: string): OrgMemberInfo | undefined;
|
15
|
+
getOrgs(): OrgMemberInfo[];
|
16
|
+
isImpersonating(): boolean;
|
17
|
+
static fromJSON(json: string): User;
|
18
|
+
}
|
19
|
+
type OrgIdToOrgMemberInfo = {
|
20
|
+
[orgId: string]: OrgMemberInfo;
|
21
|
+
};
|
22
|
+
declare class OrgMemberInfo {
|
23
|
+
orgId: string;
|
24
|
+
orgName: string;
|
25
|
+
orgMetadata: {
|
26
|
+
[key: string]: any;
|
27
|
+
};
|
28
|
+
urlSafeOrgName: string;
|
29
|
+
private userAssignedRole;
|
30
|
+
private userInheritedRolesPlusCurrentRole;
|
31
|
+
private userPermissions;
|
32
|
+
constructor(orgId: string, orgName: string, orgMetadata: {
|
33
|
+
[key: string]: any;
|
34
|
+
}, urlSafeOrgName: string, userAssignedRole: string, userInheritedRolesPlusCurrentRole: string[], userPermissions: string[]);
|
35
|
+
isRole(role: string): boolean;
|
36
|
+
isAtLeastRole(role: string): boolean;
|
37
|
+
hasPermission(permission: string): boolean;
|
38
|
+
hasAllPermissions(permissions: string[]): boolean;
|
39
|
+
static fromJSON(json: string): OrgMemberInfo;
|
40
|
+
get assignedRole(): string;
|
41
|
+
get inheritedRolesPlusCurrentRole(): string[];
|
42
|
+
get permissions(): string[];
|
43
|
+
}
|
44
|
+
|
45
|
+
declare class UnauthorizedException extends Error {
|
46
|
+
readonly message: string;
|
47
|
+
readonly status: number;
|
48
|
+
constructor(message: string);
|
49
|
+
}
|
50
|
+
declare class ConfigurationException extends Error {
|
51
|
+
readonly message: string;
|
52
|
+
readonly status: number;
|
53
|
+
constructor(message: string);
|
54
|
+
}
|
55
|
+
|
56
|
+
type ServerActions = {
|
57
|
+
getUser: () => Promise<User | undefined>;
|
58
|
+
getUserOrRedirect: () => Promise<User>;
|
59
|
+
validateAccessToken: (accessToken: string) => Promise<User>;
|
60
|
+
validateAccessTokenOrUndefined: (accessToken: string) => Promise<User | undefined>;
|
61
|
+
getRouteHandler: (req: NextRequest, { params }: {
|
62
|
+
params: {
|
63
|
+
slug: string;
|
64
|
+
};
|
65
|
+
}) => Response | Promise<Response>;
|
66
|
+
postRouteHandler: (req: NextRequest, { params }: {
|
67
|
+
params: {
|
68
|
+
slug: string;
|
69
|
+
};
|
70
|
+
}) => Response | Promise<Response>;
|
71
|
+
authMiddleware: (req: NextRequest) => Promise<Response>;
|
72
|
+
};
|
73
|
+
|
74
|
+
type InitializeAuthOptions = {
|
75
|
+
authUrl: string;
|
76
|
+
redirectUri: string;
|
77
|
+
integrationApiKey: string;
|
78
|
+
verifierKey: string;
|
79
|
+
postLoginRedirectPathFn?: (user: User) => string;
|
80
|
+
};
|
81
|
+
declare function initializeAuth(opts: InitializeAuthOptions): ServerActions;
|
82
|
+
|
83
|
+
type LoggedInProps = {
|
84
|
+
loading: false;
|
85
|
+
isLoggedIn: true;
|
86
|
+
user: User;
|
87
|
+
};
|
88
|
+
type LoggedOutProps = {
|
89
|
+
loading: false;
|
90
|
+
isLoggedIn: false;
|
91
|
+
user: undefined;
|
92
|
+
};
|
93
|
+
|
94
|
+
export { ConfigurationException, InitializeAuthOptions, LoggedInProps, LoggedOutProps, OrgIdToOrgMemberInfo, OrgMemberInfo, ServerActions, UnauthorizedException, User, initializeAuth };
|