Code RoomJWT verification code comment
HardPrep Room Coding #1873

JWT verification code comment

Code reviewCode quality & reviewSenior–Staff~30 min

Review this Node.js token verifier.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 30 min
Mark a line and say what kind of problem it is.0 findings
1const jwt = require('jsonwebtoken');
2const fs = require('fs');
3 
4const PUBLIC_KEY = fs.readFileSync('./rsa_public.pem');
5 
6function verifyToken(req, res, next) {
7 const token = req.headers.authorization?.split(' ')[1];
8 try {
9 // Public key is safe to ship to clients, so verifying with it is fine
10 const payload = jwt.verify(token, PUBLIC_KEY);
11 req.user = payload;
12 next();
13 } catch (e) {
14 res.status(401).json({ error: 'invalid token' });
15 }
16}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.