Complete the code to import the Express library.
const express = require([1]);The Express library is imported using require with the string "express".
Complete the code to create a new Express application instance.
const app = [1]();To create an Express app, call the express function.
Fix the error in the middleware that reads the refresh token from cookies.
app.use((req, res, next) => {
const refreshToken = req.cookies[1];
if (!refreshToken) return res.sendStatus(401);
next();
});To access a cookie named 'refreshToken', use bracket notation with the string key: req.cookies['refreshToken'].
Fill both blanks to verify the refresh token and generate a new access token.
jwt.verify(refreshToken, [1], (err, user) => { if (err) return res.sendStatus(403); const accessToken = jwt.sign({ id: user.id }, [2], { expiresIn: '15m' }); res.json({ accessToken }); });
The refresh token is verified with the refresh token secret, and the new access token is signed with the access token secret.
Fill all three blanks to create a route that issues a refresh token and sets it as an HTTP-only cookie.
app.post('/refresh', (req, res) => { const user = req.body; const refreshToken = jwt.sign(user, [1], { expiresIn: '7d' }); res.cookie('refreshToken', refreshToken, { httpOnly: [2], secure: [3] }); res.json({ refreshToken }); });
The refresh token is signed with the refresh token secret. The cookie is set with httpOnly and secure flags as true for security.