Skip to content

Commit e278f9f

Browse files
committed
Add more info about the proposal
1 parent a7594c1 commit e278f9f

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

lib/ts/recipe/session/nextjs/README.md

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ SuperTokensConfig.init();
3838

3939
```
4040

41-
### 3. Add the SuperTokensProvider Component
41+
### 3. Add the authentication UI
42+
43+
#### 3.1 Wrap the app with SuperTokensProvider
4244

4345
```tsx
4446
// This should abstract away more of the custom boilerplate
@@ -49,15 +51,26 @@ function Root({ children }) {
4951
}
5052
```
5153

52-
### 4. Add the SuperTokens Middleware
54+
#### 3.2 Add the UI components
55+
56+
This will differ based on custom vs pre-built UI.
5357

54-
We set the middleware as the default way of handling validation.
58+
### 4. Add the SuperTokens API function
5559

56-
### Additional Examples
60+
```ts
61+
import { getNextJSApiHandler } from "supertokens-node/nextjs";
5762

58-
#### How to protect a page?
63+
// Include the cache control logic and other custom options in this function
64+
// Allow users to config the behavior through a factory function
65+
const handler = getNextJSApiHandler();
66+
export default handler;
67+
```
5968

60-
#### Using the `SessionAuth` component
69+
### 4. Protect pages and API routes
70+
71+
#### Frontend
72+
73+
##### Using the `SessionAuth` component
6174

6275
```tsx
6376
import { SessionAuth } from "supertokens-auth-react/recipe/session";
@@ -71,7 +84,7 @@ export default function Home() {
7184
}
7285
```
7386

74-
#### Using the `useSessionContext` hook
87+
##### Using the `useSessionContext` hook
7588

7689
```tsx
7790
import { useSessionContext } from "supertokens-auth-react/recipe/session";
@@ -87,21 +100,57 @@ export default function Home() {
87100
}
88101
```
89102

90-
:::info
91-
This applies to both SSR and CSR components.
92-
:::
103+
##### During SSR
104+
105+
```tsx
106+
import { getSession } from "supertokens-auth-react/nextjs";
107+
import { SessionAuth } from "supertokens-auth-react/recipe/session";
108+
109+
export default function Home() {
110+
const session = getSession();
111+
112+
return (
113+
<SessionAuth>
114+
<div>{session.userId}</div>
115+
</SessionAuth>
116+
);
117+
}
118+
```
119+
120+
#### Backend
121+
122+
##### With session guards
123+
124+
```ts
125+
import { withSession } from "supertokens-node/nextjs";
93126

94-
#### How to protect an API route?
127+
function handler(req: NextApiRequest, res: NextApiResponse, session: STSession) {
128+
// Do something with the session
129+
}
95130

96-
#### Get the user id in an API route
131+
export default withSession(handler, {
132+
onUnauthorisedResponse: (req, res) => {},
133+
onError: (err, req, res) => {},
134+
roles: [],
135+
permissions: [],
136+
});
137+
```
97138

98-
- with session
99-
- middleware matching
100-
- Decorators?
139+
##### With a middleware
101140

102-
## SSR Session Validation
141+
```ts
142+
import { getMiddleware } from "supertokens-node/nextjs";
143+
144+
export const middleware = getMiddleware({
145+
onUnauthorisedResponse: (req, res) => {},
146+
onError: (err, req, res) => {},
147+
});
148+
149+
export const config = {
150+
matcher: "/api/:path*",
151+
};
152+
```
103153

104154
## Open Questions
105155

106-
- Can we move everything to the `react` library?
107156
- Figure out how to deal with differences between app router and page router

lib/ts/recipe/session/nextjs/getSessionOrRedirect.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import SuperTokens from "../../../superTokens";
77
import type { AccessTokenPayload, LoadedSessionContext } from "../types";
88

99
// const ACCESS_TOKEN_NAME = "st-access-token";
10-
const ACCESS_TOKEN_NAME = "sAccessToken";
10+
const COOKIE_ACCESS_TOKEN_NAME = "sAccessToken";
11+
const HEADER_ACCESS_TOKEN_NAME = "st-access-token";
1112
const FRONT_TOKEN_NAME = "sFrontToken";
1213

1314
// TODO:
1415
// - Add error handling
15-
// - Figure out the correct token name. Is it sAccessToken or st-access-token?
1616
export async function getSessionOrRedirect(): Promise<LoadedSessionContext> {
17-
const headersList = await headers();
17+
// const headersList = await headers();
1818
const cookieStore = await cookies();
1919
const frontToken = cookieStore.get(FRONT_TOKEN_NAME)?.value;
2020
const authPagePage = getWebsiteBasePath();
@@ -28,7 +28,9 @@ export async function getSessionOrRedirect(): Promise<LoadedSessionContext> {
2828
redirect(refreshTokenPath);
2929
}
3030

31-
const accessToken = cookieStore.get(ACCESS_TOKEN_NAME)?.value || headersList.get(ACCESS_TOKEN_NAME);
31+
// Apparently both end up in cookies.
32+
const accessToken =
33+
cookieStore.get(COOKIE_ACCESS_TOKEN_NAME)?.value || cookieStore.get(HEADER_ACCESS_TOKEN_NAME)?.value;
3234
if (!accessToken) {
3335
// TODO: Should redirect to auth page?
3436
redirect(refreshTokenPath);

0 commit comments

Comments
 (0)