@frontegg/nextjs 5.4.1-alpha.2582459293 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -214
- package/index.cjs.js +12 -4
- package/index.esm.js +12 -4
- package/package.json +4 -4
- package/withFronteggApp.d.ts +5 -3
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/index.cjs.js
CHANGED
|
@@ -1258,12 +1258,14 @@ function FronteggRouter() {
|
|
|
1258
1258
|
return '';
|
|
1259
1259
|
}
|
|
1260
1260
|
function FronteggRouterProps(context) {
|
|
1261
|
+
var _a;
|
|
1262
|
+
|
|
1261
1263
|
var routesObj = Object.assign(Object.assign({}, reduxStore.authInitialState.routes), fronteggConfig.authRoutes);
|
|
1262
1264
|
var routesArr = Object.keys(routesObj).reduce(function (p, key) {
|
|
1263
1265
|
return [].concat(_toConsumableArray(p), [routesObj[key]]);
|
|
1264
1266
|
}, []);
|
|
1265
1267
|
|
|
1266
|
-
var _parse = url.parse(context.req.url, true),
|
|
1268
|
+
var _parse = url.parse((_a = context.resolvedUrl) !== null && _a !== void 0 ? _a : context.req.url, true),
|
|
1267
1269
|
pathname = _parse.pathname;
|
|
1268
1270
|
|
|
1269
1271
|
if (!pathname || pathname.startsWith('/_next/data')) {
|
|
@@ -1421,7 +1423,7 @@ function withSSRSession(handler) {
|
|
|
1421
1423
|
|
|
1422
1424
|
return function (context) {
|
|
1423
1425
|
return __awaiter(_this, void 0, void 0, /*#__PURE__*/regenerator.mark(function _callee2() {
|
|
1424
|
-
var _a, session;
|
|
1426
|
+
var _a, _b, session, loginUrl;
|
|
1425
1427
|
|
|
1426
1428
|
return regenerator.wrap(function _callee2$(_context2) {
|
|
1427
1429
|
while (1) {
|
|
@@ -1441,15 +1443,21 @@ function withSSRSession(handler) {
|
|
|
1441
1443
|
return _context2.abrupt("return", handler(context, session));
|
|
1442
1444
|
|
|
1443
1445
|
case 7:
|
|
1446
|
+
loginUrl = (_a = fronteggConfig.authRoutes.loginUrl) !== null && _a !== void 0 ? _a : reduxStore.authInitialState.routes.loginUrl;
|
|
1447
|
+
|
|
1448
|
+
if (!loginUrl.startsWith('/')) {
|
|
1449
|
+
loginUrl = "/".concat(loginUrl);
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1444
1452
|
return _context2.abrupt("return", {
|
|
1445
1453
|
redirect: {
|
|
1446
1454
|
permanent: false,
|
|
1447
|
-
destination: "
|
|
1455
|
+
destination: "".concat(loginUrl, "?redirectUrl=").concat(encodeURIComponent((_b = context.resolvedUrl) !== null && _b !== void 0 ? _b : context.req.url))
|
|
1448
1456
|
},
|
|
1449
1457
|
props: {}
|
|
1450
1458
|
});
|
|
1451
1459
|
|
|
1452
|
-
case
|
|
1460
|
+
case 10:
|
|
1453
1461
|
case "end":
|
|
1454
1462
|
return _context2.stop();
|
|
1455
1463
|
}
|
package/index.esm.js
CHANGED
|
@@ -1232,12 +1232,14 @@ function FronteggRouter() {
|
|
|
1232
1232
|
return '';
|
|
1233
1233
|
}
|
|
1234
1234
|
function FronteggRouterProps(context) {
|
|
1235
|
+
var _a;
|
|
1236
|
+
|
|
1235
1237
|
var routesObj = Object.assign(Object.assign({}, authInitialState.routes), fronteggConfig.authRoutes);
|
|
1236
1238
|
var routesArr = Object.keys(routesObj).reduce(function (p, key) {
|
|
1237
1239
|
return [].concat(_toConsumableArray(p), [routesObj[key]]);
|
|
1238
1240
|
}, []);
|
|
1239
1241
|
|
|
1240
|
-
var _parse = parse(context.req.url, true),
|
|
1242
|
+
var _parse = parse((_a = context.resolvedUrl) !== null && _a !== void 0 ? _a : context.req.url, true),
|
|
1241
1243
|
pathname = _parse.pathname;
|
|
1242
1244
|
|
|
1243
1245
|
if (!pathname || pathname.startsWith('/_next/data')) {
|
|
@@ -1395,7 +1397,7 @@ function withSSRSession(handler) {
|
|
|
1395
1397
|
|
|
1396
1398
|
return function (context) {
|
|
1397
1399
|
return __awaiter(_this, void 0, void 0, /*#__PURE__*/regenerator.mark(function _callee2() {
|
|
1398
|
-
var _a, session;
|
|
1400
|
+
var _a, _b, session, loginUrl;
|
|
1399
1401
|
|
|
1400
1402
|
return regenerator.wrap(function _callee2$(_context2) {
|
|
1401
1403
|
while (1) {
|
|
@@ -1415,15 +1417,21 @@ function withSSRSession(handler) {
|
|
|
1415
1417
|
return _context2.abrupt("return", handler(context, session));
|
|
1416
1418
|
|
|
1417
1419
|
case 7:
|
|
1420
|
+
loginUrl = (_a = fronteggConfig.authRoutes.loginUrl) !== null && _a !== void 0 ? _a : authInitialState.routes.loginUrl;
|
|
1421
|
+
|
|
1422
|
+
if (!loginUrl.startsWith('/')) {
|
|
1423
|
+
loginUrl = "/".concat(loginUrl);
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1418
1426
|
return _context2.abrupt("return", {
|
|
1419
1427
|
redirect: {
|
|
1420
1428
|
permanent: false,
|
|
1421
|
-
destination: "
|
|
1429
|
+
destination: "".concat(loginUrl, "?redirectUrl=").concat(encodeURIComponent((_b = context.resolvedUrl) !== null && _b !== void 0 ? _b : context.req.url))
|
|
1422
1430
|
},
|
|
1423
1431
|
props: {}
|
|
1424
1432
|
});
|
|
1425
1433
|
|
|
1426
|
-
case
|
|
1434
|
+
case 10:
|
|
1427
1435
|
case "end":
|
|
1428
1436
|
return _context2.stop();
|
|
1429
1437
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontegg/nextjs",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.0",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@frontegg/admin-portal": "5.
|
|
6
|
-
"@frontegg/react-hooks": "5.
|
|
5
|
+
"@frontegg/admin-portal": "5.64.1",
|
|
6
|
+
"@frontegg/react-hooks": "5.64.1",
|
|
7
7
|
"jose": "^4.8.0",
|
|
8
8
|
"iron-session": "^6.1.2",
|
|
9
9
|
"http-proxy": "^1.18.1",
|
|
@@ -16,4 +16,4 @@
|
|
|
16
16
|
"main": "./index.cjs.js",
|
|
17
17
|
"module": "./index.esm.js",
|
|
18
18
|
"typings": "./index.d.ts"
|
|
19
|
-
}
|
|
19
|
+
}
|
package/withFronteggApp.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { AppContextType, AppPropsType, NextComponentType } from 'next/dist/shared/lib/utils';
|
|
1
|
+
import type { AppProps, AppInitialProps } from 'next/app';
|
|
2
|
+
import type { AppType, AppContextType, AppPropsType, NextComponentType } from 'next/dist/shared/lib/utils';
|
|
3
3
|
import { FronteggNextJSSession } from './types';
|
|
4
4
|
import { FronteggAppOptions } from '@frontegg/types';
|
|
5
|
-
export declare const withFronteggApp: (app:
|
|
5
|
+
export declare const withFronteggApp: (app: ((props: AppProps) => JSX.Element) & {
|
|
6
|
+
getInitialProps?: AppType['getInitialProps'];
|
|
7
|
+
}, options?: (Omit<FronteggAppOptions, "contextOptions"> & {
|
|
6
8
|
contextOptions?: import("@frontegg/types/ContextOptions").ContextOptions | undefined;
|
|
7
9
|
}) | undefined) => NextComponentType<AppContextType & {
|
|
8
10
|
session: FronteggNextJSSession | null;
|