Complete the code to create a JWT token using the jsonwebtoken library.
const jwt = require('jsonwebtoken'); const token = jwt.[1]({ userId: 123 }, 'secretKey');
The sign method creates a new JWT token by encoding the payload with a secret key.
Complete the code to set a session cookie in Next.js API route.
import { withIronSessionApiRoute } from 'iron-session/next'; export default withIronSessionApiRoute(async function handler(req, res) { req.session.user = { id: 1 }; await req.session.[1](); res.send('Logged in'); }, { password: 'complex_password', cookieName: 'myapp_session', cookieOptions: { secure: process.env.NODE_ENV === 'production' } });
The save method commits the session changes and sets the cookie in the response.
Fix the error in the code that verifies a JWT token.
import jwt from 'jsonwebtoken'; try { const decoded = jwt.[1](token, 'secretKey'); console.log(decoded); } catch (err) { console.error('Invalid token'); }
The verify method checks the token's validity and decodes it if valid.
Fill both blanks to create a Next.js API route that clears a session cookie.
import { withIronSessionApiRoute } from 'iron-session/next'; export default withIronSessionApiRoute(async function handler(req, res) { req.session.[1] = null; await req.session.[2](); res.send('Logged out'); }, { password: 'complex_password', cookieName: 'myapp_session', cookieOptions: { secure: process.env.NODE_ENV === 'production' } });
Setting req.session.user to null clears the user data. Then save commits the change.
Fill all three blanks to create a JWT token with expiration and verify it.
import jwt from 'jsonwebtoken'; const token = jwt.[1]({ id: 42 }, 'secretKey', { expiresIn: [2] }); const decoded = jwt.[3](token, 'secretKey');
Use sign to create the token with expiration '1h'. Then verify checks and decodes the token.
