@propelauth/nextjs 0.0.63 → 0.0.68
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 +32 -37
- package/dist/server/app-router/index.d.ts +75 -0
- package/dist/server/app-router/index.js +624 -0
- package/dist/server/app-router/index.js.map +1 -0
- package/dist/server/app-router/index.mjs +583 -0
- package/dist/server/app-router/index.mjs.map +1 -0
- package/dist/server/index.d.ts +4 -43
- package/dist/server/index.js +56 -423
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +54 -422
- package/dist/server/index.mjs.map +1 -1
- package/dist/server/pages/index.d.ts +47 -0
- package/dist/server/pages/index.js +356 -0
- package/dist/server/pages/index.js.map +1 -0
- package/dist/server/pages/index.mjs +320 -0
- package/dist/server/pages/index.mjs.map +1 -0
- package/package.json +18 -1
package/README.md
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
|
5
5
|
This library provides a simple way to integrate your Next.js application (either AppRouter or Pages) with PropelAuth.
|
6
6
|
|
7
|
+
Next.js SSR/AppRouter support is in beta, while in beta, you'll need to reach out to support@propelauth.com to have this enabled for your account.
|
8
|
+
|
7
9
|
## Installation
|
8
10
|
|
9
11
|
```bash
|
@@ -14,42 +16,35 @@ npm install @propelauth/nextjs
|
|
14
16
|
|
15
17
|
Before you start, make sure you have a PropelAuth account. You can sign up for free at [here](https://auth.propelauth.com).
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
```typescript
|
20
|
-
import {initializeAuth} from "@propelauth/nextjs/server";
|
21
|
-
|
22
|
-
const auth = initializeAuth({
|
23
|
-
authUrl: process.env.NEXT_PUBLIC_AUTH_URL,
|
24
|
-
redirectUri: process.env.REDIRECT_URI,
|
25
|
-
integrationApiKey: process.env.INTEGRATION_API_KEY,
|
26
|
-
verifierKey: process.env.VERIFIER_KEY,
|
27
|
-
})
|
19
|
+
You'll need to set the following .env variables in your Next.js application:
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
getUserOrRedirect,
|
34
|
-
getUserFromServerSideProps,
|
35
|
-
validateAccessToken,
|
36
|
-
validateAccessTokenOrUndefined,
|
37
|
-
authMiddleware,
|
38
|
-
} = auth
|
39
|
-
```
|
21
|
+
- NEXT_PUBLIC_AUTH_URL
|
22
|
+
- PROPELAUTH_API_KEY
|
23
|
+
- PROPELAUTH_VERIFIER_KEY
|
24
|
+
- PROPELAUTH_REDIRECT_URI
|
40
25
|
|
41
|
-
|
42
|
-
|
26
|
+
You can find the env variables for your application in the PropelAuth Dashboard under **Backend Integration**.
|
27
|
+
For the PROPELAUTH_REDIRECT_URI, you should use `{YOUR_URL}/api/auth/callback`.
|
28
|
+
For example, if your application is hosted at `https://myapp.com`, then your PROPELAUTH_REDIRECT_URI would be `https://myapp.com/api/auth/callback`.
|
29
|
+
Make sure to set this in the **Frontend Integration** section of your dashboard.
|
43
30
|
|
44
31
|
### 1. Set up routes
|
45
32
|
|
46
33
|
In your `src/app/api/auth/[slug]` directory, create a file called `route.ts` with the following content:
|
47
34
|
|
48
35
|
```typescript
|
49
|
-
import {
|
50
|
-
|
51
|
-
|
52
|
-
|
36
|
+
import {getRouteHandlers} from "@propelauth/nextjs/server/app-router";
|
37
|
+
import {User} from "@propelauth/nextjs/server";
|
38
|
+
import {NextRequest} from "next/server";
|
39
|
+
|
40
|
+
// postLoginRedirectPathFn is optional, but if you want to redirect the user to a different page after login, you can do so here.
|
41
|
+
const routeHandlers = getRouteHandlers({
|
42
|
+
postLoginRedirectPathFn: (user: User, req: NextRequest) => {
|
43
|
+
return "/"
|
44
|
+
}
|
45
|
+
})
|
46
|
+
export const GET = routeHandlers.getRouteHandler
|
47
|
+
export const POST = routeHandlers.postRouteHandler
|
53
48
|
```
|
54
49
|
|
55
50
|
### 2. Set up AuthProvider
|
@@ -89,7 +84,7 @@ export default function MyApp({Component, pageProps}: AppProps) {
|
|
89
84
|
In your `src/middleware.ts` file, add the following:
|
90
85
|
|
91
86
|
```typescript
|
92
|
-
import {authMiddleware} from "
|
87
|
+
import {authMiddleware} from "@propelauth/nextjs/server/app-router";
|
93
88
|
|
94
89
|
export const middleware = authMiddleware
|
95
90
|
|
@@ -110,10 +105,10 @@ export const config = {
|
|
110
105
|
### Get the user in Server Components (AppRouter example)
|
111
106
|
|
112
107
|
```tsx
|
113
|
-
import {getUser} from "
|
108
|
+
import {getUser} from "@propelauth/nextjs/server/app-router";
|
114
109
|
|
115
|
-
const WelcomeMessage = () => {
|
116
|
-
const user = getUser()
|
110
|
+
const WelcomeMessage = async () => {
|
111
|
+
const user = await getUser()
|
117
112
|
|
118
113
|
if (user) {
|
119
114
|
return <div>Hello {user.firstName}!</div>
|
@@ -124,11 +119,11 @@ const WelcomeMessage = () => {
|
|
124
119
|
```
|
125
120
|
|
126
121
|
```tsx
|
127
|
-
import {
|
122
|
+
import {getUser} from "@propelauth/nextjs/server/app-router";
|
128
123
|
|
129
|
-
const WelcomeMessage = () => {
|
124
|
+
const WelcomeMessage = async () => {
|
130
125
|
// If the user is not logged in, they will be redirected to the login page
|
131
|
-
const user = getUserOrRedirect()
|
126
|
+
const user = await getUserOrRedirect()
|
132
127
|
|
133
128
|
return <div>Hello {user.firstName}!</div>
|
134
129
|
}
|
@@ -138,7 +133,7 @@ const WelcomeMessage = () => {
|
|
138
133
|
|
139
134
|
```tsx
|
140
135
|
import {GetServerSideProps, InferGetServerSidePropsType} from "next";
|
141
|
-
import {getUserFromServerSideProps} from "
|
136
|
+
import {getUserFromServerSideProps} from "@propelauth/nextjs/server/pages";
|
142
137
|
import {User} from "@propelauth/nextjs/client";
|
143
138
|
|
144
139
|
export default function WelcomeMessage({userJson}: InferGetServerSidePropsType<typeof getServerSideProps>) {
|
@@ -189,7 +184,7 @@ The quick answer is:
|
|
189
184
|
|
190
185
|
```tsx
|
191
186
|
// src/app/org/[slug]/page.tsx
|
192
|
-
import {getUserOrRedirect} from "
|
187
|
+
import {getUserOrRedirect} from "@propelauth/nextjs/server/app-router";
|
193
188
|
|
194
189
|
export default async function AdminOnlyPage({ params }: { params: { slug: string } }) {
|
195
190
|
const user = await getUserOrRedirect()
|
@@ -0,0 +1,75 @@
|
|
1
|
+
import { NextRequest } from 'next/server';
|
2
|
+
|
3
|
+
declare class UnauthorizedException extends Error {
|
4
|
+
readonly message: string;
|
5
|
+
readonly status: number;
|
6
|
+
constructor(message: string);
|
7
|
+
}
|
8
|
+
declare class ConfigurationException extends Error {
|
9
|
+
readonly message: string;
|
10
|
+
readonly status: number;
|
11
|
+
constructor(message: string);
|
12
|
+
}
|
13
|
+
|
14
|
+
declare class User {
|
15
|
+
userId: string;
|
16
|
+
orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo;
|
17
|
+
email: string;
|
18
|
+
firstName?: string;
|
19
|
+
lastName?: string;
|
20
|
+
username?: string;
|
21
|
+
legacyUserId?: string;
|
22
|
+
impersonatorUserId?: string;
|
23
|
+
constructor(userId: string, email: string, orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo, firstName?: string, lastName?: string, username?: string, legacyUserId?: string, impersonatorUserId?: string);
|
24
|
+
getOrg(orgId: string): OrgMemberInfo | undefined;
|
25
|
+
getOrgByName(orgName: string): OrgMemberInfo | undefined;
|
26
|
+
getOrgs(): OrgMemberInfo[];
|
27
|
+
isImpersonating(): boolean;
|
28
|
+
static fromJSON(json: string): User;
|
29
|
+
}
|
30
|
+
type OrgIdToOrgMemberInfo = {
|
31
|
+
[orgId: string]: OrgMemberInfo;
|
32
|
+
};
|
33
|
+
declare class OrgMemberInfo {
|
34
|
+
orgId: string;
|
35
|
+
orgName: string;
|
36
|
+
orgMetadata: {
|
37
|
+
[key: string]: any;
|
38
|
+
};
|
39
|
+
urlSafeOrgName: string;
|
40
|
+
private userAssignedRole;
|
41
|
+
private userInheritedRolesPlusCurrentRole;
|
42
|
+
private userPermissions;
|
43
|
+
constructor(orgId: string, orgName: string, orgMetadata: {
|
44
|
+
[key: string]: any;
|
45
|
+
}, urlSafeOrgName: string, userAssignedRole: string, userInheritedRolesPlusCurrentRole: string[], userPermissions: string[]);
|
46
|
+
isRole(role: string): boolean;
|
47
|
+
isAtLeastRole(role: string): boolean;
|
48
|
+
hasPermission(permission: string): boolean;
|
49
|
+
hasAllPermissions(permissions: string[]): boolean;
|
50
|
+
static fromJSON(json: string): OrgMemberInfo;
|
51
|
+
get assignedRole(): string;
|
52
|
+
get inheritedRolesPlusCurrentRole(): string[];
|
53
|
+
get permissions(): string[];
|
54
|
+
}
|
55
|
+
|
56
|
+
declare function getUserOrRedirect(): Promise<User>;
|
57
|
+
declare function getUser(): Promise<User | undefined>;
|
58
|
+
declare function authMiddleware(req: NextRequest): Promise<Response>;
|
59
|
+
type RouteHandlerArgs = {
|
60
|
+
postLoginRedirectPathFn?: (user: User, req: NextRequest) => string;
|
61
|
+
};
|
62
|
+
declare function getRouteHandlers(args?: RouteHandlerArgs): {
|
63
|
+
getRouteHandler: (req: NextRequest, { params }: {
|
64
|
+
params: {
|
65
|
+
slug: string;
|
66
|
+
};
|
67
|
+
}) => Response | Promise<Response>;
|
68
|
+
postRouteHandler: (req: NextRequest, { params }: {
|
69
|
+
params: {
|
70
|
+
slug: string;
|
71
|
+
};
|
72
|
+
}) => Response | Promise<Response>;
|
73
|
+
};
|
74
|
+
|
75
|
+
export { ConfigurationException, RouteHandlerArgs, UnauthorizedException, authMiddleware, getRouteHandlers, getUser, getUserOrRedirect };
|