Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[edge-functions/jwt-authentication] Fix for breaking changes in Next.js canary #443

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions edge-functions/jwt-authentication/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NextRequest, NextResponse } from 'next/server'
import { nanoid } from 'nanoid'
import { SignJWT, jwtVerify } from 'jose'
import { USER_TOKEN, JWT_SECRET_KEY } from './constants'
import { USER_TOKEN, getJwtSecretKey } from './constants'

interface UserJwtPayload {
jti: string
Expand All @@ -14,14 +14,14 @@ export class AuthError extends Error {}
* Verifies the user's JWT token and returns its payload if it's valid.
*/
export async function verifyAuth(req: NextRequest) {
const token = req.cookies.get(USER_TOKEN)
const token = req.cookies.get(USER_TOKEN)?.value

if (!token) throw new AuthError('Missing user token')

try {
const verified = await jwtVerify(
token,
new TextEncoder().encode(JWT_SECRET_KEY)
new TextEncoder().encode(getJwtSecretKey())
)
return verified.payload as UserJwtPayload
} catch (err) {
Expand All @@ -38,7 +38,7 @@ export async function setUserCookie(res: NextResponse) {
.setJti(nanoid())
.setIssuedAt()
.setExpirationTime('2h')
.sign(new TextEncoder().encode(JWT_SECRET_KEY))
.sign(new TextEncoder().encode(getJwtSecretKey()))

res.cookies.set(USER_TOKEN, token, {
httpOnly: true,
Expand Down
10 changes: 9 additions & 1 deletion edge-functions/jwt-authentication/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export const USER_TOKEN = 'user-token'

export const JWT_SECRET_KEY = process.env.JWT_SECRET_KEY!
const JWT_SECRET_KEY: string | undefined = process.env.JWT_SECRET_KEY!

export function getJwtSecretKey(): string {
if (!JWT_SECRET_KEY || JWT_SECRET_KEY.length === 0) {
throw new Error('The environment variable JWT_SECRET_KEY is not set.')
}

return JWT_SECRET_KEY
}
2 changes: 1 addition & 1 deletion edge-functions/jwt-authentication/pages/protected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Page, Text, Code, Link, Button } from '@vercel/examples-ui'
import { USER_TOKEN } from '@lib/constants'

export default function Protected() {
const { reload } = useRouter()
const { reload } = useRouter(true)
lfades marked this conversation as resolved.
Show resolved Hide resolved

return (
<Page>
Expand Down