@@ -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
6376import { 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
7790import { 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
0 commit comments