You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -76,6 +76,143 @@ This will handle Kinde Auth endpoints in your Next.js app.
76
76
77
77
**Important!** Our SDK relies on this file existing in this location specified above.
78
78
79
+
## **Customising Kinde Auth API paths**
80
+
81
+
The default path for the Kinde Auth API is `/api/auth`. If your Next.js application uses a custom base path for your API, you can override this setting by setting the following variable in your `.env` file:
82
+
83
+
```bash
84
+
KINDE_AUTH_API_PATH="/my/custom/path"
85
+
```
86
+
87
+
You can also customise the Kinde Auth API sub-paths by setting the following variables in your `.env` file:
88
+
89
+
-`KINDE_AUTH_LOGIN_ROUTE` - defaults to `login`
90
+
-`KINDE_AUTH_LOGOUT_ROUTE` - defaults to `logout`
91
+
-`KINDE_AUTH_REGISTER_ROUTE` - defaults to `register`
92
+
-`KINDE_AUTH_CREATEORG_ROUTE` - defaults to `create_org`
93
+
-`KINDE_AUTH_HEALTH_ROUTE` - defaults to `health`
94
+
-`KINDE_AUTH_SETUP_ROUTE` - defaults to `setup`
95
+
96
+
#### **Example**
97
+
98
+
Given the following `.env` file:
99
+
100
+
```bash
101
+
KINDE_AUTH_API_PATH="/my/custom/path"
102
+
KINDE_AUTH_LOGIN_ROUTE="app_login"
103
+
```
104
+
105
+
The Kinde login route for your application will be `/my/custom/path/app_login`.
106
+
107
+
## **Set up middleware**
108
+
109
+
Middleware is used to protect routes in your Next.js app, and is a requirement for a seamless authentication experience.
110
+
111
+
We provide a `withAuth` helper that will protect routes covered by the matcher. If the user is not authenticated then they are redirected to login and once they have logged in they will be redirected back to the protected page which they should now have access to.
112
+
113
+
We require this middleware to run on all routes beside Next.js internals and static files. The provided matcher will do this for you.
114
+
115
+
This means that by default, all routes will be protected. You must opt-out public routes - see [opting routes out of middleware protection](#opting-routes-out-of-middleware-protection) for more information.
116
+
117
+
<Aside>
118
+
119
+
Want to learn more about middleware? Check out the [Next.js middleware docs](https://nextjs.org/docs/app/building-your-application/routing/middleware).
120
+
121
+
</Aside>
122
+
123
+
#### **Middleware configuration**
124
+
125
+
Create a `middleware.ts` file in your project's root directory and add the following code:
#### **Route protection with callback function after authorization**
142
+
143
+
You can use the `withAuth` helper as shown below with a `middleware` callback function which has access to the `req.kindeAuth` object that exposes the token and user data.
Wrap your app in the Kinde Auth Provider. This will give you access to the Kinde Auth data in your app and will ensure that the tokens are refreshed when needed.
@@ -1340,87 +1477,29 @@ if (!(await isAuthenticated())) {
1340
1477
}
1341
1478
```
1342
1479
1343
-
### Protect routes using middleware
1344
-
1345
-
You can also protect routes with Next.js middleware.
1346
-
1347
-
<Aside>
1348
-
1349
-
As of right now the middleware in the app router does not work when trying to redirect to `api/auth/login`. This is because of Next.js caching which causes issues during authentication.
1350
-
1351
-
</Aside>
1352
-
1353
-
**Default page protection**
1354
-
1355
-
We provide a `withAuth` helper that will protect routes covered by the matcher. If the user is not authenticated then they are redirected to login and once they have logged in they will be redirected back to the protected page which they should now have access to.
**Page protection with callback function after authorization**
1368
1480
1369
-
You can use the `withAuth` helper as shown below with a `middleware` callback function which has access to the `req.kindeAuth` object that exposes the token and user data.
Our middleware will automatically refresh the tokens in your session in the background.
1382
1485
1383
-
**Middleware options**
1486
+
Sometimes, you may want to refresh these tokens yourself. An example of this is when you update Kinde data via the UI or with the Management API.
1384
1487
1385
-
There are options that can be passed into the middleware function to configure its functionality.
1488
+
To have these updates immediately reflected in your app, you will need to get the most up-to-date Kinde data and then refresh the tokens in your session.
1386
1489
1387
-
-`isReturnToCurrentPage` - redirect the user back to the page they were trying to access
1388
-
-`loginPage` - define the path of the login page (where the users are redirected to when not authenticated)
1389
-
-`publicPaths` - define the public paths
1390
-
-`isAuthorized` - define the criteria for authorization
1490
+
To get the most up-to-date Kinde data in your session, use the `refreshTokens` helper function provided by `getKindeServerSession`.
// The user will be considered authorized if they have the permission 'eat:chips'
1404
-
returntoken.permissions.includes("eat:chips");
1405
-
}
1406
-
}
1407
-
);
1492
+
<Asidetitle="Important">
1408
1493
1409
-
exportconst config = {
1410
-
matcher: ["/admin"]
1411
-
};
1412
-
```
1413
-
1414
-
## Refreshing Kinde data
1494
+
Due to limitations in Next.js, this will only work in a route handler or server action.
1415
1495
1416
-
Kinde data can be updated via the UI or with the Management API. To have these updates be reflected in your app, you will need to get the most up-to-date Kinde data and then refresh the tokens in your session.
1417
-
1418
-
To get the most up-to-date Kinde data in your session, use the `refreshTokens` helper function.
0 commit comments