Skip to content

NextJS 12 middleware veify JWT token prolem #38227

Answered by icyJoseph
philiposD asked this question in Help
Discussion options

You must be logged in to vote

Hi,

jsonwebtoken cannot run on the Edge environment. You have to use a library that does. I am using jose, https://www.npmjs.com/package/jose, here's a small snippet that can get you started quickly:

import {SignJWT, jwtVerify, type JWTPayload} from 'jose';

export async function sign(payload: Token, secret: string): Promise<string> {
    const iat = Math.floor(Date.now() / 1000);
    const exp = iat + 60* 60; // one hour

    return new SignJWT({...payload})
        .setProtectedHeader({alg: 'HS256', typ: 'JWT'})
        .setExpirationTime(exp)
        .setIssuedAt(iat)
        .setNotBefore(iat)
        .sign(new TextEncoder().encode(secret));
}

export async function verify(token: string,

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
7 replies
@philiposD
Comment options

@Bacale-Kevin
Comment options

@h0lme3
Comment options

@enes-erenn
Comment options

@sknightq
Comment options

Answer selected by philiposD
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
8 participants