@@ -38,7 +38,9 @@ SuperTokensConfig.init();
38
38
39
39
```
40
40
41
- ### 3. Add the SuperTokensProvider Component
41
+ ### 3. Add the authentication UI
42
+
43
+ #### 3.1 Wrap the app with SuperTokensProvider
42
44
43
45
``` tsx
44
46
// This should abstract away more of the custom boilerplate
@@ -49,15 +51,26 @@ function Root({ children }) {
49
51
}
50
52
```
51
53
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.
53
57
54
- We set the middleware as the default way of handling validation.
58
+ ### 4. Add the SuperTokens API function
55
59
56
- ### Additional Examples
60
+ ``` ts
61
+ import { getNextJSApiHandler } from " supertokens-node/nextjs" ;
57
62
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
+ ```
59
68
60
- #### Using the ` SessionAuth ` component
69
+ ### 4. Protect pages and API routes
70
+
71
+ #### Frontend
72
+
73
+ ##### Using the ` SessionAuth ` component
61
74
62
75
``` tsx
63
76
import { SessionAuth } from " supertokens-auth-react/recipe/session" ;
@@ -71,7 +84,7 @@ export default function Home() {
71
84
}
72
85
```
73
86
74
- #### Using the ` useSessionContext ` hook
87
+ ##### Using the ` useSessionContext ` hook
75
88
76
89
``` tsx
77
90
import { useSessionContext } from " supertokens-auth-react/recipe/session" ;
@@ -87,21 +100,57 @@ export default function Home() {
87
100
}
88
101
```
89
102
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" ;
93
126
94
- #### How to protect an API route?
127
+ function handler(req : NextApiRequest , res : NextApiResponse , session : STSession ) {
128
+ // Do something with the session
129
+ }
95
130
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
+ ```
97
138
98
- - with session
99
- - middleware matching
100
- - Decorators?
139
+ ##### With a middleware
101
140
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
+ ```
103
153
104
154
## Open Questions
105
155
106
- - Can we move everything to the ` react ` library?
107
156
- Figure out how to deal with differences between app router and page router
0 commit comments