@frontegg/nextjs 6.7.8 → 6.7.9-alpha.3741489660
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/CHANGELOG.md +4 -0
- package/FronteggBaseProvider-5529ded6.js +119 -0
- package/FronteggBaseProvider-5529ded6.js.map +1 -0
- package/FronteggClientProviderNext13-65bb4cd0.js +19 -0
- package/FronteggClientProviderNext13-65bb4cd0.js.map +1 -0
- package/FronteggConfig-7ae86562.js +123 -0
- package/FronteggConfig-7ae86562.js.map +1 -0
- package/FronteggProvider.d.ts +3 -12
- package/FronteggProviderNoSSR.d.ts +2 -4
- package/README.md +56 -4
- package/api-70f0e81e.js +58 -0
- package/api-70f0e81e.js.map +1 -0
- package/client/FronteggAppRouter.d.ts +10 -0
- package/client/FronteggClientProviderNext13.d.ts +3 -0
- package/client/index.d.ts +2 -0
- package/client/index.js +63 -0
- package/client/index.js.map +1 -0
- package/{FronteggConfig.d.ts → common/FronteggConfig.d.ts} +1 -1
- package/{api.d.ts → common/api.d.ts} +0 -0
- package/{AppContext.d.ts → common/client/AppContext.d.ts} +0 -0
- package/common/client/ExpireInListener.d.ts +1 -0
- package/common/client/FronteggBaseProvider.d.ts +3 -0
- package/common/client/createOrGetFronteggApp.d.ts +11 -0
- package/common/client/hooks/index.d.ts +1 -0
- package/common/client/hooks/useRequestAuthorizeSSR.d.ts +9 -0
- package/common/client/index.d.ts +4 -0
- package/{consts.d.ts → common/consts.d.ts} +0 -0
- package/common/getMeAndTenants.d.ts +2 -0
- package/{helpers.d.ts → common/helpers.d.ts} +7 -9
- package/common/index.d.ts +6 -0
- package/common/index.js +12 -0
- package/common/index.js.map +1 -0
- package/common/types.d.ts +74 -0
- package/common/utils/createGetSession.d.ts +7 -0
- package/common/utils/getCookieFromRequest.d.ts +2 -0
- package/common/utils/index.d.ts +3 -0
- package/common/utils/parseCookie.d.ts +1 -0
- package/consts-873fdf4e.js +9 -0
- package/consts-873fdf4e.js.map +1 -0
- package/createGetSession-81b1748e.js +50 -0
- package/createGetSession-81b1748e.js.map +1 -0
- package/edge/getSessionOnEdge.d.ts +2 -0
- package/edge/index.d.ts +1 -0
- package/edge/index.js +29 -0
- package/edge/index.js.map +1 -0
- package/getCookieFromRequest-624c37b5.js +28 -0
- package/getCookieFromRequest-624c37b5.js.map +1 -0
- package/helpers-dbf29bd5.js +189 -0
- package/helpers-dbf29bd5.js.map +1 -0
- package/index.d.ts +1 -2
- package/index.js +599 -0
- package/index.js.map +1 -0
- package/package.json +16 -10
- package/refreshToken.d.ts +3 -0
- package/server/FronteggProviderNext13.d.ts +3 -0
- package/server/getSessionNext13.d.ts +4 -0
- package/server/index.d.ts +2 -0
- package/server/index.js +87 -0
- package/server/index.js.map +1 -0
- package/session.d.ts +4 -8
- package/withFronteggApp.d.ts +10 -8
- package/index.cjs.js +0 -2747
- package/index.esm.js +0 -2691
- package/types.d.ts +0 -53
package/index.js
ADDED
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
import { __rest, __assign, __spreadArrays, __awaiter, __generator } from 'tslib';
|
|
2
|
+
import { useRouter } from 'next/router';
|
|
3
|
+
import React, { useCallback, useMemo, useEffect, useContext } from 'react';
|
|
4
|
+
import { F as FronteggBaseProvider, E as ExpireInListener, A as AppContext } from './FronteggBaseProvider-5529ded6.js';
|
|
5
|
+
import { AppHolder, initialize } from '@frontegg/js';
|
|
6
|
+
export { AdminPortal } from '@frontegg/js';
|
|
7
|
+
import { authInitialState } from '@frontegg/redux-store';
|
|
8
|
+
import { ContextHolder, fronteggSilentRefreshTokenUrl, fronteggRefreshTokenUrl, fronteggAuthApiRoutes } from '@frontegg/rest-api';
|
|
9
|
+
import { FronteggStoreProvider, useAuthUserOrNull, useLoginWithRedirect, useLoginActions } from '@frontegg/react-hooks';
|
|
10
|
+
export * from '@frontegg/react-hooks';
|
|
11
|
+
import { F as FronteggConfig } from './FronteggConfig-7ae86562.js';
|
|
12
|
+
export { F as FronteggConfig } from './FronteggConfig-7ae86562.js';
|
|
13
|
+
import './api-70f0e81e.js';
|
|
14
|
+
import 'cookie';
|
|
15
|
+
import 'iron-session';
|
|
16
|
+
import 'jose';
|
|
17
|
+
import { parse } from 'url';
|
|
18
|
+
import { g as getTokensFromCookie, c as createCookie, r as rewriteCookieProperty, a as createSessionFromAccessToken, b as removeCookies, d as getMeAndTenants, m as modifySetCookieIfUnsecure, e as addToCookies } from './helpers-dbf29bd5.js';
|
|
19
|
+
export { e as addToCookies, c as createCookie, a as createSessionFromAccessToken, d as getMeAndTenants, g as getTokensFromCookie, m as modifySetCookieIfUnsecure, p as parseCookieFromArray, b as removeCookies, r as rewriteCookieProperty } from './helpers-dbf29bd5.js';
|
|
20
|
+
import { f as fronteggErrors } from './consts-873fdf4e.js';
|
|
21
|
+
export { f as fronteggErrors } from './consts-873fdf4e.js';
|
|
22
|
+
import { g as getCookieFromRequest } from './getCookieFromRequest-624c37b5.js';
|
|
23
|
+
export { g as getCookieFromRequest, p as parseCookie } from './getCookieFromRequest-624c37b5.js';
|
|
24
|
+
import { c as createGetSession } from './createGetSession-81b1748e.js';
|
|
25
|
+
export { c as createGetSession } from './createGetSession-81b1748e.js';
|
|
26
|
+
import httpProxy from 'http-proxy';
|
|
27
|
+
export * from '@frontegg/types';
|
|
28
|
+
|
|
29
|
+
var FronteggProvider = function (_a) {
|
|
30
|
+
var children = _a.children, basename = _a.basename, props = __rest(_a, ["children", "basename"]);
|
|
31
|
+
var router = useRouter();
|
|
32
|
+
var baseName = basename !== null && basename !== void 0 ? basename : router.basePath;
|
|
33
|
+
return (React.createElement(FronteggBaseProvider, __assign({ router: router, basename: baseName }, props), children));
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
var Connector = function (_props) {
|
|
37
|
+
var _a;
|
|
38
|
+
var router = _props.router, appName = _props.appName, hostedLoginBox = _props.hostedLoginBox; _props.customLoginBox; var props = __rest(_props, ["router", "appName", "hostedLoginBox", "customLoginBox"]);
|
|
39
|
+
var isSSR = typeof window === 'undefined';
|
|
40
|
+
var baseName = (_a = props.basename) !== null && _a !== void 0 ? _a : router.basePath;
|
|
41
|
+
var onRedirectTo = useCallback(function (_path, opts) {
|
|
42
|
+
var path = _path;
|
|
43
|
+
if (path.startsWith(baseName)) {
|
|
44
|
+
path = path.substring(baseName.length);
|
|
45
|
+
}
|
|
46
|
+
if (opts === null || opts === void 0 ? void 0 : opts.preserveQueryParams) {
|
|
47
|
+
path = "" + path + window.location.search;
|
|
48
|
+
}
|
|
49
|
+
if ((opts === null || opts === void 0 ? void 0 : opts.refresh) && !isSSR) {
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
window.Cypress ? router.push(path) : (window.location.href = path);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
(opts === null || opts === void 0 ? void 0 : opts.replace) ? router.replace(path) : router.push(path);
|
|
55
|
+
}
|
|
56
|
+
}, []);
|
|
57
|
+
var app = useMemo(function () {
|
|
58
|
+
var _a;
|
|
59
|
+
var createdApp;
|
|
60
|
+
try {
|
|
61
|
+
createdApp = AppHolder.getInstance(appName !== null && appName !== void 0 ? appName : 'default');
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
createdApp = initialize(__assign(__assign({}, props), { hostedLoginBox: hostedLoginBox !== null && hostedLoginBox !== void 0 ? hostedLoginBox : false, basename: (_a = props.basename) !== null && _a !== void 0 ? _a : baseName, authOptions: __assign(__assign({}, props.authOptions), { onRedirectTo: onRedirectTo }), contextOptions: __assign({ requestCredentials: 'include' }, props.contextOptions), onRedirectTo: onRedirectTo }), appName !== null && appName !== void 0 ? appName : 'default');
|
|
65
|
+
}
|
|
66
|
+
return createdApp;
|
|
67
|
+
}, [appName, props, hostedLoginBox, baseName, onRedirectTo]);
|
|
68
|
+
ContextHolder.setOnRedirectTo(onRedirectTo);
|
|
69
|
+
useEffect(function () {
|
|
70
|
+
app.store.dispatch({ type: 'auth/requestAuthorize', payload: true });
|
|
71
|
+
}, [app]);
|
|
72
|
+
return (React.createElement(AppContext.Provider, { value: app },
|
|
73
|
+
React.createElement(FronteggStoreProvider, __assign({}, __assign(__assign({}, props), { app: app })), props.children)));
|
|
74
|
+
};
|
|
75
|
+
var FronteggNextJSProvider = function (props) {
|
|
76
|
+
var router = useRouter();
|
|
77
|
+
return (React.createElement(Connector, __assign({}, props, { router: router }),
|
|
78
|
+
React.createElement(ExpireInListener, null),
|
|
79
|
+
props.children));
|
|
80
|
+
};
|
|
81
|
+
var FronteggProviderNoSSR = function (props) {
|
|
82
|
+
return (React.createElement(FronteggNextJSProvider, __assign({}, props, { framework: 'nextjs' }), props.children));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
var AuthorizedContent = function (props) {
|
|
86
|
+
var _a, _b;
|
|
87
|
+
var isAuthorized = true; // Initially
|
|
88
|
+
var user = useAuthUserOrNull();
|
|
89
|
+
if (!(user === null || user === void 0 ? void 0 : user.superUser)) {
|
|
90
|
+
if (props.requiredPermissions) {
|
|
91
|
+
if (!(user === null || user === void 0 ? void 0 : user.permissions) || (user === null || user === void 0 ? void 0 : user.permissions.length) === 0) {
|
|
92
|
+
isAuthorized = false;
|
|
93
|
+
}
|
|
94
|
+
var _loop_1 = function (permission) {
|
|
95
|
+
if (!((_a = user === null || user === void 0 ? void 0 : user.permissions) === null || _a === void 0 ? void 0 : _a.find(function (_a) {
|
|
96
|
+
var key = _a.key;
|
|
97
|
+
return key === permission;
|
|
98
|
+
}))) {
|
|
99
|
+
isAuthorized = false;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
for (var _i = 0, _c = props.requiredPermissions; _i < _c.length; _i++) {
|
|
103
|
+
var permission = _c[_i];
|
|
104
|
+
_loop_1(permission);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (props.requiredRoles) {
|
|
108
|
+
if (!(user === null || user === void 0 ? void 0 : user.roles) || (user === null || user === void 0 ? void 0 : user.roles.length) === 0) {
|
|
109
|
+
isAuthorized = false;
|
|
110
|
+
}
|
|
111
|
+
var _loop_2 = function (role) {
|
|
112
|
+
if (!((_b = user === null || user === void 0 ? void 0 : user.roles) === null || _b === void 0 ? void 0 : _b.find(function (_a) {
|
|
113
|
+
var key = _a.key;
|
|
114
|
+
return key === role;
|
|
115
|
+
}))) {
|
|
116
|
+
isAuthorized = false;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
for (var _d = 0, _e = props.requiredRoles; _d < _e.length; _d++) {
|
|
120
|
+
var role = _e[_d];
|
|
121
|
+
_loop_2(role);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (typeof props.render === 'function') {
|
|
126
|
+
return React.createElement(React.Fragment, null, props.render(isAuthorized));
|
|
127
|
+
}
|
|
128
|
+
return isAuthorized ? React.createElement(React.Fragment, null, props.children) : null;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
function FronteggRouter() {
|
|
132
|
+
var app = useContext(AppContext);
|
|
133
|
+
var _a = useRouter(), query = _a.query, replace = _a.replace;
|
|
134
|
+
var loginWithRedirect = useLoginWithRedirect();
|
|
135
|
+
var logout = useLoginActions().logout;
|
|
136
|
+
useEffect(function () {
|
|
137
|
+
if (!app) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (app.options.hostedLoginBox) {
|
|
141
|
+
var routesObj = __assign(__assign({}, authInitialState.routes), FronteggConfig.authRoutes);
|
|
142
|
+
var _a = query, pathArr = _a["frontegg-router"], queryParams = __rest(_a, ['frontegg-router']);
|
|
143
|
+
var pathname = "/" + pathArr.join('/');
|
|
144
|
+
if (pathname === routesObj.loginUrl) {
|
|
145
|
+
if (queryParams.redirectUrl) {
|
|
146
|
+
localStorage.setItem('FRONTEGG_AFTER_AUTH_REDIRECT_URL', "" + window.location.origin + queryParams.redirectUrl);
|
|
147
|
+
}
|
|
148
|
+
loginWithRedirect();
|
|
149
|
+
}
|
|
150
|
+
else if (pathname === routesObj.logoutUrl) {
|
|
151
|
+
var _baseUrl = app.options.contextOptions.baseUrl;
|
|
152
|
+
var baseUrl_1 = typeof _baseUrl === 'string' ? _baseUrl : _baseUrl('');
|
|
153
|
+
logout(function () {
|
|
154
|
+
window.location.href = baseUrl_1 + "/oauth/logout?post_logout_redirect_uri=" + encodeURIComponent(window.location.origin);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}, [app, query, loginWithRedirect, logout, replace]);
|
|
159
|
+
return '';
|
|
160
|
+
}
|
|
161
|
+
function FronteggRouterProps(context) {
|
|
162
|
+
var _a;
|
|
163
|
+
var routesObj = __assign(__assign({}, authInitialState.routes), FronteggConfig.authRoutes);
|
|
164
|
+
var routesArr = Object.keys(routesObj).reduce(function (p, key) { return __spreadArrays(p, [routesObj[key]]); }, []);
|
|
165
|
+
var pathname = parse((_a = context.resolvedUrl) !== null && _a !== void 0 ? _a : context.req.url, true).pathname;
|
|
166
|
+
if (!pathname || pathname.startsWith('/_next/data')) {
|
|
167
|
+
var query = context.req.query[Object.keys(context.req.query)[0]];
|
|
168
|
+
pathname = "/" + (Array.isArray(query) ? query.join('/') : query);
|
|
169
|
+
}
|
|
170
|
+
var notFound = routesArr.indexOf(pathname) === -1;
|
|
171
|
+
if (FronteggConfig.fronteggAppOptions.hostedLoginBox) {
|
|
172
|
+
var notFound_1 = !(routesObj.loginUrl === pathname ||
|
|
173
|
+
routesObj.logoutUrl === pathname ||
|
|
174
|
+
routesObj.hostedLoginRedirectUrl === pathname);
|
|
175
|
+
return { notFound: notFound_1, props: {} };
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
notFound: notFound,
|
|
179
|
+
props: {},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
var getSession = function (req) {
|
|
184
|
+
return createGetSession({ getCookie: function () { return getCookieFromRequest(req); }, cookieResolver: getTokensFromCookie });
|
|
185
|
+
};
|
|
186
|
+
function withSSRSession(handler) {
|
|
187
|
+
var _this = this;
|
|
188
|
+
return function (context) { return __awaiter(_this, void 0, void 0, function () {
|
|
189
|
+
var session, loginUrl;
|
|
190
|
+
var _a, _b;
|
|
191
|
+
return __generator(this, function (_c) {
|
|
192
|
+
switch (_c.label) {
|
|
193
|
+
case 0: return [4 /*yield*/, getSession(context.req)];
|
|
194
|
+
case 1:
|
|
195
|
+
session = _c.sent();
|
|
196
|
+
if (session) {
|
|
197
|
+
return [2 /*return*/, handler(context, session)];
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
loginUrl = (_a = FronteggConfig.authRoutes.loginUrl) !== null && _a !== void 0 ? _a : authInitialState.routes.loginUrl;
|
|
201
|
+
if (!loginUrl.startsWith('/')) {
|
|
202
|
+
loginUrl = "/" + loginUrl;
|
|
203
|
+
}
|
|
204
|
+
return [2 /*return*/, {
|
|
205
|
+
redirect: {
|
|
206
|
+
permanent: false,
|
|
207
|
+
destination: loginUrl + "?redirectUrl=" + encodeURIComponent((_b = context.resolvedUrl) !== null && _b !== void 0 ? _b : context.req.url),
|
|
208
|
+
},
|
|
209
|
+
props: {},
|
|
210
|
+
}];
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}); };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function refreshTokenHostedLogin(ctx, headers) {
|
|
218
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
219
|
+
var sealFromCookies, tokens;
|
|
220
|
+
return __generator(this, function (_a) {
|
|
221
|
+
switch (_a.label) {
|
|
222
|
+
case 0:
|
|
223
|
+
_a.trys.push([0, 3, , 4]);
|
|
224
|
+
sealFromCookies = getCookieFromRequest(ctx === null || ctx === void 0 ? void 0 : ctx.req);
|
|
225
|
+
return [4 /*yield*/, getTokensFromCookie(sealFromCookies)];
|
|
226
|
+
case 1:
|
|
227
|
+
tokens = _a.sent();
|
|
228
|
+
if (!(tokens === null || tokens === void 0 ? void 0 : tokens.refreshToken)) {
|
|
229
|
+
return [2 /*return*/, null];
|
|
230
|
+
}
|
|
231
|
+
return [4 /*yield*/, fetch(process.env['FRONTEGG_BASE_URL'] + "/frontegg" + fronteggSilentRefreshTokenUrl, {
|
|
232
|
+
method: 'POST',
|
|
233
|
+
credentials: 'include',
|
|
234
|
+
body: JSON.stringify({
|
|
235
|
+
grant_type: 'refresh_token',
|
|
236
|
+
refresh_token: tokens.refreshToken,
|
|
237
|
+
}),
|
|
238
|
+
headers: {
|
|
239
|
+
'accept-encoding': headers['accept-encoding'],
|
|
240
|
+
'accept-language': headers['accept-language'],
|
|
241
|
+
cookie: headers['cookie'],
|
|
242
|
+
accept: headers['accept'],
|
|
243
|
+
'user-agent': headers['user-agent'],
|
|
244
|
+
connection: headers['connection'],
|
|
245
|
+
'cache-control': headers['cache-control'],
|
|
246
|
+
},
|
|
247
|
+
})];
|
|
248
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
249
|
+
case 3:
|
|
250
|
+
_a.sent();
|
|
251
|
+
return [2 /*return*/, null];
|
|
252
|
+
case 4: return [2 /*return*/];
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
function refreshTokenEmbedded(ctx, headers, cookies) {
|
|
258
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
259
|
+
var refreshTokenKey, cookieKey;
|
|
260
|
+
return __generator(this, function (_a) {
|
|
261
|
+
switch (_a.label) {
|
|
262
|
+
case 0:
|
|
263
|
+
refreshTokenKey = ("fe_refresh_" + FronteggConfig.clientId).replace(/-/g, '');
|
|
264
|
+
cookieKey = Object.keys(cookies).find(function (cookie) {
|
|
265
|
+
return cookie.replace(/-/g, '') === refreshTokenKey;
|
|
266
|
+
});
|
|
267
|
+
if (!cookieKey) {
|
|
268
|
+
// ctx.res?.setHeader('set-cookie', removedCookies);
|
|
269
|
+
// remove all fe_nextjs-session cookies
|
|
270
|
+
return [2 /*return*/, null];
|
|
271
|
+
}
|
|
272
|
+
return [4 /*yield*/, fetch(process.env['FRONTEGG_BASE_URL'] + "/frontegg" + fronteggRefreshTokenUrl, {
|
|
273
|
+
method: 'POST',
|
|
274
|
+
credentials: 'include',
|
|
275
|
+
body: '{}',
|
|
276
|
+
headers: {
|
|
277
|
+
'accept-encoding': headers['accept-encoding'],
|
|
278
|
+
'accept-language': headers['accept-language'],
|
|
279
|
+
cookie: headers['cookie'],
|
|
280
|
+
accept: headers['accept'],
|
|
281
|
+
'user-agent': headers['user-agent'],
|
|
282
|
+
connection: headers['connection'],
|
|
283
|
+
'cache-control': headers['cache-control'],
|
|
284
|
+
},
|
|
285
|
+
})];
|
|
286
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
function refreshToken(ctx) {
|
|
292
|
+
var _a;
|
|
293
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
294
|
+
var request, session, isSecured, headers, cookies, response, data, rewriteCookieDomainConfig, cookieHeader, newSetCookie, _b, session, decodedJwt, refreshToken_1, cookieValue;
|
|
295
|
+
var _c;
|
|
296
|
+
return __generator(this, function (_d) {
|
|
297
|
+
switch (_d.label) {
|
|
298
|
+
case 0:
|
|
299
|
+
_d.trys.push([0, 13, , 14]);
|
|
300
|
+
request = ctx.req;
|
|
301
|
+
if (!request) {
|
|
302
|
+
return [2 /*return*/, null];
|
|
303
|
+
}
|
|
304
|
+
_d.label = 1;
|
|
305
|
+
case 1:
|
|
306
|
+
_d.trys.push([1, 3, , 4]);
|
|
307
|
+
return [4 /*yield*/, getSession(ctx.req)];
|
|
308
|
+
case 2:
|
|
309
|
+
session = _d.sent();
|
|
310
|
+
if (session) {
|
|
311
|
+
return [2 /*return*/, session];
|
|
312
|
+
}
|
|
313
|
+
return [3 /*break*/, 4];
|
|
314
|
+
case 3:
|
|
315
|
+
_d.sent();
|
|
316
|
+
return [3 /*break*/, 4];
|
|
317
|
+
case 4:
|
|
318
|
+
isSecured = new URL(FronteggConfig.appUrl).protocol === 'https:';
|
|
319
|
+
headers = request.headers;
|
|
320
|
+
cookies = request.cookies;
|
|
321
|
+
if (ctx.req.url.startsWith('/oauth/callback')) {
|
|
322
|
+
return [2 /*return*/, null];
|
|
323
|
+
}
|
|
324
|
+
response = void 0;
|
|
325
|
+
if (!FronteggConfig.fronteggAppOptions.hostedLoginBox) return [3 /*break*/, 6];
|
|
326
|
+
return [4 /*yield*/, refreshTokenHostedLogin(ctx, headers)];
|
|
327
|
+
case 5:
|
|
328
|
+
response = _d.sent();
|
|
329
|
+
return [3 /*break*/, 8];
|
|
330
|
+
case 6: return [4 /*yield*/, refreshTokenEmbedded(ctx, headers, cookies)];
|
|
331
|
+
case 7:
|
|
332
|
+
response = _d.sent();
|
|
333
|
+
_d.label = 8;
|
|
334
|
+
case 8:
|
|
335
|
+
if (!response) {
|
|
336
|
+
removeCookies(FronteggConfig.cookieName, isSecured, FronteggConfig.cookieDomain, ctx.res);
|
|
337
|
+
return [2 /*return*/, null];
|
|
338
|
+
}
|
|
339
|
+
if (!response.ok) return [3 /*break*/, 11];
|
|
340
|
+
return [4 /*yield*/, response.text()];
|
|
341
|
+
case 9:
|
|
342
|
+
data = _d.sent();
|
|
343
|
+
rewriteCookieDomainConfig = (_c = {},
|
|
344
|
+
_c[FronteggConfig.baseUrlHost] = FronteggConfig.cookieDomain,
|
|
345
|
+
_c);
|
|
346
|
+
cookieHeader = response.headers.raw()['set-cookie'];
|
|
347
|
+
newSetCookie = rewriteCookieProperty(cookieHeader, rewriteCookieDomainConfig, 'domain');
|
|
348
|
+
return [4 /*yield*/, createSessionFromAccessToken(data)];
|
|
349
|
+
case 10:
|
|
350
|
+
_b = _d.sent(), session = _b[0], decodedJwt = _b[1], refreshToken_1 = _b[2];
|
|
351
|
+
if (!session) {
|
|
352
|
+
return [2 /*return*/, null];
|
|
353
|
+
}
|
|
354
|
+
cookieValue = createCookie({ session: session, expires: new Date(decodedJwt.exp * 1000), isSecured: isSecured });
|
|
355
|
+
if (typeof newSetCookie === 'string') {
|
|
356
|
+
newSetCookie = [newSetCookie];
|
|
357
|
+
}
|
|
358
|
+
newSetCookie.push.apply(newSetCookie, cookieValue);
|
|
359
|
+
(_a = ctx.res) === null || _a === void 0 ? void 0 : _a.setHeader('set-cookie', newSetCookie);
|
|
360
|
+
return [2 /*return*/, {
|
|
361
|
+
accessToken: JSON.parse(data).accessToken,
|
|
362
|
+
user: decodedJwt,
|
|
363
|
+
refreshToken: refreshToken_1,
|
|
364
|
+
}];
|
|
365
|
+
case 11:
|
|
366
|
+
// remove all fe_nextjs-session cookies
|
|
367
|
+
// ctx.res?.setHeader('set-cookie', removedCookies);
|
|
368
|
+
return [2 /*return*/, null];
|
|
369
|
+
case 12: return [3 /*break*/, 14];
|
|
370
|
+
case 13:
|
|
371
|
+
_d.sent();
|
|
372
|
+
return [2 /*return*/, null];
|
|
373
|
+
case 14: return [2 /*return*/];
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
var withFronteggApp = function (app, options) {
|
|
380
|
+
var _a, _b;
|
|
381
|
+
var originalGetInitialProps = app.getInitialProps;
|
|
382
|
+
app.getInitialProps = function (appContext) { return __awaiter(void 0, void 0, void 0, function () {
|
|
383
|
+
var ctx, Component, session, _a, user, tenants, _b, envAppUrl, envBaseUrl, envClientId, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
384
|
+
var _o;
|
|
385
|
+
return __generator(this, function (_p) {
|
|
386
|
+
switch (_p.label) {
|
|
387
|
+
case 0:
|
|
388
|
+
ctx = appContext.ctx, Component = appContext.Component;
|
|
389
|
+
if (!ctx.req) return [3 /*break*/, 9];
|
|
390
|
+
return [4 /*yield*/, refreshToken(ctx)];
|
|
391
|
+
case 1:
|
|
392
|
+
session = _p.sent();
|
|
393
|
+
return [4 /*yield*/, getMeAndTenants((_o = ctx.req) === null || _o === void 0 ? void 0 : _o.headers, session === null || session === void 0 ? void 0 : session.accessToken)];
|
|
394
|
+
case 2:
|
|
395
|
+
_a = _p.sent(), user = _a.user, tenants = _a.tenants;
|
|
396
|
+
appContext.session = session;
|
|
397
|
+
appContext.user = user;
|
|
398
|
+
appContext.tenants = tenants;
|
|
399
|
+
_b = FronteggConfig.appEnvConfig, envAppUrl = _b.envAppUrl, envBaseUrl = _b.envBaseUrl, envClientId = _b.envClientId;
|
|
400
|
+
if (!envAppUrl) {
|
|
401
|
+
throw Error(fronteggErrors.envAppUrl);
|
|
402
|
+
}
|
|
403
|
+
if (!envBaseUrl) {
|
|
404
|
+
throw Error(fronteggErrors.envBaseUrl);
|
|
405
|
+
}
|
|
406
|
+
if (!envClientId) {
|
|
407
|
+
throw Error(fronteggErrors.envClientId);
|
|
408
|
+
}
|
|
409
|
+
_c = {};
|
|
410
|
+
_d = [{}];
|
|
411
|
+
if (!originalGetInitialProps) return [3 /*break*/, 4];
|
|
412
|
+
return [4 /*yield*/, originalGetInitialProps(appContext)];
|
|
413
|
+
case 3:
|
|
414
|
+
_e = _p.sent();
|
|
415
|
+
return [3 /*break*/, 5];
|
|
416
|
+
case 4:
|
|
417
|
+
_e = {};
|
|
418
|
+
_p.label = 5;
|
|
419
|
+
case 5:
|
|
420
|
+
_f = [__assign.apply(void 0, _d.concat([(_e)]))];
|
|
421
|
+
if (!Component.getInitialProps) return [3 /*break*/, 7];
|
|
422
|
+
return [4 /*yield*/, Component.getInitialProps(ctx)];
|
|
423
|
+
case 6:
|
|
424
|
+
_g = _p.sent();
|
|
425
|
+
return [3 /*break*/, 8];
|
|
426
|
+
case 7:
|
|
427
|
+
_g = {};
|
|
428
|
+
_p.label = 8;
|
|
429
|
+
case 8: return [2 /*return*/, (_c.pageProps = __assign.apply(void 0, [__assign.apply(void 0, _f.concat([(_g)])), { session: session,
|
|
430
|
+
user: user,
|
|
431
|
+
tenants: tenants,
|
|
432
|
+
envAppUrl: envAppUrl, envBaseUrl: process.env['FRONTEGG_BASE_URL'], envClientId: process.env['FRONTEGG_CLIENT_ID'] }]),
|
|
433
|
+
_c)];
|
|
434
|
+
case 9:
|
|
435
|
+
appContext.session = null;
|
|
436
|
+
_h = {};
|
|
437
|
+
_j = [{}];
|
|
438
|
+
if (!originalGetInitialProps) return [3 /*break*/, 11];
|
|
439
|
+
return [4 /*yield*/, originalGetInitialProps(appContext)];
|
|
440
|
+
case 10:
|
|
441
|
+
_k = _p.sent();
|
|
442
|
+
return [3 /*break*/, 12];
|
|
443
|
+
case 11:
|
|
444
|
+
_k = {};
|
|
445
|
+
_p.label = 12;
|
|
446
|
+
case 12:
|
|
447
|
+
_l = [__assign.apply(void 0, _j.concat([(_k)]))];
|
|
448
|
+
if (!Component.getInitialProps) return [3 /*break*/, 14];
|
|
449
|
+
return [4 /*yield*/, Component.getInitialProps(ctx)];
|
|
450
|
+
case 13:
|
|
451
|
+
_m = _p.sent();
|
|
452
|
+
return [3 /*break*/, 15];
|
|
453
|
+
case 14:
|
|
454
|
+
_m = {};
|
|
455
|
+
_p.label = 15;
|
|
456
|
+
case 15: return [2 /*return*/, (_h.pageProps = __assign.apply(void 0, _l.concat([(_m)])),
|
|
457
|
+
_h)];
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
}); };
|
|
461
|
+
FronteggConfig.authRoutes = (_b = (_a = options === null || options === void 0 ? void 0 : options.authOptions) === null || _a === void 0 ? void 0 : _a.routes) !== null && _b !== void 0 ? _b : {};
|
|
462
|
+
FronteggConfig.fronteggAppOptions = options !== null && options !== void 0 ? options : {};
|
|
463
|
+
function CustomFronteggApp(appProps) {
|
|
464
|
+
var _a = appProps.pageProps, user = _a.user, tenants = _a.tenants, session = _a.session, envAppUrl = _a.envAppUrl, envBaseUrl = _a.envBaseUrl, envClientId = _a.envClientId;
|
|
465
|
+
return (React.createElement(FronteggProvider, __assign({}, options, { user: user, tenants: tenants, session: session, envAppUrl: envAppUrl, envBaseUrl: envBaseUrl, envClientId: envClientId }), app(appProps)));
|
|
466
|
+
}
|
|
467
|
+
CustomFronteggApp.getInitialProps = app.getInitialProps;
|
|
468
|
+
return CustomFronteggApp;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* @see https://www.npmjs.com/package/http-proxy
|
|
473
|
+
*/
|
|
474
|
+
var proxy = httpProxy.createProxyServer({
|
|
475
|
+
target: process.env['FRONTEGG_BASE_URL'],
|
|
476
|
+
});
|
|
477
|
+
/**
|
|
478
|
+
* Please refer to the following links for the specification document for HTTP.
|
|
479
|
+
* @see https://tools.ietf.org/html/rfc7231
|
|
480
|
+
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
|
|
481
|
+
*/
|
|
482
|
+
var hasRequestBodyMethods = ['HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH'];
|
|
483
|
+
/**
|
|
484
|
+
* If pattern information matching the input url information is found in the `pathRewrite` array,
|
|
485
|
+
* the url value is partially replaced with the `pathRewrite.replaceStr` value.
|
|
486
|
+
* @param url
|
|
487
|
+
* @param pathRewrite
|
|
488
|
+
*/
|
|
489
|
+
var rewritePath = function (url, pathRewrite) {
|
|
490
|
+
if (Array.isArray(pathRewrite)) {
|
|
491
|
+
for (var _i = 0, pathRewrite_1 = pathRewrite; _i < pathRewrite_1.length; _i++) {
|
|
492
|
+
var item = pathRewrite_1[_i];
|
|
493
|
+
var patternStr = item.patternStr, replaceStr = item.replaceStr;
|
|
494
|
+
var pattern = RegExp(patternStr);
|
|
495
|
+
if (pattern.test(url)) {
|
|
496
|
+
return url.replace(pattern, replaceStr);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
// tslint:disable-next-line:forin
|
|
502
|
+
for (var patternStr in pathRewrite) {
|
|
503
|
+
var pattern = RegExp(patternStr);
|
|
504
|
+
var path = pathRewrite[patternStr];
|
|
505
|
+
if (pattern.test(url)) {
|
|
506
|
+
return url.replace(pattern, path);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return url;
|
|
511
|
+
};
|
|
512
|
+
/**
|
|
513
|
+
* Next.js HTTP Proxy Middleware
|
|
514
|
+
* @see https://nextjs.org/docs/api-routes/api-middlewares
|
|
515
|
+
* @param {NextApiRequest} req
|
|
516
|
+
* @param {NextApiResponse} res
|
|
517
|
+
*/
|
|
518
|
+
function fronteggMiddleware(req, res) {
|
|
519
|
+
var _this = this;
|
|
520
|
+
return new Promise(function (resolve, reject) {
|
|
521
|
+
var _a;
|
|
522
|
+
var pathRewrite = [
|
|
523
|
+
{
|
|
524
|
+
patternStr: '^/api/',
|
|
525
|
+
replaceStr: '/',
|
|
526
|
+
},
|
|
527
|
+
];
|
|
528
|
+
if (pathRewrite) {
|
|
529
|
+
req.url = rewritePath(req.url, pathRewrite);
|
|
530
|
+
}
|
|
531
|
+
if (hasRequestBodyMethods.indexOf(req.method) >= 0 && typeof req.body === 'object') {
|
|
532
|
+
req.body = JSON.stringify(req.body);
|
|
533
|
+
}
|
|
534
|
+
var isSecured = new URL(FronteggConfig.appUrl).protocol === 'https:';
|
|
535
|
+
proxy
|
|
536
|
+
.once('proxyReq', function (proxyReq, req) {
|
|
537
|
+
if (hasRequestBodyMethods.indexOf(req.method) >= 0 && typeof req.body === 'string') {
|
|
538
|
+
proxyReq.write(req.body);
|
|
539
|
+
proxyReq.end();
|
|
540
|
+
}
|
|
541
|
+
})
|
|
542
|
+
.once('proxyRes', function (proxyRes, req, serverResponse) {
|
|
543
|
+
proxyRes.headers['set-cookie'] = modifySetCookieIfUnsecure(proxyRes.headers['set-cookie'], isSecured);
|
|
544
|
+
var _end = res.end;
|
|
545
|
+
var buffer = new Buffer('');
|
|
546
|
+
proxyRes
|
|
547
|
+
.on('data', function (chunk) {
|
|
548
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
549
|
+
})
|
|
550
|
+
.on('end', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
551
|
+
var output, isLogout, _a, session, decodedJwt, sessionCookie;
|
|
552
|
+
var _b, _c;
|
|
553
|
+
return __generator(this, function (_d) {
|
|
554
|
+
switch (_d.label) {
|
|
555
|
+
case 0:
|
|
556
|
+
output = buffer.toString('utf-8');
|
|
557
|
+
isLogout = (_b = req === null || req === void 0 ? void 0 : req.url) === null || _b === void 0 ? void 0 : _b.endsWith((_c = fronteggAuthApiRoutes.find(function (path) { return path.endsWith('/logout'); })) !== null && _c !== void 0 ? _c : '/logout');
|
|
558
|
+
if (!isLogout) return [3 /*break*/, 1];
|
|
559
|
+
removeCookies(FronteggConfig.cookieName, isSecured, FronteggConfig.cookieDomain, serverResponse);
|
|
560
|
+
return [3 /*break*/, 3];
|
|
561
|
+
case 1: return [4 /*yield*/, createSessionFromAccessToken(output)];
|
|
562
|
+
case 2:
|
|
563
|
+
_a = _d.sent(), session = _a[0], decodedJwt = _a[1];
|
|
564
|
+
if (session) {
|
|
565
|
+
sessionCookie = createCookie({
|
|
566
|
+
session: session,
|
|
567
|
+
expires: new Date(decodedJwt.exp * 1000),
|
|
568
|
+
isSecured: isSecured,
|
|
569
|
+
});
|
|
570
|
+
addToCookies(sessionCookie, serverResponse);
|
|
571
|
+
}
|
|
572
|
+
_d.label = 3;
|
|
573
|
+
case 3:
|
|
574
|
+
res.setHeader('content-length', output.length);
|
|
575
|
+
res.setHeader('content-encoding', '');
|
|
576
|
+
// @ts-ignore
|
|
577
|
+
_end.apply(res, [output]);
|
|
578
|
+
return [2 /*return*/];
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
}); });
|
|
582
|
+
// disable default behavior to read jwt
|
|
583
|
+
// @ts-ignore
|
|
584
|
+
serverResponse.write = function () { return undefined; };
|
|
585
|
+
// @ts-ignore
|
|
586
|
+
serverResponse.end = function () { return undefined; };
|
|
587
|
+
})
|
|
588
|
+
.once('error', reject)
|
|
589
|
+
.web(req, res, {
|
|
590
|
+
changeOrigin: true,
|
|
591
|
+
cookieDomainRewrite: (_a = {},
|
|
592
|
+
_a[FronteggConfig.baseUrlHost] = FronteggConfig.cookieDomain,
|
|
593
|
+
_a),
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export { AuthorizedContent, FronteggProvider, FronteggProviderNoSSR, FronteggRouter, FronteggRouterProps, fronteggMiddleware, getSession, rewritePath, withFronteggApp, withSSRSession };
|
|
599
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontegg/nextjs",
|
|
3
|
-
"
|
|
3
|
+
"libName": "FronteggNextJs",
|
|
4
|
+
"version": "6.7.9-alpha.3741489660",
|
|
5
|
+
"author": "Frontegg LTD",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"types": "index.d.ts",
|
|
4
8
|
"dependencies": {
|
|
5
9
|
"@frontegg/js": "6.52.0",
|
|
6
10
|
"@frontegg/react-hooks": "6.52.0",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
11
|
+
"@types/http-proxy": "^1.17.9",
|
|
12
|
+
"cookie": "^0.5.0",
|
|
9
13
|
"http-proxy": "^1.18.1",
|
|
10
|
-
"
|
|
14
|
+
"iron-session": "^6.3.1",
|
|
15
|
+
"jose": "^4.9.2"
|
|
11
16
|
},
|
|
12
17
|
"peerDependencies": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
18
|
+
"next": ">10.0.0",
|
|
19
|
+
"react": ">16.9.0",
|
|
20
|
+
"react-dom": ">16.9.0"
|
|
15
21
|
},
|
|
16
|
-
"main": "
|
|
17
|
-
"module": "
|
|
18
|
-
"
|
|
19
|
-
}
|
|
22
|
+
"main": "index.js",
|
|
23
|
+
"module": "index.esm.js",
|
|
24
|
+
"es2015": "index.es.js"
|
|
25
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { FronteggUserSession, FronteggUserTokens } from '../common';
|
|
2
|
+
export declare const getSession: () => Promise<import("../common").FronteggNextJSSession | undefined>;
|
|
3
|
+
export declare function getUserSession(): Promise<FronteggUserSession | undefined>;
|
|
4
|
+
export declare function getUserTokens(): Promise<FronteggUserTokens | undefined>;
|