@@ -14,57 +14,38 @@ import {
1414 FRONT_TOKEN_COOKIE_NAME ,
1515 ANTI_CSRF_TOKEN_COOKIE_NAME ,
1616 CURRENT_PATH_COOKIE_NAME ,
17+ SESSION_REFRESH_API_PATH ,
1718} from "./constants" ;
1819
19- import type { SuperTokensNextjsConfig , WithSession } from "./types" ;
20+ import type { ApiRequestMiddleware , SuperTokensNextjsConfig } from "./types" ;
2021
2122let AppInfo : SuperTokensNextjsConfig [ "appInfo" ] ;
2223const DEFAULT_API_BASE_PATH = "/api/auth" ;
2324
24- // Open questions:
25- // - Do we need the x-user-id header?
26- // - Do we want to enforce API route validation from the middleware
27- // or do we explain how to do this at the API function level
28- // - How do we pass withSession and ValidateSessionOptions together
29- // - If we use withSession in the middleware, do we add some ability to choose which routes will be protected?
25+ // TODO: Test this inside a server action / form action
3026export function superTokensMiddleware (
3127 config : SuperTokensNextjsConfig ,
32- withSession : WithSession
28+ apiRequestMiddleware ?: ApiRequestMiddleware
3329) : ( request : Request ) => Promise < Response | void > {
30+ // TODO: Fix edge cases
3431 const usesTheNextjsApiAsTheAuthenticationServer = config . appInfo . apiDomain === config . appInfo . websiteDomain ;
3532
3633 return async ( request : Request ) => {
3734 const requestUrl = new URL ( request . url ) ;
38- if ( requestUrl . pathname . startsWith ( "/api/auth/session/refresh" ) && request . method === "GET" ) {
35+ // TODO: Use a constant here
36+ if ( requestUrl . pathname . startsWith ( SESSION_REFRESH_API_PATH ) && request . method === "GET" ) {
3937 return refreshSession ( config , request ) ;
4038 }
4139
4240 if ( requestUrl . pathname . startsWith ( "/api" ) && usesTheNextjsApiAsTheAuthenticationServer ) {
43- if ( request . headers . has ( "x-user-id" ) ) {
44- console . warn (
45- "The FE tried to pass x-user-id, which is only supposed to be a backend internal header. Ignoring."
46- ) ;
47- request . headers . delete ( "x-user-id" ) ;
48- }
49-
5041 if ( requestUrl . pathname . startsWith ( config . appInfo . apiBasePath || DEFAULT_API_BASE_PATH ) ) {
5142 // this hits our pages/api/auth/* endpoints
5243 return next ( ) ;
5344 }
5445
55- return withSession ( request , async ( err , session ) => {
56- if ( err ) {
57- return new Response ( JSON . stringify ( err ) , {
58- status : 500 ,
59- headers : { "Content-Type" : "application/json" } ,
60- } ) ;
61- }
62- const response = next ( ) ;
63- if ( session !== undefined ) {
64- response . headers . append ( "x-user-id" , session . getUserId ( ) ) ;
65- }
66- return response ;
67- } ) ;
46+ if ( apiRequestMiddleware ) {
47+ return apiRequestMiddleware ( request ) ;
48+ }
6849 }
6950
7051 if (
@@ -105,6 +86,7 @@ export async function refreshSession(config: SuperTokensNextjsConfig, request: R
10586 }
10687
10788 const requestUrl = new URL ( request . url ) ;
89+ // TODO: Validate the redirect path
10890 const redirectTo = requestUrl . searchParams . get ( REDIRECT_PATH_PARAM_NAME ) || "/" ;
10991 try {
11092 const tokens = await fetchNewTokens ( request ) ;
0 commit comments