@propelauth/nextjs 0.3.10-withjs.0 → 0.3.11
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 +117 -246
- package/dist/client/index.js +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +1 -1
- package/dist/client/index.mjs.map +1 -1
- package/dist/server/app-router/index.d.ts +7 -8
- package/dist/server/app-router/index.js +3 -3
- package/dist/server/app-router/index.js.map +1 -1
- package/dist/server/app-router/index.mjs +3 -3
- package/dist/server/app-router/index.mjs.map +1 -1
- package/dist/server/index.d.ts +2 -1
- package/dist/server/index.js +3 -3
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +3 -3
- package/dist/server/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
+
<p align="center">
|
2
|
+
<a href="https://www.propelauth.com?ref=github" target="_blank" align="center">
|
3
|
+
<img src="https://www.propelauth.com/imgs/lockup.svg" width="200">
|
4
|
+
</a>
|
5
|
+
</p>
|
6
|
+
|
7
|
+
|
1
8
|
# PropelAuth Next.js (v13+) Library
|
2
9
|
|
3
10
|
[PropelAuth](https://www.propelauth.com?utm_source=github&utm_medium=library&utm_campaign=nextjs) is a user management and authentication service for your B2B/multi-tenant applications.
|
4
11
|
|
5
|
-
This library provides a simple way to integrate your Next.js application (either
|
6
|
-
|
7
|
-
Next.js SSR/AppRouter support is currently in beta.
|
12
|
+
This library provides a simple way to integrate your Next.js application (either App Router or Pages) with PropelAuth.
|
8
13
|
|
9
14
|
## Installation
|
10
15
|
|
@@ -12,297 +17,163 @@ Next.js SSR/AppRouter support is currently in beta.
|
|
12
17
|
npm install @propelauth/nextjs
|
13
18
|
```
|
14
19
|
|
15
|
-
##
|
16
|
-
|
17
|
-
Before you start, make sure you have a PropelAuth account. You can sign up for free at [here](https://auth.propelauth.com).
|
18
|
-
|
19
|
-
You'll need to set the following .env variables in your Next.js application:
|
20
|
-
|
21
|
-
- NEXT_PUBLIC_AUTH_URL
|
22
|
-
- PROPELAUTH_API_KEY
|
23
|
-
- PROPELAUTH_VERIFIER_KEY
|
24
|
-
- PROPELAUTH_REDIRECT_URI
|
25
|
-
|
26
|
-
You can find the NEXT_PUBLIC_AUTH_URL, PROPELAUTH_API_KEY, and PROPELAUTH_VERIFIER_KEY variables for your application in the PropelAuth Dashboard under Backend Integration.
|
27
|
-
|
28
|
-
When you copy the PROPELAUTH_VERIFIER_KEY from the PropelAuth dashboard, it will automatically paste into your .env file with line breaks. However, due to the way some systems interpret multiline environment variables, you will need to edit the verifier key value to include ‘\n’ instead of newline characters. For example:
|
29
|
-
|
30
|
-
```
|
31
|
-
PROPELAUTH_VERIFIER_KEY=-----BEGIN PUBLIC KEY-----\nMIIBIjANBgk...
|
32
|
-
```
|
20
|
+
## Automatic Installation
|
33
21
|
|
34
|
-
If
|
22
|
+
If you would prefer a manual installation process instead of an automatic one, check out the installation guides for [App Router](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/installation-and-setup) and [Pages Router](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/installation-and-setup).
|
35
23
|
|
36
|
-
|
24
|
+
Begin by installing the PropelAuth CLI:
|
37
25
|
|
38
|
-
|
39
|
-
|
40
|
-
In your `src/app/api/auth/[slug]` directory, create a file called `route.ts` with the following content:
|
41
|
-
|
42
|
-
```typescript
|
43
|
-
import { getRouteHandlers } from '@propelauth/nextjs/server/app-router'
|
44
|
-
import { NextRequest } from 'next/server'
|
45
|
-
|
46
|
-
// postLoginRedirectPathFn is optional, but if you want to redirect the user to a different page after login, you can do so here.
|
47
|
-
const routeHandlers = getRouteHandlers({
|
48
|
-
postLoginRedirectPathFn: (req: NextRequest) => {
|
49
|
-
return '/'
|
50
|
-
},
|
51
|
-
})
|
52
|
-
export const GET = routeHandlers.getRouteHandler
|
53
|
-
export const POST = routeHandlers.postRouteHandler
|
26
|
+
```bash
|
27
|
+
npm i -g @propelauth/cli
|
54
28
|
```
|
55
29
|
|
56
|
-
|
30
|
+
## Logging into the PropelAuth CLI
|
57
31
|
|
58
|
-
|
32
|
+
Before we install PropelAuth in your Next.js project we first have to log into the CLI. If you haven't already created an account in PropelAuth, navigate to [auth.propelauth.com](https://auth.propelauth.com/en/signup) to get started.
|
59
33
|
|
60
|
-
|
34
|
+
Once you have an account with PropelAuth, run this command to login to the CLI:
|
61
35
|
|
62
|
-
```
|
63
|
-
|
64
|
-
return (
|
65
|
-
<html lang="en">
|
66
|
-
<AuthProvider authUrl={process.env.NEXT_PUBLIC_AUTH_URL}>
|
67
|
-
<body className={inter.className}>{children}</body>
|
68
|
-
</AuthProvider>
|
69
|
-
</html>
|
70
|
-
)
|
71
|
-
}
|
36
|
+
```bash
|
37
|
+
propelauth login
|
72
38
|
```
|
73
39
|
|
74
|
-
|
75
|
-
|
76
|
-
In your `_app.tsx` file, add the `AuthProvider`:
|
40
|
+
To login you'll be prompted to create and copy/paste a Personal API Key into your terminal.
|
77
41
|
|
78
|
-
```typescript jsx
|
79
|
-
export default function MyApp({Component, pageProps}: AppProps) {
|
80
|
-
return (
|
81
|
-
<AuthProvider authUrl={process.env.NEXT_PUBLIC_AUTH_URL}>
|
82
|
-
<Component {...pageProps} />
|
83
|
-
</AuthProvider>
|
84
|
-
)
|
85
|
-
}
|
86
42
|
```
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
// It should be called on every request that requires authentication AND /api/auth/.* routes.
|
99
|
-
export const config = {
|
100
|
-
matcher: [
|
101
|
-
// REQUIRED: Match all request paths that start with /api/auth/
|
102
|
-
'/api/auth/(.*)',
|
103
|
-
// OPTIONAL: Don't match any static assets
|
104
|
-
'/((?!_next/static|_next/image|favicon.ico).*)',
|
105
|
-
],
|
106
|
-
}
|
43
|
+
┌ ⚡ PropelAuth Login
|
44
|
+
│
|
45
|
+
●
|
46
|
+
│ Please visit the following URL to create a personal API key:
|
47
|
+
│
|
48
|
+
● https://auth.propelauth.com/api_keys/personal
|
49
|
+
│
|
50
|
+
│
|
51
|
+
◆ Enter your API key
|
52
|
+
│ # enter your API key here
|
53
|
+
└
|
107
54
|
```
|
108
55
|
|
109
|
-
|
56
|
+
You can create a Personal API Key by navigating to https://auth.propelauth.com/api_keys/personal and clicking **+ New API Key**.
|
110
57
|
|
111
|
-
### Get the user in Server Components (App Router example)
|
112
58
|
|
113
|
-
|
114
|
-
import { getUser } from '@propelauth/nextjs/server/app-router'
|
59
|
+
### Selecting a Default Project
|
115
60
|
|
116
|
-
|
117
|
-
const user = await getUser()
|
61
|
+
Once your API Key is validated the CLI will prompt you to select a default project, if desired. If you select a default project, the CLI will not prompt you to select a project again until you repeat the login process or run the `set-default-project` command.
|
118
62
|
|
119
|
-
if (user) {
|
120
|
-
return <div>Hello {user.firstName}!</div>
|
121
|
-
} else {
|
122
|
-
return <div>Please log in to be welcomed</div>
|
123
|
-
}
|
124
|
-
}
|
125
63
|
```
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
return <div>Hello {user.firstName}!</div>
|
135
|
-
}
|
64
|
+
┌
|
65
|
+
◇ ✓ Projects fetched successfully
|
66
|
+
│
|
67
|
+
◆ Select a project to use
|
68
|
+
│ ● Always ask which project to use (You will be prompted for each command)
|
69
|
+
│ ○ Acme Inc / New Project
|
70
|
+
└
|
136
71
|
```
|
137
72
|
|
138
|
-
|
73
|
+
## Installing and Setting up PropelAuth in Next.js
|
139
74
|
|
140
|
-
|
141
|
-
import { GetServerSideProps, InferGetServerSidePropsType } from 'next'
|
142
|
-
import { getUserFromServerSideProps } from '@propelauth/nextjs/server/pages'
|
143
|
-
import { User } from '@propelauth/nextjs/client'
|
75
|
+
Once you have logged into the CLI it's time to install PropelAuth within your Next.js app! Navigate to your Next.js project directory and run the following command:
|
144
76
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
77
|
+
```bash
|
78
|
+
propelauth setup
|
79
|
+
```
|
80
|
+
|
81
|
+
During installation you'll see the following prompts:
|
82
|
+
|
83
|
+
```
|
84
|
+
┌ ⚡ PropelAuth Setup
|
85
|
+
│
|
86
|
+
◆ Select your project framework:
|
87
|
+
│ ○ Next.js (App Router)
|
88
|
+
│ ● Next.js (Pages Router) (Uses pages/ directory structure)
|
89
|
+
│
|
90
|
+
◆ Select a project to use for this command
|
91
|
+
│ ● Acme Inc / New Project
|
92
|
+
│
|
93
|
+
◆ Enter the URL your Next.js app runs on:
|
94
|
+
│ ↳ http://localhost:3000
|
95
|
+
│
|
96
|
+
● No API key found in environment file
|
97
|
+
│
|
98
|
+
◆ Would you like to generate a new Backend Integration API Key for this project?
|
99
|
+
│ ● yes / ○ no
|
100
|
+
│
|
101
|
+
│ Enter a name for the new Backend Integration API Key:
|
102
|
+
│ ↳ Next.js Integration
|
103
|
+
│
|
104
|
+
● Updates needed for the Frontend Integration settings for your test environment
|
105
|
+
│ - Login redirect path: / → /api/auth/callback
|
106
|
+
│ - Logout redirect path: / → /api/auth/logout
|
107
|
+
│ - Development URL: none → http://localhost:3000
|
108
|
+
│
|
109
|
+
◆ Your test environment config needs to be updated, would you like to apply these changes now?
|
110
|
+
│ ● yes / ○ no
|
111
|
+
│
|
112
|
+
◆ Install @propelauth/nextjs now?
|
113
|
+
│ ● npm install @propelauth/nextjs (detected)
|
114
|
+
│ ○ yarn add @propelauth/nextjs
|
115
|
+
│ ○ pnpm install @propelauth/nextjs
|
116
|
+
│ ○ bun install @propelauth/nextjs
|
117
|
+
│ ○ Skip installation
|
118
|
+
│
|
119
|
+
● _app.tsx with AuthProvider at /src/pages/_app.tsx differs from what we expected.
|
120
|
+
│
|
121
|
+
◆ Overwrite _app.tsx with AuthProvider?
|
122
|
+
│ ● Yes / ○ No
|
123
|
+
│
|
124
|
+
└ PropelAuth has been successfully set up in your Next.js project!
|
125
|
+
```
|
126
|
+
|
127
|
+
Once the CLI has successfully installed PropelAuth within your Next.js project it will also include some example components to help you get started.
|
128
|
+
|
129
|
+
### App Router Server Component
|
130
|
+
|
131
|
+
```jsx {{ title: "Server Component" }}
|
132
|
+
import { getUserOrRedirect } from "@propelauth/nextjs/server/app-router";
|
150
133
|
|
151
|
-
|
152
|
-
const user = await
|
153
|
-
|
154
|
-
return { redirect: { destination: '/api/auth/login', permanent: false } }
|
155
|
-
}
|
156
|
-
return { props: { userJson: JSON.stringify(user) } }
|
134
|
+
const WelcomeMessage = async () => {
|
135
|
+
const user = await getUserOrRedirect()
|
136
|
+
return <div>Welcome, {user.email}!</div>
|
157
137
|
}
|
158
|
-
```
|
159
|
-
|
160
|
-
### Get the user in API Routes (Pages example)
|
161
138
|
|
162
|
-
|
163
|
-
import { NextApiRequest, NextApiResponse } from 'next'
|
164
|
-
import { getUserFromApiRouteRequest } from '@propelauth/nextjs/server/pages'
|
165
|
-
|
166
|
-
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
167
|
-
const user = await getUserFromApiRouteRequest(req, res)
|
168
|
-
if (user) {
|
169
|
-
res.status(200).json({ email: user.email })
|
170
|
-
} else {
|
171
|
-
res.status(401).json({ error: 'unauthorized' })
|
172
|
-
}
|
173
|
-
}
|
139
|
+
export default WelcomeMessage;
|
174
140
|
```
|
175
141
|
|
176
|
-
###
|
142
|
+
### App Router Client Component
|
177
143
|
|
178
|
-
```
|
179
|
-
|
144
|
+
```jsx
|
145
|
+
"use client";
|
180
146
|
|
181
|
-
import { useUser } from
|
147
|
+
import { useUser } from "@propelauth/nextjs/client";
|
182
148
|
|
183
149
|
const WelcomeMessage = () => {
|
184
|
-
const {
|
185
|
-
|
150
|
+
const {loading, user} = useUser()
|
186
151
|
if (loading) {
|
187
152
|
return <div>Loading...</div>
|
188
153
|
} else if (user) {
|
189
|
-
return <div>
|
154
|
+
return <div>Welcome, {user.email}!</div>
|
190
155
|
} else {
|
191
156
|
return <div>Please log in to be welcomed</div>
|
192
157
|
}
|
193
158
|
}
|
194
|
-
```
|
195
|
-
|
196
|
-
### Checking organization membership / RBAC
|
197
|
-
|
198
|
-
Note that this works on both the client's `User` object or the client/server `UserFromToken` object, but the below example is on the server.
|
199
|
-
|
200
|
-
If you are curious where the organization information comes from, check out our documentation on [organizations](https://docs.propelauth.com/overview/organizations?utm_source=github&utm_medium=library&utm_campaign=nextjs).
|
201
|
-
The quick answer is:
|
202
|
-
|
203
|
-
- PropelAuth provides UI for users to create organizations and invite other users to join them.
|
204
|
-
- Your users can also create Enterprise SSO/SAML connections to their own Identity Providers (IdPs) so their organization members can log in with their existing work credentials.
|
205
|
-
- You can create organizations and add users to them via our APIs or our Dashboard.
|
206
|
-
|
207
|
-
```tsx
|
208
|
-
// src/app/org/[slug]/page.tsx
|
209
|
-
import { getUserOrRedirect } from '@propelauth/nextjs/server/app-router'
|
210
|
-
|
211
|
-
export default async function AdminOnlyPage({ params }: { params: { slug: string } }) {
|
212
|
-
const user = await getUserOrRedirect()
|
213
|
-
const org = user.getOrgByName(params.slug)
|
214
|
-
const isAdmin = org?.isRole('Admin')
|
215
|
-
|
216
|
-
if (!isAdmin) {
|
217
|
-
return <div>Not found</div>
|
218
|
-
} else {
|
219
|
-
return (
|
220
|
-
<div>
|
221
|
-
Welcome {user.firstName}, Admin of {org?.orgName}
|
222
|
-
</div>
|
223
|
-
)
|
224
|
-
}
|
225
|
-
}
|
226
|
-
```
|
227
|
-
|
228
|
-
### Logging out
|
229
|
-
|
230
|
-
```tsx
|
231
|
-
'use client'
|
232
|
-
|
233
|
-
import { useLogoutFunction } from '@propelauth/nextjs/client'
|
234
|
-
|
235
|
-
export default function LogoutButton() {
|
236
|
-
const logoutFn = useLogoutFunction()
|
237
|
-
return <button onClick={logoutFn}>Logout</button>
|
238
|
-
}
|
239
|
-
```
|
240
|
-
|
241
|
-
### Logging in / Signing up
|
242
|
-
|
243
|
-
If you don't want to use redirect functions, you can also use `useHostedPageUrls` which will return the URLs instead of redirecting.
|
244
|
-
|
245
|
-
```tsx
|
246
|
-
'use client'
|
247
159
|
|
248
|
-
|
249
|
-
|
250
|
-
export default function SignupAndLoginButtons() {
|
251
|
-
const { redirectToSignupPage, redirectToLoginPage } = useRedirectFunctions()
|
252
|
-
return (
|
253
|
-
<>
|
254
|
-
<button onClick={redirectToSignupPage}>Sign up</button>
|
255
|
-
<button onClick={redirectToLoginPage}>Log in</button>
|
256
|
-
</>
|
257
|
-
)
|
258
|
-
}
|
160
|
+
export default WelcomeMessage;
|
259
161
|
```
|
260
162
|
|
261
|
-
|
262
|
-
|
263
|
-
PropelAuth also provides you with pre-built account and organization management UIs.
|
264
|
-
You can redirect your users to these pages by using the following functions:
|
163
|
+
## Calling Backend APIs
|
265
164
|
|
266
|
-
|
267
|
-
|
165
|
+
You can also use the library to call the PropelAuth APIs directly, allowing you to fetch users, create orgs, and a lot more.
|
166
|
+
See the [API Reference](https://docs.propelauth.com/reference) for more information.
|
268
167
|
|
269
|
-
|
270
|
-
|
271
|
-
export default function AccountAndOrgButtons() {
|
272
|
-
const { redirectToAccountPage, redirectToOrgPage } = useRedirectFunctions()
|
273
|
-
return (
|
274
|
-
<>
|
275
|
-
<button onClick={redirectToAccountPage}>Account</button>
|
276
|
-
<button onClick={redirectToOrgPage}>Organization</button>
|
277
|
-
</>
|
278
|
-
)
|
279
|
-
}
|
280
|
-
```
|
281
|
-
|
282
|
-
### Using APIs
|
283
|
-
|
284
|
-
You can use our [APIs](https://docs.propelauth.com/reference/backend-apis/node) like so:
|
285
|
-
|
286
|
-
```ts
|
287
|
-
import { getPropelAuthApis } from '@propelauth/nextjs/server'
|
168
|
+
```typescript
|
169
|
+
import { getPropelAuthApis } from "@propelauth/nextjs/server";
|
288
170
|
|
171
|
+
// Can be done in an API route or getServerSideProps
|
289
172
|
const apis = getPropelAuthApis()
|
290
173
|
await apis.disableUser(userId)
|
291
174
|
```
|
292
175
|
|
293
|
-
|
176
|
+
## Questions?
|
294
177
|
|
295
|
-
|
296
|
-
to an external API, you'll need an access token. You can get an access token on the frontend from the `useUser` hook:
|
297
|
-
|
298
|
-
```tsx
|
299
|
-
import { useUser } from '@propelauth/nextjs/client'
|
300
|
-
|
301
|
-
const MyComponent = () => {
|
302
|
-
const { loading, accessToken } = useUser()
|
303
|
-
|
304
|
-
// Make a request to an external API with useEffect, useQuery, etc.
|
305
|
-
}
|
306
|
-
```
|
178
|
+
Feel free to reach out at support@propelauth.com
|
307
179
|
|
308
|
-
Within the App Router, you can also call `getAccessToken` to get the access token.
|
package/dist/client/index.js
CHANGED
@@ -300,7 +300,7 @@ function toOrgIdToOrgMemberInfo(snake_case) {
|
|
300
300
|
|
301
301
|
// src/client/AuthProvider.tsx
|
302
302
|
var import_react2 = __toESM(require("react"));
|
303
|
-
var import_navigation = require("next/navigation
|
303
|
+
var import_navigation = require("next/navigation");
|
304
304
|
|
305
305
|
// src/client/utils.ts
|
306
306
|
function hasWindow() {
|