0
0
NestJSframework~20 mins

Token generation and validation in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Token Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS JWT token validation snippet?
Consider a NestJS service method that validates a JWT token using the JwtService. What will be the output if the token is expired?
NestJS
async validateToken(token: string) {
  try {
    const payload = this.jwtService.verify(token);
    return { valid: true, payload };
  } catch (error) {
    return { valid: false, error: error.message };
  }
}
AThrows an unhandled exception
B{ valid: true, payload: <decoded_payload> }
C{ valid: false, error: 'invalid token' }
D{ valid: false, error: 'jwt expired' }
Attempts:
2 left
💡 Hint
Think about what happens when JwtService.verify encounters an expired token.
📝 Syntax
intermediate
1:30remaining
Which option correctly generates a JWT token with a 1 hour expiration in NestJS?
Given the JwtService instance, which code snippet correctly creates a token that expires in 1 hour?
NestJS
const payload = { username: 'user1', sub: 123 };
Athis.jwtService.sign(payload, { expiresIn: '1h' });
Bthis.jwtService.sign(payload, { expiration: 3600 });
Cthis.jwtService.sign(payload, { expires: 3600 });
Dthis.jwtService.sign(payload, { expireIn: '1h' });
Attempts:
2 left
💡 Hint
Check the correct option key name for expiration in JwtService.sign options.
🔧 Debug
advanced
2:00remaining
Why does this NestJS token validation code always throw an error?
Analyze the following code snippet. Why does it always throw an error when validating tokens?
NestJS
async validateToken(token: string) {
  const payload = this.jwtService.verify(token, { secret: 'wrongSecret' });
  return payload;
}
ABecause the secret used to verify the token is incorrect, causing a JsonWebTokenError.
BBecause the token is expired and no catch block handles the error.
CBecause the token is malformed and cannot be parsed.
DBecause the method is missing async/await keywords.
Attempts:
2 left
💡 Hint
Check the secret key used for verification compared to the signing key.
🧠 Conceptual
advanced
1:00remaining
What is the main purpose of the 'sub' claim in a JWT token?
In JWT tokens, the 'sub' claim is often included. What does it represent?
AIt contains the token's signature algorithm.
BIt specifies the token's expiration time.
CIt identifies the subject or user the token is issued for.
DIt indicates the token issuer's name.
Attempts:
2 left
💡 Hint
Think about what 'sub' stands for in identity tokens.
state_output
expert
2:30remaining
What is the value of 'isValid' after running this NestJS token validation code?
Given the following code, what will be the value of 'isValid' if the token is valid but the payload does not contain the 'sub' property?
NestJS
let isValid = false;
try {
  const payload = this.jwtService.verify(token);
  if (payload.sub) {
    isValid = true;
  }
} catch {
  isValid = false;
}
Atrue
Bfalse
CThrows an error
Dundefined
Attempts:
2 left
💡 Hint
Check the condition that sets isValid to true.