async validateToken(token: string) { try { const payload = this.jwtService.verify(token); return { valid: true, payload }; } catch (error) { return { valid: false, error: error.message }; } }
The JwtService.verify method throws an error with message 'jwt expired' when the token is expired. The catch block catches this and returns an object with valid false and the error message.
const payload = { username: 'user1', sub: 123 };The correct option key is expiresIn with a string like '1h' or a number in seconds. Other keys like 'expiration' or 'expireIn' are invalid.
async validateToken(token: string) { const payload = this.jwtService.verify(token, { secret: 'wrongSecret' }); return payload; }
The verify method throws a JsonWebTokenError if the secret used does not match the one used to sign the token. This causes the error.
The 'sub' claim stands for 'subject' and identifies the principal that is the subject of the JWT, usually the user ID.
let isValid = false; try { const payload = this.jwtService.verify(token); if (payload.sub) { isValid = true; } } catch { isValid = false; }
The code sets isValid to true only if payload.sub exists. If the token is valid but payload.sub is missing, isValid remains false.
