'How to hide some dom element if user hasn't jwt token in cookies
As cookies, it is unable to retrieve from client side. What's the solution we can get verify in _middleware and passing isAuth to client side in nextjs?
// pages/_middleware.ts
const JWT_SECRET = process.env.JWT_TOKEN as string;
export async function middleware(req: NextRequest, ev: NextFetchEvent) {
const { cookies } = req;
const token = cookies.appToken;
/*
Passing some value to client side if it is not login yet.
*/
if( !token ) {
}
try {
// verify the token
const { payload: jwtData } = await jose.jwtVerify(
token, new TextEncoder().encode( JWT_SECRET )
)
return NextResponse.next();
} catch (error) {
return NextResponse.next();
// return NextResponse.redirect(new URL('/login', req.url));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
