Skip to content

Commit a054efe

Browse files
authored
Revert "refactor: migrate auth and utility api routes to app router (#1408)" (#1409)
This reverts commit e956d62.
1 parent e956d62 commit a054efe

File tree

8 files changed

+115
-94
lines changed

8 files changed

+115
-94
lines changed

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/'

src/app/api/revalidate/route.ts

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

src/app/api/user/emailVerification/route.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.
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 { NextRequest, NextResponse } from 'next/server'
6+
import { NextApiHandler } from 'next'
77

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

11-
export async function GET (req: NextRequest): Promise<NextResponse> {
11+
const handler: NextApiHandler = (req, res): void => {
1212
const clientIdParam = `client_id=${auth0ClientId}`
13-
const referer = req.headers.get('referer')
14-
15-
if (referer == null) {
16-
return NextResponse.redirect(`${auth0Domain}/v2/logout?${clientIdParam}`)
13+
if (req.headers.referer == null) {
14+
res.redirect(`${auth0Domain}/v2/logout?${clientIdParam}`)
1715
} else {
18-
const returnTo = new URL(referer).origin
19-
return NextResponse.redirect(`${auth0Domain}/v2/logout?returnTo=${encodeURIComponent(returnTo)}&${clientIdParam}`)
16+
const returnTo = new URL(req.headers.referer).origin
17+
res.redirect(`${auth0Domain}/v2/logout?returnTo=${encodeURIComponent(returnTo)}&${clientIdParam}`)
2018
}
2119
}
20+
21+
export default handler
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { NextApiRequest, NextApiHandler } from 'next'
2+
import { validate as isValid } from 'uuid'
3+
4+
/**
5+
* Invalidate legacy climb page (/climbs/<uuid>)
6+
* @see https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration
7+
* @deprecated
8+
*/
9+
const handler: NextApiHandler = async (req: NextApiRequest, res) => {
10+
if (!res.writable) return
11+
const climbUuid = req.query?.c as string
12+
if (isValid(climbUuid)) {
13+
await res.revalidate(`/climb/${climbUuid}`)
14+
res.json({ revalidated: true })
15+
}
16+
}
17+
18+
export default handler

src/pages/api/revalidate.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { NextApiRequest, NextApiResponse, NextApiHandler } from 'next'
2+
import withAuth from './withAuth'
3+
import { checkUsername } from '../../js/utils'
4+
5+
/**
6+
* Notify backend to regenerate a page.
7+
* @see https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration
8+
*/
9+
const handler: NextApiHandler = async (req: NextApiRequest, res) => {
10+
try {
11+
await profileHandler(req, res)
12+
await otherPagesHandler(req, res)
13+
res.end()
14+
} catch (e) {
15+
return res.status(500).send('Error revalidating page')
16+
}
17+
}
18+
19+
/**
20+
* Send a fetch('/api/revalidate?u=<username>') to regenerate the user page
21+
*/
22+
const profileHandler = async (req: NextApiRequest, res: NextApiResponse): Promise<any> => {
23+
if (!res.writable) return
24+
const username = req.query?.u as string
25+
if (checkUsername(username)) {
26+
await res.revalidate(`/u/${encodeURIComponent(username)}`)
27+
res.json({ revalidated: true })
28+
}
29+
}
30+
31+
/**
32+
* Send a fetch('/api/revalidate?page=edit') to regenerate the edit history.
33+
* Need to whitelist the page in `ALLOWS` array.
34+
*/
35+
const otherPagesHandler = async (req: NextApiRequest, res: NextApiResponse): Promise<any> => {
36+
if (!res.writable) return
37+
const page = req.query?.page as string
38+
const ALLOWS = ['/edit']
39+
if (ALLOWS.includes(page)) {
40+
await res.status(200).revalidate(page)
41+
res.json({ revalidated: true })
42+
}
43+
}
44+
45+
export default withAuth(handler)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { NextApiHandler, NextApiRequest } from 'next'
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 handler: NextApiHandler = async (req, res) => {
18+
switch (req?.method) {
19+
case 'GET': {
20+
await verify(req)
21+
res.status(200).end()
22+
break
23+
}
24+
case 'POST': {
25+
const token = await verify(req)
26+
const auth0UserId = token.payload.sub
27+
await sendEmailVerification(auth0UserId)
28+
res.status(200).end()
29+
break
30+
}
31+
default: res.status(503).end()
32+
}
33+
}
34+
35+
export default handler
36+
37+
const verify = async (req: NextApiRequest): Promise<any> => {
38+
const jwt = req.query?.token as string ?? null
39+
if (jwt != null) {
40+
return await jose.jwtVerify(jwt, Buffer.from(nextauthSecret))
41+
}
42+
}

tsconfig.json

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

0 commit comments

Comments
 (0)