@propelauth/nextjs 0.0.67 → 0.0.68

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/README.md +32 -37
  2. package/package.json +1 -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
- Create a file called `propelauth.ts` in the `src` directory (or your project's root if you prefer) with the following content:
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
- export const {
30
- getRouteHandler,
31
- postRouteHandler,
32
- getUser,
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
- This file exports all of our server-side functions that you will need to use in your application.
42
- You can find all the env variables for your application in the PropelAuth Dashboard under **Backend Integration**.
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 {getRouteHandler, postRouteHandler} from "@/auth";
50
-
51
- export const GET = getRouteHandler
52
- export const POST = postRouteHandler
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 "@/auth";
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 "@/auth";
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 {getUserOrRedirect} from "@/auth";
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 "@/auth";
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 "@/auth";
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()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@propelauth/nextjs",
3
- "version": "0.0.67",
3
+ "version": "0.0.68",
4
4
  "exports": {
5
5
  "./server": {
6
6
  "browser": "./dist/server/index.mjs",