0
0
GraphQLquery~10 mins

Authentication errors in context in GraphQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the user is authenticated in the context.

GraphQL
if (!context.[1]) {
  throw new Error('Not authenticated');
}
Drag options to blanks, or click blank then click option'
Atoken
Bauth
Cuser
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Checking context.auth instead of context.user
Checking context.token which may not be set
Using context.session which might not exist
2fill in blank
medium

Complete the code to throw an authentication error with a specific message.

GraphQL
throw new [1]('Authentication failed');
Drag options to blanks, or click blank then click option'
ATypeError
BReferenceError
CError
DAuthenticationError
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic Error which is less descriptive
Using TypeError or ReferenceError which are unrelated
Not throwing an error at all
3fill in blank
hard

Fix the error in the code to correctly access the authentication token from context headers.

GraphQL
const token = context.req.headers.[1];
Drag options to blanks, or click blank then click option'
AAuthorization
Bauthorization
Ctoken
Dauth
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'Authorization' which may be undefined
Using 'auth' or 'token' which are not standard header names
4fill in blank
hard

Fill both blanks to extract the token from the authorization header string.

GraphQL
const token = context.req.headers.authorization?.[1](' ')[[2]];
Drag options to blanks, or click blank then click option'
Asplit
B1
C0
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which returns 'Bearer' instead of the token
Using join instead of split
Not handling the case when authorization is undefined
5fill in blank
hard

Fill all three blanks to verify the token and attach the user to context.

GraphQL
const user = await verifyToken([1]);
if (!user) {
  throw new AuthenticationError('Invalid token');
}
context.[2] = user;
return [3];
Drag options to blanks, or click blank then click option'
Atoken
Buser
Ccontext.user
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable to verifyToken
Not attaching the user to context
Returning context.user instead of user