@frontegg/nextjs 5.4.0-alpha.2494518347 → 5.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -214
- package/helpers.d.ts +2 -0
- package/index.cjs.js +90 -25
- package/index.esm.js +72 -26
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,217 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
# nextjs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
and integrate them into their SaaS portals in up to 5 lines of code.
|
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
|
5
4
|
|
|
6
|
-
##
|
|
5
|
+
## Running unit tests
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
- [Create new NextJS project](#create-new-nextjs-project)
|
|
10
|
-
- [Add to existing project](#add-to-existing-project)
|
|
11
|
-
- [Getting Started](#getting-started)
|
|
12
|
-
- [Create Frontegg worksapce](#create-frontegg-worksapce)
|
|
13
|
-
- [Setup environment](#setup-environment)
|
|
14
|
-
- [Documentation](#documentation)
|
|
15
|
-
- [API Reference](#api-reference)
|
|
16
|
-
- [Frontegg Provider Options](#frontegg-provider-options)
|
|
17
|
-
- [getSession](#getsession)
|
|
18
|
-
- [withSSRSession](#withssrsession)
|
|
19
|
-
- for more [visit](https://docs.frontegg.com/docs/self-service-introduction)
|
|
20
|
-
|
|
21
|
-
## Installation
|
|
22
|
-
|
|
23
|
-
### Create new NextJS project
|
|
24
|
-
|
|
25
|
-
To start a new Create Next App project with TypeScript, you can run:
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
npx create-next-app --example "https://github.com/frontegg/frontegg-nextjs/tree/main" --example-path "apps/example" my-nextjs-app-name
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
or
|
|
32
|
-
|
|
33
|
-
```bash
|
|
34
|
-
yarn create next-app --example "https://github.com/frontegg/frontegg-nextjs/tree/main" --example-path "apps/example" my-nextjs-app-name
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
> If you've previously installed `create-react-app` globally via `npm install -g create-next-app`, we recommend you uninstall the package using `npm uninstall -g create-next-app` or `yarn global remove create-next-app` to ensure that `npx` always uses the latest version.
|
|
38
|
-
>
|
|
39
|
-
> Global installations of `create-next-app` are no longer supported.
|
|
40
|
-
|
|
41
|
-
### Add to existing project
|
|
42
|
-
|
|
43
|
-
To Add Frontegg to your existing Nextjs project, follow below steps:
|
|
44
|
-
|
|
45
|
-
1. Use package manager to install Frontegg Next.JS library.
|
|
46
|
-
|
|
47
|
-
```bash
|
|
48
|
-
npm install --save @frontegg/nextjs
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
or
|
|
52
|
-
|
|
53
|
-
```bash
|
|
54
|
-
yarn add --save @frontegg/nextjs
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
2. Wrap the default export with `withFronteggApp` in `./pages/_app.tsx`:
|
|
58
|
-
|
|
59
|
-
```tsx
|
|
60
|
-
// ./pages/_app.tsx
|
|
61
|
-
|
|
62
|
-
import { withFronteggApp } from '@frontegg/nextjs';
|
|
63
|
-
|
|
64
|
-
function CustomApp({ Component, pageProps }: AppProps) {
|
|
65
|
-
return <Component {...pageProps} />;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export default withFronteggApp(CustomApp);
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
3. Create files for frontegg middleware under `./pages/api/frontegg/[...frontegg-middleware].ts`:
|
|
72
|
-
|
|
73
|
-
```tsx
|
|
74
|
-
// ./pages/api/frontegg/[...frontegg-middleware].ts
|
|
75
|
-
|
|
76
|
-
export { fronteggMiddleware as default } from '@frontegg/nextjs';
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
4. Create placeholder pages for frontegg router under `./pages/[...frontegg-router].tsx`:
|
|
80
|
-
|
|
81
|
-
```tsx
|
|
82
|
-
// ./pages/[...frontegg-router].tsx
|
|
83
|
-
|
|
84
|
-
export {
|
|
85
|
-
FronteggRouter as default,
|
|
86
|
-
FronteggRouterProps as getServerSideProps,
|
|
87
|
-
} from '@frontegg/nextjs';
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
## Getting Started
|
|
91
|
-
|
|
92
|
-
### Create Frontegg worksapce
|
|
93
|
-
|
|
94
|
-
Navigate to [Frontegg Portal Settgins](https://portal.frontegg.com/development/settings), If you don't have application
|
|
95
|
-
follow integration steps after signing up.
|
|
96
|
-
|
|
97
|
-
Next, configure the "Allowed Origins" in your application under "Domain" tab of the "Settings" page :
|
|
98
|
-
|
|
99
|
-
- http://localhost:3000 // for development environments
|
|
100
|
-
- https://my-company-domain.com // for production environments
|
|
101
|
-
|
|
102
|
-
Copy ClientID, Frontegg Domain from "Settings" page, You'll need these values in the next step.
|
|
103
|
-
|
|
104
|
-
### Setup environment
|
|
105
|
-
|
|
106
|
-
To setup your Next.js application to communicate with Frontegg, you have to create a new file named `.env.local` under
|
|
107
|
-
your root project directory, this file will be used to store environment variables that will be used, configuration
|
|
108
|
-
options:
|
|
109
|
-
|
|
110
|
-
```dotenv
|
|
111
|
-
# The AppUrl is to tell Frontegg your application hostname
|
|
112
|
-
FRONTEGG_APP_URL='http://localhost:3000'
|
|
113
|
-
|
|
114
|
-
# The Frontegg domain is your unique URL to connect to the Frontegg gateway
|
|
115
|
-
FRONTEGG_BASE_URL='https://{YOUR_SUB_DOMAIN}.frontegg.com'
|
|
116
|
-
|
|
117
|
-
# Your Frontegg application's Client ID
|
|
118
|
-
FRONTEGG_CLIENT_ID='{YOUR_APPLICATION_CLIENT_ID}'
|
|
119
|
-
|
|
120
|
-
# The statless session encruption password, used to encrypt
|
|
121
|
-
# jwt before sending it to the client side.
|
|
122
|
-
#
|
|
123
|
-
# For quick password generation use the following command:
|
|
124
|
-
# node -e "console.log(crypto.randomBytes(32).toString('hex'))"
|
|
125
|
-
FRONTEGG_ENCRYPTION_PASSWORD='{SESSION_ENCRYPTION_PASSWORD}'
|
|
126
|
-
|
|
127
|
-
# The statless session cookie name
|
|
128
|
-
FRONTEGG_COOKIE_NAME='fe_session'
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
## Documentation
|
|
132
|
-
|
|
133
|
-
### API Reference
|
|
134
|
-
|
|
135
|
-
Visit [Frontegg Docs](https://docs.frontegg.com) for the full documentation.
|
|
136
|
-
|
|
137
|
-
### Frontegg Provider Options
|
|
138
|
-
|
|
139
|
-
Pass seconds argument to `withFronteggApp` function in `_app.ts` file to customize
|
|
140
|
-
Frontegg library.
|
|
141
|
-
|
|
142
|
-
```tsx
|
|
143
|
-
// ./pages/_app.tsx
|
|
144
|
-
|
|
145
|
-
import { withFronteggApp } from '@frontegg/nextjs';
|
|
146
|
-
|
|
147
|
-
function CustomApp({ Component, pageProps }: AppProps) {
|
|
148
|
-
return <Component {...pageProps} />;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export default withFronteggApp(CustomApp, {
|
|
152
|
-
/**
|
|
153
|
-
* Frontegg options for customizations
|
|
154
|
-
*/
|
|
155
|
-
});
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### getSession
|
|
159
|
-
|
|
160
|
-
For any pages that required AccessToken in Server Side, you can use:
|
|
161
|
-
|
|
162
|
-
```tsx
|
|
163
|
-
import { GetServerSideProps } from 'next';
|
|
164
|
-
import { getSession } from '@frontegg/nextjs';
|
|
165
|
-
|
|
166
|
-
export default function MyPage({ products }) {
|
|
167
|
-
return (
|
|
168
|
-
<div>
|
|
169
|
-
<h1>My Page</h1>
|
|
170
|
-
{products}
|
|
171
|
-
</div>
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export const getServerSideProps: GetServerSideProps = async (context) => {
|
|
176
|
-
const session = await getSession(context.req);
|
|
177
|
-
if (session) {
|
|
178
|
-
const { data } = await fetch('{external}/product', {
|
|
179
|
-
headers: {
|
|
180
|
-
Authorization: 'bearer ' + session.accessToken,
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
return { props: { products: data } };
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return { props: { products: [] } };
|
|
187
|
-
};
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
### withSSRSession
|
|
191
|
-
|
|
192
|
-
withSSRSession HOC can be used to automatic redirect users to login screen if not logged in:
|
|
193
|
-
|
|
194
|
-
```tsx
|
|
195
|
-
import { GetServerSideProps } from 'next';
|
|
196
|
-
import { withSSRSession } from '@frontegg/nextjs';
|
|
197
|
-
|
|
198
|
-
export default function MyPage({ products }) {
|
|
199
|
-
return (
|
|
200
|
-
<div>
|
|
201
|
-
<h1>My Page</h1>
|
|
202
|
-
{products}
|
|
203
|
-
</div>
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export const getServerSideProps: GetServerSideProps = withSSRSession(
|
|
208
|
-
async (context, session) => {
|
|
209
|
-
const { data } = await fetch('{external}/product', {
|
|
210
|
-
headers: {
|
|
211
|
-
Authorization: 'bearer ' + session.accessToken,
|
|
212
|
-
},
|
|
213
|
-
});
|
|
214
|
-
return { props: { products: data } };
|
|
215
|
-
}
|
|
216
|
-
);
|
|
217
|
-
```
|
|
7
|
+
Run `nx test nextjs` to execute the unit tests via [Jest](https://jestjs.io).
|
package/helpers.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ import { FronteggNextJSSession } from './types';
|
|
|
5
5
|
export declare function refreshToken(ctx: NextPageContext): Promise<FronteggNextJSSession | null>;
|
|
6
6
|
export declare function addToCookies(cookieValue: string, res: ServerResponse): void;
|
|
7
7
|
export declare function removeCookies(cookieName: string, isSecured: boolean, cookieDomain: string, res: ServerResponse): void;
|
|
8
|
+
export declare function compress(input: string): Promise<string>;
|
|
9
|
+
export declare function uncompress(input: string): Promise<string>;
|
|
8
10
|
export declare function createSessionFromAccessToken(output: string): Promise<[string, any] | []>;
|
|
9
11
|
export declare const modifySetCookieIfUnsecure: (setCookieValue: string[] | undefined, isSecured: boolean) => string[] | undefined;
|
package/index.cjs.js
CHANGED
|
@@ -13,12 +13,32 @@ var reduxStore = require('@frontegg/redux-store');
|
|
|
13
13
|
var url = require('url');
|
|
14
14
|
var cookie = require('cookie');
|
|
15
15
|
var ironSession = require('iron-session');
|
|
16
|
+
var zlib = require('zlib');
|
|
16
17
|
var httpProxy = require('http-proxy');
|
|
17
18
|
var types = require('@frontegg/types');
|
|
18
19
|
|
|
19
20
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
20
21
|
|
|
22
|
+
function _interopNamespace(e) {
|
|
23
|
+
if (e && e.__esModule) return e;
|
|
24
|
+
var n = Object.create(null);
|
|
25
|
+
if (e) {
|
|
26
|
+
Object.keys(e).forEach(function (k) {
|
|
27
|
+
if (k !== 'default') {
|
|
28
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
29
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: function () { return e[k]; }
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
n["default"] = e;
|
|
37
|
+
return Object.freeze(n);
|
|
38
|
+
}
|
|
39
|
+
|
|
21
40
|
var cookie__default = /*#__PURE__*/_interopDefaultLegacy(cookie);
|
|
41
|
+
var zlib__namespace = /*#__PURE__*/_interopNamespace(zlib);
|
|
22
42
|
var httpProxy__default = /*#__PURE__*/_interopDefaultLegacy(httpProxy);
|
|
23
43
|
|
|
24
44
|
/******************************************************************************
|
|
@@ -1062,7 +1082,7 @@ var FronteggConfig = /*#__PURE__*/function () {
|
|
|
1062
1082
|
this.authRoutes = {};
|
|
1063
1083
|
this.fronteggAppOptions = {};
|
|
1064
1084
|
this._clientId = (_a = process.env['FRONTEGG_CLIENT_ID']) !== null && _a !== void 0 ? _a : '';
|
|
1065
|
-
this._cookieName = (_b = process.env['FRONTEGG_COOKIE_NAME']) !== null && _b !== void 0 ? _b : "
|
|
1085
|
+
this._cookieName = (_b = process.env['FRONTEGG_COOKIE_NAME']) !== null && _b !== void 0 ? _b : "fe_next_session";
|
|
1066
1086
|
this._password = (_c = process.env['FRONTEGG_ENCRYPTION_PASSWORD']) !== null && _c !== void 0 ? _c : '';
|
|
1067
1087
|
this._passwordsAsMap = normalizeStringPasswordToMap(this._password);
|
|
1068
1088
|
}
|
|
@@ -1070,7 +1090,7 @@ var FronteggConfig = /*#__PURE__*/function () {
|
|
|
1070
1090
|
_createClass(FronteggConfig, [{
|
|
1071
1091
|
key: "cookieName",
|
|
1072
1092
|
get: function get() {
|
|
1073
|
-
return this._cookieName;
|
|
1093
|
+
return "".concat(this._cookieName, "-").concat(this._clientId.replace(/-/g, ''));
|
|
1074
1094
|
}
|
|
1075
1095
|
}, {
|
|
1076
1096
|
key: "password",
|
|
@@ -1198,9 +1218,14 @@ function FronteggRouter() {
|
|
|
1198
1218
|
var app = react.useContext(AppContext);
|
|
1199
1219
|
|
|
1200
1220
|
var _useRouter = router.useRouter(),
|
|
1201
|
-
query = _useRouter.query
|
|
1221
|
+
query = _useRouter.query,
|
|
1222
|
+
replace = _useRouter.replace;
|
|
1202
1223
|
|
|
1203
1224
|
var loginWithRedirect = reactHooks.useLoginWithRedirect();
|
|
1225
|
+
|
|
1226
|
+
var _useLoginActions = reactHooks.useLoginActions(),
|
|
1227
|
+
logout = _useLoginActions.logout;
|
|
1228
|
+
|
|
1204
1229
|
react.useEffect(function () {
|
|
1205
1230
|
if (!app) {
|
|
1206
1231
|
return;
|
|
@@ -1216,12 +1241,20 @@ function FronteggRouter() {
|
|
|
1216
1241
|
var pathname = "/".concat(pathArr.join('/'));
|
|
1217
1242
|
|
|
1218
1243
|
if (pathname === routesObj.loginUrl) {
|
|
1244
|
+
if (queryParams.redirectUrl) {
|
|
1245
|
+
localStorage.setItem('FRONTEGG_AFTER_AUTH_REDIRECT_URL', "".concat(window.location.origin, "/").concat(queryParams.redirectUrl));
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1219
1248
|
loginWithRedirect();
|
|
1220
|
-
} else {
|
|
1221
|
-
|
|
1249
|
+
} else if (pathname === routesObj.logoutUrl) {
|
|
1250
|
+
var _baseUrl = app.options.contextOptions.baseUrl;
|
|
1251
|
+
var baseUrl = typeof _baseUrl === 'string' ? _baseUrl : _baseUrl('');
|
|
1252
|
+
logout(function () {
|
|
1253
|
+
window.location.href = "".concat(baseUrl, "/oauth/logout?post_logout_redirect_uri=").concat(encodeURIComponent(window.location.origin));
|
|
1254
|
+
});
|
|
1222
1255
|
}
|
|
1223
1256
|
}
|
|
1224
|
-
}, [app, query, loginWithRedirect]);
|
|
1257
|
+
}, [app, query, loginWithRedirect, logout, replace]);
|
|
1225
1258
|
return '';
|
|
1226
1259
|
}
|
|
1227
1260
|
function FronteggRouterProps(context) {
|
|
@@ -1241,7 +1274,7 @@ function FronteggRouterProps(context) {
|
|
|
1241
1274
|
var notFound = routesArr.indexOf(pathname) === -1;
|
|
1242
1275
|
|
|
1243
1276
|
if (fronteggConfig.fronteggAppOptions.hostedLoginBox) {
|
|
1244
|
-
var _notFound = !(routesObj.loginUrl === pathname || routesObj.hostedLoginRedirectUrl === pathname);
|
|
1277
|
+
var _notFound = !(routesObj.loginUrl === pathname || routesObj.logoutUrl === pathname || routesObj.hostedLoginRedirectUrl === pathname);
|
|
1245
1278
|
|
|
1246
1279
|
return {
|
|
1247
1280
|
notFound: _notFound,
|
|
@@ -1314,7 +1347,7 @@ function _defineProperty(obj, key, value) {
|
|
|
1314
1347
|
|
|
1315
1348
|
function getSession(req) {
|
|
1316
1349
|
return __awaiter(this, void 0, void 0, /*#__PURE__*/regenerator.mark(function _callee() {
|
|
1317
|
-
var sealFromCookies, jwt, publicKey, _yield$jwtVerify, payload, session;
|
|
1350
|
+
var sealFromCookies, compressedJwt, jwt, publicKey, _yield$jwtVerify, payload, session;
|
|
1318
1351
|
|
|
1319
1352
|
return regenerator.wrap(function _callee$(_context) {
|
|
1320
1353
|
while (1) {
|
|
@@ -1337,16 +1370,21 @@ function getSession(req) {
|
|
|
1337
1370
|
});
|
|
1338
1371
|
|
|
1339
1372
|
case 6:
|
|
1340
|
-
|
|
1373
|
+
compressedJwt = _context.sent;
|
|
1341
1374
|
_context.next = 9;
|
|
1342
|
-
return
|
|
1375
|
+
return uncompress(compressedJwt);
|
|
1343
1376
|
|
|
1344
1377
|
case 9:
|
|
1345
|
-
|
|
1378
|
+
jwt = _context.sent;
|
|
1346
1379
|
_context.next = 12;
|
|
1347
|
-
return
|
|
1380
|
+
return fronteggConfig.getJwtPublicKey();
|
|
1348
1381
|
|
|
1349
1382
|
case 12:
|
|
1383
|
+
publicKey = _context.sent;
|
|
1384
|
+
_context.next = 15;
|
|
1385
|
+
return jose.jwtVerify(jwt, publicKey);
|
|
1386
|
+
|
|
1387
|
+
case 15:
|
|
1350
1388
|
_yield$jwtVerify = _context.sent;
|
|
1351
1389
|
payload = _yield$jwtVerify.payload;
|
|
1352
1390
|
session = {
|
|
@@ -1355,27 +1393,27 @@ function getSession(req) {
|
|
|
1355
1393
|
};
|
|
1356
1394
|
|
|
1357
1395
|
if (!(session.user.exp * 1000 < Date.now())) {
|
|
1358
|
-
_context.next =
|
|
1396
|
+
_context.next = 20;
|
|
1359
1397
|
break;
|
|
1360
1398
|
}
|
|
1361
1399
|
|
|
1362
1400
|
return _context.abrupt("return", undefined);
|
|
1363
1401
|
|
|
1364
|
-
case
|
|
1402
|
+
case 20:
|
|
1365
1403
|
return _context.abrupt("return", session);
|
|
1366
1404
|
|
|
1367
|
-
case
|
|
1368
|
-
_context.prev =
|
|
1405
|
+
case 23:
|
|
1406
|
+
_context.prev = 23;
|
|
1369
1407
|
_context.t0 = _context["catch"](0);
|
|
1370
1408
|
console.error(_context.t0);
|
|
1371
1409
|
return _context.abrupt("return", undefined);
|
|
1372
1410
|
|
|
1373
|
-
case
|
|
1411
|
+
case 27:
|
|
1374
1412
|
case "end":
|
|
1375
1413
|
return _context.stop();
|
|
1376
1414
|
}
|
|
1377
1415
|
}
|
|
1378
|
-
}, _callee, null, [[0,
|
|
1416
|
+
}, _callee, null, [[0, 23]]);
|
|
1379
1417
|
}));
|
|
1380
1418
|
}
|
|
1381
1419
|
function withSSRSession(handler) {
|
|
@@ -1628,11 +1666,33 @@ function removeCookies(cookieName, isSecured, cookieDomain, res) {
|
|
|
1628
1666
|
|
|
1629
1667
|
res.setHeader('set-cookie', [].concat(_toConsumableArray(existingSetCookie), [cookieValue]));
|
|
1630
1668
|
}
|
|
1669
|
+
function compress(input) {
|
|
1670
|
+
return new Promise(function (resolve, reject) {
|
|
1671
|
+
zlib__namespace.brotliCompress(input, function (error, result) {
|
|
1672
|
+
if (error) {
|
|
1673
|
+
reject(error);
|
|
1674
|
+
} else {
|
|
1675
|
+
resolve(result.toString('base64'));
|
|
1676
|
+
}
|
|
1677
|
+
});
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
function uncompress(input) {
|
|
1681
|
+
return new Promise(function (resolve, reject) {
|
|
1682
|
+
zlib__namespace.brotliDecompress(Buffer.from(input, 'base64'), function (error, result) {
|
|
1683
|
+
if (error) {
|
|
1684
|
+
reject(error);
|
|
1685
|
+
} else {
|
|
1686
|
+
resolve(result.toString('utf-8'));
|
|
1687
|
+
}
|
|
1688
|
+
});
|
|
1689
|
+
});
|
|
1690
|
+
}
|
|
1631
1691
|
function createSessionFromAccessToken(output) {
|
|
1632
1692
|
var _a;
|
|
1633
1693
|
|
|
1634
1694
|
return __awaiter(this, void 0, void 0, /*#__PURE__*/regenerator.mark(function _callee2() {
|
|
1635
|
-
var data, accessToken, decodedJwt, session;
|
|
1695
|
+
var data, accessToken, decodedJwt, compressedAccessToken, session;
|
|
1636
1696
|
return regenerator.wrap(function _callee2$(_context2) {
|
|
1637
1697
|
while (1) {
|
|
1638
1698
|
switch (_context2.prev = _context2.next) {
|
|
@@ -1643,26 +1703,31 @@ function createSessionFromAccessToken(output) {
|
|
|
1643
1703
|
decodedJwt = jose.decodeJwt(accessToken);
|
|
1644
1704
|
decodedJwt.expiresIn = Math.floor((decodedJwt.exp * 1000 - Date.now()) / 1000);
|
|
1645
1705
|
_context2.next = 7;
|
|
1646
|
-
return
|
|
1706
|
+
return compress(accessToken);
|
|
1707
|
+
|
|
1708
|
+
case 7:
|
|
1709
|
+
compressedAccessToken = _context2.sent;
|
|
1710
|
+
_context2.next = 10;
|
|
1711
|
+
return ironSession.sealData(compressedAccessToken, {
|
|
1647
1712
|
password: fronteggConfig.passwordsAsMap,
|
|
1648
1713
|
ttl: decodedJwt.exp
|
|
1649
1714
|
});
|
|
1650
1715
|
|
|
1651
|
-
case
|
|
1716
|
+
case 10:
|
|
1652
1717
|
session = _context2.sent;
|
|
1653
1718
|
return _context2.abrupt("return", [session, decodedJwt]);
|
|
1654
1719
|
|
|
1655
|
-
case
|
|
1656
|
-
_context2.prev =
|
|
1720
|
+
case 14:
|
|
1721
|
+
_context2.prev = 14;
|
|
1657
1722
|
_context2.t0 = _context2["catch"](0);
|
|
1658
1723
|
return _context2.abrupt("return", []);
|
|
1659
1724
|
|
|
1660
|
-
case
|
|
1725
|
+
case 17:
|
|
1661
1726
|
case "end":
|
|
1662
1727
|
return _context2.stop();
|
|
1663
1728
|
}
|
|
1664
1729
|
}
|
|
1665
|
-
}, _callee2, null, [[0,
|
|
1730
|
+
}, _callee2, null, [[0, 14]]);
|
|
1666
1731
|
}));
|
|
1667
1732
|
}
|
|
1668
1733
|
var modifySetCookieIfUnsecure = function modifySetCookieIfUnsecure(setCookieValue, isSecured) {
|
package/index.esm.js
CHANGED
|
@@ -2,7 +2,7 @@ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
|
2
2
|
import { createContext, useCallback, useMemo, useEffect, useContext } from 'react';
|
|
3
3
|
import { AppHolder, initialize } from '@frontegg/admin-portal';
|
|
4
4
|
export { AdminPortal } from '@frontegg/admin-portal';
|
|
5
|
-
import { FronteggStoreProvider, useAuthUserOrNull, useAuthActions, useLoginWithRedirect } from '@frontegg/react-hooks';
|
|
5
|
+
import { FronteggStoreProvider, useAuthUserOrNull, useAuthActions, useLoginWithRedirect, useLoginActions } from '@frontegg/react-hooks';
|
|
6
6
|
export * from '@frontegg/react-hooks';
|
|
7
7
|
import { fronteggAuthApiRoutes, ContextHolder, fronteggRefreshTokenUrl } from '@frontegg/rest-api';
|
|
8
8
|
import { useRouter } from 'next/router';
|
|
@@ -11,6 +11,7 @@ import { authInitialState } from '@frontegg/redux-store';
|
|
|
11
11
|
import { parse } from 'url';
|
|
12
12
|
import cookie from 'cookie';
|
|
13
13
|
import { unsealData, sealData } from 'iron-session';
|
|
14
|
+
import * as zlib from 'zlib';
|
|
14
15
|
import httpProxy from 'http-proxy';
|
|
15
16
|
export * from '@frontegg/types';
|
|
16
17
|
|
|
@@ -1055,7 +1056,7 @@ var FronteggConfig = /*#__PURE__*/function () {
|
|
|
1055
1056
|
this.authRoutes = {};
|
|
1056
1057
|
this.fronteggAppOptions = {};
|
|
1057
1058
|
this._clientId = (_a = process.env['FRONTEGG_CLIENT_ID']) !== null && _a !== void 0 ? _a : '';
|
|
1058
|
-
this._cookieName = (_b = process.env['FRONTEGG_COOKIE_NAME']) !== null && _b !== void 0 ? _b : "
|
|
1059
|
+
this._cookieName = (_b = process.env['FRONTEGG_COOKIE_NAME']) !== null && _b !== void 0 ? _b : "fe_next_session";
|
|
1059
1060
|
this._password = (_c = process.env['FRONTEGG_ENCRYPTION_PASSWORD']) !== null && _c !== void 0 ? _c : '';
|
|
1060
1061
|
this._passwordsAsMap = normalizeStringPasswordToMap(this._password);
|
|
1061
1062
|
}
|
|
@@ -1063,7 +1064,7 @@ var FronteggConfig = /*#__PURE__*/function () {
|
|
|
1063
1064
|
_createClass(FronteggConfig, [{
|
|
1064
1065
|
key: "cookieName",
|
|
1065
1066
|
get: function get() {
|
|
1066
|
-
return this._cookieName;
|
|
1067
|
+
return "".concat(this._cookieName, "-").concat(this._clientId.replace(/-/g, ''));
|
|
1067
1068
|
}
|
|
1068
1069
|
}, {
|
|
1069
1070
|
key: "password",
|
|
@@ -1191,9 +1192,14 @@ function FronteggRouter() {
|
|
|
1191
1192
|
var app = useContext(AppContext);
|
|
1192
1193
|
|
|
1193
1194
|
var _useRouter = useRouter(),
|
|
1194
|
-
query = _useRouter.query
|
|
1195
|
+
query = _useRouter.query,
|
|
1196
|
+
replace = _useRouter.replace;
|
|
1195
1197
|
|
|
1196
1198
|
var loginWithRedirect = useLoginWithRedirect();
|
|
1199
|
+
|
|
1200
|
+
var _useLoginActions = useLoginActions(),
|
|
1201
|
+
logout = _useLoginActions.logout;
|
|
1202
|
+
|
|
1197
1203
|
useEffect(function () {
|
|
1198
1204
|
if (!app) {
|
|
1199
1205
|
return;
|
|
@@ -1209,12 +1215,20 @@ function FronteggRouter() {
|
|
|
1209
1215
|
var pathname = "/".concat(pathArr.join('/'));
|
|
1210
1216
|
|
|
1211
1217
|
if (pathname === routesObj.loginUrl) {
|
|
1218
|
+
if (queryParams.redirectUrl) {
|
|
1219
|
+
localStorage.setItem('FRONTEGG_AFTER_AUTH_REDIRECT_URL', "".concat(window.location.origin, "/").concat(queryParams.redirectUrl));
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1212
1222
|
loginWithRedirect();
|
|
1213
|
-
} else {
|
|
1214
|
-
|
|
1223
|
+
} else if (pathname === routesObj.logoutUrl) {
|
|
1224
|
+
var _baseUrl = app.options.contextOptions.baseUrl;
|
|
1225
|
+
var baseUrl = typeof _baseUrl === 'string' ? _baseUrl : _baseUrl('');
|
|
1226
|
+
logout(function () {
|
|
1227
|
+
window.location.href = "".concat(baseUrl, "/oauth/logout?post_logout_redirect_uri=").concat(encodeURIComponent(window.location.origin));
|
|
1228
|
+
});
|
|
1215
1229
|
}
|
|
1216
1230
|
}
|
|
1217
|
-
}, [app, query, loginWithRedirect]);
|
|
1231
|
+
}, [app, query, loginWithRedirect, logout, replace]);
|
|
1218
1232
|
return '';
|
|
1219
1233
|
}
|
|
1220
1234
|
function FronteggRouterProps(context) {
|
|
@@ -1234,7 +1248,7 @@ function FronteggRouterProps(context) {
|
|
|
1234
1248
|
var notFound = routesArr.indexOf(pathname) === -1;
|
|
1235
1249
|
|
|
1236
1250
|
if (fronteggConfig.fronteggAppOptions.hostedLoginBox) {
|
|
1237
|
-
var _notFound = !(routesObj.loginUrl === pathname || routesObj.hostedLoginRedirectUrl === pathname);
|
|
1251
|
+
var _notFound = !(routesObj.loginUrl === pathname || routesObj.logoutUrl === pathname || routesObj.hostedLoginRedirectUrl === pathname);
|
|
1238
1252
|
|
|
1239
1253
|
return {
|
|
1240
1254
|
notFound: _notFound,
|
|
@@ -1307,7 +1321,7 @@ function _defineProperty(obj, key, value) {
|
|
|
1307
1321
|
|
|
1308
1322
|
function getSession(req) {
|
|
1309
1323
|
return __awaiter(this, void 0, void 0, /*#__PURE__*/regenerator.mark(function _callee() {
|
|
1310
|
-
var sealFromCookies, jwt, publicKey, _yield$jwtVerify, payload, session;
|
|
1324
|
+
var sealFromCookies, compressedJwt, jwt, publicKey, _yield$jwtVerify, payload, session;
|
|
1311
1325
|
|
|
1312
1326
|
return regenerator.wrap(function _callee$(_context) {
|
|
1313
1327
|
while (1) {
|
|
@@ -1330,16 +1344,21 @@ function getSession(req) {
|
|
|
1330
1344
|
});
|
|
1331
1345
|
|
|
1332
1346
|
case 6:
|
|
1333
|
-
|
|
1347
|
+
compressedJwt = _context.sent;
|
|
1334
1348
|
_context.next = 9;
|
|
1335
|
-
return
|
|
1349
|
+
return uncompress(compressedJwt);
|
|
1336
1350
|
|
|
1337
1351
|
case 9:
|
|
1338
|
-
|
|
1352
|
+
jwt = _context.sent;
|
|
1339
1353
|
_context.next = 12;
|
|
1340
|
-
return
|
|
1354
|
+
return fronteggConfig.getJwtPublicKey();
|
|
1341
1355
|
|
|
1342
1356
|
case 12:
|
|
1357
|
+
publicKey = _context.sent;
|
|
1358
|
+
_context.next = 15;
|
|
1359
|
+
return jwtVerify(jwt, publicKey);
|
|
1360
|
+
|
|
1361
|
+
case 15:
|
|
1343
1362
|
_yield$jwtVerify = _context.sent;
|
|
1344
1363
|
payload = _yield$jwtVerify.payload;
|
|
1345
1364
|
session = {
|
|
@@ -1348,27 +1367,27 @@ function getSession(req) {
|
|
|
1348
1367
|
};
|
|
1349
1368
|
|
|
1350
1369
|
if (!(session.user.exp * 1000 < Date.now())) {
|
|
1351
|
-
_context.next =
|
|
1370
|
+
_context.next = 20;
|
|
1352
1371
|
break;
|
|
1353
1372
|
}
|
|
1354
1373
|
|
|
1355
1374
|
return _context.abrupt("return", undefined);
|
|
1356
1375
|
|
|
1357
|
-
case
|
|
1376
|
+
case 20:
|
|
1358
1377
|
return _context.abrupt("return", session);
|
|
1359
1378
|
|
|
1360
|
-
case
|
|
1361
|
-
_context.prev =
|
|
1379
|
+
case 23:
|
|
1380
|
+
_context.prev = 23;
|
|
1362
1381
|
_context.t0 = _context["catch"](0);
|
|
1363
1382
|
console.error(_context.t0);
|
|
1364
1383
|
return _context.abrupt("return", undefined);
|
|
1365
1384
|
|
|
1366
|
-
case
|
|
1385
|
+
case 27:
|
|
1367
1386
|
case "end":
|
|
1368
1387
|
return _context.stop();
|
|
1369
1388
|
}
|
|
1370
1389
|
}
|
|
1371
|
-
}, _callee, null, [[0,
|
|
1390
|
+
}, _callee, null, [[0, 23]]);
|
|
1372
1391
|
}));
|
|
1373
1392
|
}
|
|
1374
1393
|
function withSSRSession(handler) {
|
|
@@ -1621,11 +1640,33 @@ function removeCookies(cookieName, isSecured, cookieDomain, res) {
|
|
|
1621
1640
|
|
|
1622
1641
|
res.setHeader('set-cookie', [].concat(_toConsumableArray(existingSetCookie), [cookieValue]));
|
|
1623
1642
|
}
|
|
1643
|
+
function compress(input) {
|
|
1644
|
+
return new Promise(function (resolve, reject) {
|
|
1645
|
+
zlib.brotliCompress(input, function (error, result) {
|
|
1646
|
+
if (error) {
|
|
1647
|
+
reject(error);
|
|
1648
|
+
} else {
|
|
1649
|
+
resolve(result.toString('base64'));
|
|
1650
|
+
}
|
|
1651
|
+
});
|
|
1652
|
+
});
|
|
1653
|
+
}
|
|
1654
|
+
function uncompress(input) {
|
|
1655
|
+
return new Promise(function (resolve, reject) {
|
|
1656
|
+
zlib.brotliDecompress(Buffer.from(input, 'base64'), function (error, result) {
|
|
1657
|
+
if (error) {
|
|
1658
|
+
reject(error);
|
|
1659
|
+
} else {
|
|
1660
|
+
resolve(result.toString('utf-8'));
|
|
1661
|
+
}
|
|
1662
|
+
});
|
|
1663
|
+
});
|
|
1664
|
+
}
|
|
1624
1665
|
function createSessionFromAccessToken(output) {
|
|
1625
1666
|
var _a;
|
|
1626
1667
|
|
|
1627
1668
|
return __awaiter(this, void 0, void 0, /*#__PURE__*/regenerator.mark(function _callee2() {
|
|
1628
|
-
var data, accessToken, decodedJwt, session;
|
|
1669
|
+
var data, accessToken, decodedJwt, compressedAccessToken, session;
|
|
1629
1670
|
return regenerator.wrap(function _callee2$(_context2) {
|
|
1630
1671
|
while (1) {
|
|
1631
1672
|
switch (_context2.prev = _context2.next) {
|
|
@@ -1636,26 +1677,31 @@ function createSessionFromAccessToken(output) {
|
|
|
1636
1677
|
decodedJwt = decodeJwt(accessToken);
|
|
1637
1678
|
decodedJwt.expiresIn = Math.floor((decodedJwt.exp * 1000 - Date.now()) / 1000);
|
|
1638
1679
|
_context2.next = 7;
|
|
1639
|
-
return
|
|
1680
|
+
return compress(accessToken);
|
|
1681
|
+
|
|
1682
|
+
case 7:
|
|
1683
|
+
compressedAccessToken = _context2.sent;
|
|
1684
|
+
_context2.next = 10;
|
|
1685
|
+
return sealData(compressedAccessToken, {
|
|
1640
1686
|
password: fronteggConfig.passwordsAsMap,
|
|
1641
1687
|
ttl: decodedJwt.exp
|
|
1642
1688
|
});
|
|
1643
1689
|
|
|
1644
|
-
case
|
|
1690
|
+
case 10:
|
|
1645
1691
|
session = _context2.sent;
|
|
1646
1692
|
return _context2.abrupt("return", [session, decodedJwt]);
|
|
1647
1693
|
|
|
1648
|
-
case
|
|
1649
|
-
_context2.prev =
|
|
1694
|
+
case 14:
|
|
1695
|
+
_context2.prev = 14;
|
|
1650
1696
|
_context2.t0 = _context2["catch"](0);
|
|
1651
1697
|
return _context2.abrupt("return", []);
|
|
1652
1698
|
|
|
1653
|
-
case
|
|
1699
|
+
case 17:
|
|
1654
1700
|
case "end":
|
|
1655
1701
|
return _context2.stop();
|
|
1656
1702
|
}
|
|
1657
1703
|
}
|
|
1658
|
-
}, _callee2, null, [[0,
|
|
1704
|
+
}, _callee2, null, [[0, 14]]);
|
|
1659
1705
|
}));
|
|
1660
1706
|
}
|
|
1661
1707
|
var modifySetCookieIfUnsecure = function modifySetCookieIfUnsecure(setCookieValue, isSecured) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontegg/nextjs",
|
|
3
|
-
"version": "5.4.
|
|
3
|
+
"version": "5.4.1",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"@frontegg/admin-portal": "5.54.2",
|
|
6
6
|
"@frontegg/react-hooks": "5.54.2",
|
|
@@ -16,4 +16,4 @@
|
|
|
16
16
|
"main": "./index.cjs.js",
|
|
17
17
|
"module": "./index.esm.js",
|
|
18
18
|
"typings": "./index.d.ts"
|
|
19
|
-
}
|
|
19
|
+
}
|