@frontegg/nextjs 4.0.18-alpha.2289216589 → 5.0.1-alpha.2295828351

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.
@@ -0,0 +1,26 @@
1
+ import { KeyLike } from 'jose';
2
+ import { AuthPageRoutes } from '@frontegg/redux-store';
3
+ export declare type PasswordsMap = {
4
+ [id: string]: string;
5
+ };
6
+ export declare type Password = string | PasswordsMap;
7
+ export declare function normalizeStringPasswordToMap(password: Password): PasswordsMap;
8
+ declare class FronteggConfig {
9
+ private readonly _cookieName;
10
+ private readonly _password;
11
+ private _jwtPublicKey;
12
+ private readonly _passwordsAsMap;
13
+ private readonly _clientId;
14
+ authRoutes: Partial<AuthPageRoutes>;
15
+ constructor();
16
+ get cookieName(): string;
17
+ get password(): string;
18
+ get clientId(): string;
19
+ get baseUrlHost(): string;
20
+ get appUrl(): string;
21
+ get cookieDomain(): string;
22
+ getJwtPublicKey(): Promise<KeyLike | Uint8Array>;
23
+ get passwordsAsMap(): PasswordsMap;
24
+ }
25
+ declare const _default: FronteggConfig;
26
+ export default _default;
@@ -0,0 +1,20 @@
1
+ import type { NextApiRequest, NextApiResponse } from 'next';
2
+ /**
3
+ * If pattern information matching the input url information is found in the `pathRewrite` array,
4
+ * the url value is partially replaced with the `pathRewrite.replaceStr` value.
5
+ * @param url
6
+ * @param pathRewrite
7
+ */
8
+ export declare const rewritePath: (url: string, pathRewrite: {
9
+ [key: string]: string;
10
+ } | {
11
+ patternStr: string;
12
+ replaceStr: string;
13
+ }[]) => string;
14
+ /**
15
+ * Next.js HTTP Proxy Middleware
16
+ * @see https://nextjs.org/docs/api-routes/api-middlewares
17
+ * @param {NextApiRequest} req
18
+ * @param {NextApiResponse} res
19
+ */
20
+ export declare function fronteggMiddleware(req: NextApiRequest, res: NextApiResponse): Promise<any>;
@@ -1,13 +1,18 @@
1
1
  import { FC, ReactNode } from 'react';
2
2
  import { FronteggAppOptions } from '@frontegg/types';
3
3
  import { NextRouter } from 'next/router';
4
- export declare type FronteggProviderProps = FronteggAppOptions & {
4
+ import { FronteggNextJSSession } from './types';
5
+ export declare type FronteggProviderProps = Omit<FronteggAppOptions, 'contextOptions'> & {
5
6
  children?: ReactNode;
7
+ session?: FronteggNextJSSession;
8
+ envAppUrl: string;
9
+ envBaseUrl: string;
10
+ envClientId: string;
11
+ contextOptions?: Omit<FronteggAppOptions['contextOptions'], 'baseUrl'>;
6
12
  };
7
- declare type ConnectorProps = FronteggAppOptions & {
13
+ declare type ConnectorProps = FronteggProviderProps & {
8
14
  router: NextRouter;
9
15
  appName?: string;
10
- children?: ReactNode;
11
16
  };
12
17
  export declare const Connector: FC<ConnectorProps>;
13
18
  export declare const FronteggProvider: FC<FronteggProviderProps>;
@@ -0,0 +1,5 @@
1
+ export declare function FronteggRouter(): string;
2
+ export declare function FronteggRouterProps(context: any): {
3
+ notFound: boolean;
4
+ props: {};
5
+ };
package/README.md CHANGED
@@ -1,68 +1,217 @@
1
- # Frontegg Next.js
2
-
3
- ![alt text](https://fronteggstuff.blob.core.windows.net/frongegg-logos/logo-transparent.png)
1
+ ![alt text](./logo.png)
4
2
 
5
3
  Frontegg is a web platform where SaaS companies can set up their fully managed, scalable and brand aware - SaaS features
6
4
  and integrate them into their SaaS portals in up to 5 lines of code.
7
5
 
8
- ### @frontegg/nextjs uses AdminPortal and LoginBox.
6
+ ## Table of Contents
7
+
8
+ - [Installation](#installation)
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)
9
20
 
10
21
  ## Installation
11
22
 
12
- Use the package manager [npm](https://www.npmjs.com/) to install frontegg Next.js library.
23
+ ### Create new NextJS project
24
+
25
+ To start a new Create Next App project with TypeScript, you can run:
13
26
 
14
27
  ```bash
15
- npm install @frontegg/nextjs
28
+ npx create-next-app --example https://github.com/frontegg/frontegg-nextjs --example-path apps/example my-nextjs-app-name
16
29
  ```
17
30
 
18
- ## Configuration
31
+ or
19
32
 
20
- Wrap your application in `_app.js` with `Frontegg Provider`:
33
+ ```bash
34
+ yarn create next-app --example https://github.com/frontegg/frontegg-nextjs --example-path apps/example my-nextjs-app-name
35
+ ```
21
36
 
22
- ```js
23
- import { FronteggProvider } from '@frontegg/nextjs'
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.
24
40
 
25
- const contextOptions = {
26
- baseUrl: 'https://{HOST}.frontegg.com',// Your backend base URL (frontegg will direct the requests to it)
27
- }
41
+ ### Add to existing project
28
42
 
29
- export const App = ({ Component, pageProps }) => {
30
- return <FronteggProvider contextOptions={contextOptions}>
31
- <Component {...pageProps}/>
32
- </FronteggProvider>
33
- }
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
34
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'
35
129
  ```
36
130
 
37
- ### Usage
131
+ ## Documentation
38
132
 
39
- You can use React Hooks to access Frontegg store.
133
+ ### API Reference
40
134
 
41
- ```js
42
- import { useAuthUser } from '@frontegg/nextjs'
135
+ Visit [Frontegg Docs](https://docs.frontegg.com) for the full documentation.
43
136
 
44
- const HomePage = () => {
45
- const user = useAuthUser();
137
+ ### Frontegg Provider Options
46
138
 
47
- return <div>
48
- Logged In user: {user.email}
49
- </div>
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} />;
50
149
  }
150
+
151
+ export default withFronteggApp(CustomApp, {
152
+ /**
153
+ * Frontegg options for customizations
154
+ */
155
+ });
51
156
  ```
52
157
 
53
- Openning the Admin Portal is available via the following code snippet.
158
+ ### getSession
54
159
 
55
- ```js
56
- import { AdminPortal } from '@frontegg/nextjs'
160
+ For any pages that required AccessToken in Server Side, you can use:
57
161
 
58
- const Toolbar = () => {
162
+ ```tsx
163
+ import { GetServerSideProps } from 'next';
164
+ import { getSession } from '@frontegg/nextjs';
59
165
 
60
- return <nav>
61
- {/*... your application tabs ...*/}
166
+ export default function MyPage({ products }) {
167
+ return (
168
+ <div>
169
+ <h1>My Page</h1>
170
+ {products}
171
+ </div>
172
+ );
173
+ }
62
174
 
63
- <button onClick={() => AdminPortal.show()}>
64
- Open Admin Portal
65
- </button>
66
- </nav>
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
+ );
67
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
+ );
68
217
  ```
package/helpers.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import { ServerResponse } from 'http';
3
+ import { NextPageContext } from 'next/dist/shared/lib/utils';
4
+ import { FronteggNextJSSession } from './types';
5
+ export declare function refreshToken(ctx: NextPageContext): Promise<FronteggNextJSSession | null>;
6
+ export declare function addToCookies(cookieValue: string, res: ServerResponse): void;
7
+ export declare function removeCookies(cookieName: string, isSecured: boolean, cookieDomain: string, res: ServerResponse): void;
8
+ export declare function createSessionFromAccessToken(output: string): Promise<[string, any] | []>;
9
+ export declare const modifySetCookieIfUnsecure: (setCookieValue: string[] | undefined, isSecured: boolean) => string[] | undefined;