Skip to content

Commit 70d117b

Browse files
committed
Reapply "refactor: migrate auth and utility api routes to app router (#1408)" (#1409)
This reverts commit a054efe.
1 parent 2d745a6 commit 70d117b

8 files changed

Lines changed: 94 additions & 115 deletions

File tree

src/app/api/auth/[...nextauth]/authOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import axios from 'axios'
22
import type { NextAuthOptions } from 'next-auth'
33
import Auth0Provider from 'next-auth/providers/auth0'
44

5-
import { AUTH_CONFIG_SERVER } from '../../../../Config'
6-
import { IUserMetadata, UserRole } from '../../../../js/types/User'
5+
import { AUTH_CONFIG_SERVER } from '@/Config'
6+
import { IUserMetadata, UserRole } from '@/js/types/User'
77
import { initializeUserInDB } from '@/js/auth/initializeUserInDb'
88

99
const CustomClaimsNS = 'https://tacos.openbeta.io/'
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
* However, it does not actually log out of auth0. Therefore, after logging out and then in the user will automatically be logged in again.
44
* The default user experience here could be ok for providers such as facebook/google/etc but we want users to be able to log in with another account.
55
*/
6-
import { NextApiHandler } from 'next'
6+
import { NextRequest, NextResponse } from 'next/server'
77

88
const auth0Domain = process.env.AUTH0_DOMAIN ?? ''
99
const auth0ClientId = process.env.AUTH0_CLIENT_ID ?? ''
1010

11-
const handler: NextApiHandler = (req, res): void => {
11+
export async function GET (req: NextRequest): Promise<NextResponse> {
1212
const clientIdParam = `client_id=${auth0ClientId}`
13-
if (req.headers.referer == null) {
14-
res.redirect(`${auth0Domain}/v2/logout?${clientIdParam}`)
13+
const referer = req.headers.get('referer')
14+
15+
if (referer == null) {
16+
return NextResponse.redirect(`${auth0Domain}/v2/logout?${clientIdParam}`)
1517
} else {
16-
const returnTo = new URL(req.headers.referer).origin
17-
res.redirect(`${auth0Domain}/v2/logout?returnTo=${encodeURIComponent(returnTo)}&${clientIdParam}`)
18+
const returnTo = new URL(referer).origin
19+
return NextResponse.redirect(`${auth0Domain}/v2/logout?returnTo=${encodeURIComponent(returnTo)}&${clientIdParam}`)
1820
}
1921
}
20-
21-
export default handler

src/app/api/revalidate/route.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { revalidatePath } from 'next/cache'
3+
import { withUserAuth } from '@/js/auth/withUserAuth'
4+
import { checkUsername } from '@/js/utils'
5+
6+
/**
7+
* Notify backend to regenerate a page.
8+
* @see https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration
9+
*/
10+
const handler = async (req: NextRequest): Promise<NextResponse> => {
11+
try {
12+
const username = req.nextUrl.searchParams.get('u')
13+
const page = req.nextUrl.searchParams.get('page')
14+
15+
let revalidated = false
16+
17+
// Handle user profile revalidation
18+
if (username != null && checkUsername(username)) {
19+
revalidatePath(`/u/${encodeURIComponent(username)}`)
20+
revalidated = true
21+
}
22+
23+
// Handle other pages revalidation
24+
const ALLOWS = ['/edit']
25+
if (page != null && ALLOWS.includes(page)) {
26+
revalidatePath(page)
27+
revalidated = true
28+
}
29+
30+
return NextResponse.json({ revalidated })
31+
} catch (e) {
32+
return NextResponse.json(
33+
{ error: 'Error revalidating page' },
34+
{ status: 500 }
35+
)
36+
}
37+
}
38+
39+
export const GET = withUserAuth(handler)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import * as jose from 'jose'
3+
import { AUTH_CONFIG_SERVER } from '@/Config'
4+
import { sendEmailVerification } from '@/js/auth/ManagementClient'
5+
6+
if (AUTH_CONFIG_SERVER == null) throw new Error('AUTH_CONFIG_SERVER not defined')
7+
8+
const { nextauthSecret } = AUTH_CONFIG_SERVER
9+
10+
/**
11+
* JWT-verify 'session_token' from Auth0 postLogin action. The userId to request new email verification
12+
* is in the JWT payload. We want to make sure the token really comes from Auth0.
13+
* - GET: verify only
14+
* - POST: verify and send a verification email for the user.
15+
* @param endpoint /api/user/emailVerification?token=<Auth0 session_token>
16+
*/
17+
const verify = async (req: NextRequest): Promise<any> => {
18+
const jwt = req.nextUrl.searchParams.get('token')
19+
if (jwt != null) {
20+
return await jose.jwtVerify(jwt, Buffer.from(nextauthSecret))
21+
}
22+
}
23+
24+
export async function GET (req: NextRequest): Promise<NextResponse> {
25+
try {
26+
await verify(req)
27+
return new NextResponse(null, { status: 200 })
28+
} catch {
29+
return new NextResponse(null, { status: 503 })
30+
}
31+
}
32+
33+
export async function POST (req: NextRequest): Promise<NextResponse> {
34+
try {
35+
const token = await verify(req)
36+
const auth0UserId = token.payload.sub
37+
await sendEmailVerification(auth0UserId)
38+
return new NextResponse(null, { status: 200 })
39+
} catch {
40+
return new NextResponse(null, { status: 503 })
41+
}
42+
}

src/pages/api/legacyInvalidateClimbCache.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/pages/api/revalidate.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/pages/api/user/emailVerification.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
],
4242
"@/public/*": [
4343
"../public/*"
44+
],
45+
"@/*": [
46+
"*"
4447
]
4548
},
4649
"plugins": [

0 commit comments

Comments
 (0)