-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: convert next example to use app dir
- Loading branch information
Showing
14 changed files
with
245 additions
and
1,243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NextResponse, NextRequest } from "next/server"; | ||
|
||
export async function GET(request: NextRequest) { | ||
let userId = request.headers.get("x-user-id"); | ||
return NextResponse.json({ | ||
userId, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { NextResponse, NextRequest } from "next/server"; | ||
import { withSession } from "../../../middleware"; | ||
|
||
export async function GET(request: NextRequest) { | ||
return withSession(request, async (session) => { | ||
if (session === undefined) { | ||
return new NextResponse("Authentication required", { | ||
status: 401, | ||
}); | ||
} | ||
return NextResponse.json({ | ||
note: "Fetch any data from your application for authenticated user after using verifySession middleware", | ||
userId: session.getUserId(), | ||
sessionHandle: session.getHandle(), | ||
accessTokenPayload: session.getAccessTokenPayload(), | ||
}); | ||
}); | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/next/with-emailpassword/app/auth/[[...path]]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use client"; | ||
import { useEffect } from "react"; | ||
import { redirectToAuth } from "supertokens-auth-react"; | ||
import SuperTokens from "supertokens-auth-react/ui"; | ||
import { PreBuiltUIList } from "../../../config/frontendConfig"; | ||
|
||
export default function Auth() { | ||
// if the user visits a page that is not handled by us (like /auth/random), then we redirect them back to the auth page. | ||
useEffect(() => { | ||
if (SuperTokens.canHandleRoute(PreBuiltUIList) === false) { | ||
redirectToAuth(); | ||
} | ||
}, []); | ||
|
||
if (typeof window !== "undefined") { | ||
return SuperTokens.getRoutingComponent(PreBuiltUIList); | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use client"; | ||
import SuperTokensReact, { SuperTokensWrapper } from "supertokens-auth-react"; | ||
import { frontendConfig } from "../config/frontendConfig"; | ||
|
||
// export const metadata = { | ||
// title: 'Next.js', | ||
// description: 'Generated by Next.js', | ||
// } | ||
|
||
if (typeof window !== "undefined") { | ||
// we only want to call this init function on the frontend, so we check typeof window !== 'undefined' | ||
SuperTokensReact.init(frontendConfig()); | ||
} | ||
|
||
export default function RootLayout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html lang="en"> | ||
<SuperTokensWrapper> | ||
<body suppressHydrationWarning={true}>{children}</body> | ||
</SuperTokensWrapper> | ||
</html> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { NextResponse } from "next/server"; | ||
import type { NextRequest } from "next/server"; | ||
import Session, { SessionContainer } from "supertokens-node/recipe/session"; | ||
import supertokens from "supertokens-node"; | ||
import { backendConfig } from "./config/backendConfig"; | ||
|
||
supertokens.init(backendConfig()); | ||
|
||
export async function middleware(request: NextRequest & { session?: SessionContainer }) { | ||
if (request.nextUrl.pathname.startsWith("/api/auth")) { | ||
// this hits our pages/api/auth/* endpoints | ||
return NextResponse.next(); | ||
} | ||
|
||
return withSession(request, async (session) => { | ||
if (session === undefined) { | ||
return NextResponse.next(); | ||
} | ||
return NextResponse.next({ | ||
headers: { | ||
"x-user-id": session.getUserId(), | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
export const config = { | ||
matcher: "/api/:path*", | ||
}; | ||
|
||
export async function withSession( | ||
request: NextRequest, | ||
handler: (session: SessionContainer | undefined) => Promise<NextResponse> | ||
) { | ||
try { | ||
const token = request.cookies.get("sAccessToken"); | ||
if (token === undefined) { | ||
return handler(undefined); | ||
} | ||
const accessToken = token.value; | ||
let session = await Session.getSessionWithoutRequestResponse(accessToken, undefined, { | ||
sessionRequired: false, | ||
}); | ||
let response = await handler(session); | ||
if (session !== undefined) { | ||
let tokens = session.getAllSessionTokensDangerously(); | ||
if (tokens.accessAndFrontTokenUpdated) { | ||
response.cookies.set({ | ||
name: "sAccessToken", | ||
value: tokens.accessToken, | ||
httpOnly: true, | ||
path: "/", | ||
expires: Date.now() + 3153600000000, | ||
}); | ||
response.headers.append("front-token", tokens.frontToken); | ||
} | ||
} | ||
return response; | ||
} catch (err) { | ||
if (Session.Error.isErrorFromSuperTokens(err)) { | ||
return new Response("Authentication required", { | ||
status: err.type === Session.Error.INVALID_CLAIMS ? 403 : 401, | ||
}); | ||
} | ||
throw err; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
reactStrictMode: true, | ||
experimental: { | ||
appDir: true, | ||
}, | ||
}; | ||
|
||
module.exports = nextConfig; |
Oops, something went wrong.