Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the OAuth client from the Astro integration package.
Astro
import { [1] } from '@astrojs/oauth';
Attempts:
3 left
💡 Hint
Common Mistakes
Using OAuthClient or OAuthProvider which are not exported by '@astrojs/oauth'.
Trying to import from a wrong package.
✗ Incorrect
The correct import from '@astrojs/oauth' is 'OAuth' which provides the OAuth client functionality.
2fill in blank
mediumComplete the code to create a new OAuth instance with GitHub as the provider.
Astro
const oauth = new OAuth({
provider: '[1]',
clientId: import.meta.env.GITHUB_CLIENT_ID,
clientSecret: import.meta.env.GITHUB_CLIENT_SECRET
});Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'GitHub' instead of lowercase 'github'.
Choosing a different provider name that does not match the service.
✗ Incorrect
To configure GitHub OAuth, the provider must be set to 'github' in lowercase.
3fill in blank
hardFix the error in the OAuth callback handler to correctly extract the code parameter.
Astro
export async function get({ url }) {
const code = url.searchParams.[1]('code');
if (!code) {
return new Response('Missing code', { status: 400 });
}
// Continue with token exchange
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' which modifies parameters instead of reading.
Using 'has' which returns a boolean, not the value.
Using 'getAll' which returns an array, not a string.
✗ Incorrect
The 'get' method of URLSearchParams retrieves the first value associated with the given key, which is needed to get the 'code' parameter.
4fill in blank
hardFill both blanks to complete the OAuth token exchange and return a JSON response.
Astro
const token = await oauth.[1](code); return new Response(JSON.stringify({ accessToken: token.[2] }), { headers: { 'Content-Type': 'application/json' } });
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fetchToken' which is not a method of the OAuth instance.
Accessing 'token' property instead of 'access_token'.
✗ Incorrect
The method to exchange code for token is 'getAccessToken', and the token property holding the token string is 'access_token'.
5fill in blank
hardFill all three blanks to create an OAuth redirect URL with the correct scopes and state parameter.
Astro
const redirectUrl = oauth.[1]({ scope: '[2]', state: [3] });
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'generateUrl' which is not the correct method.
Providing scopes as an array or comma-separated string instead of space-separated.
Passing state as a plain object instead of a string.
✗ Incorrect
The method to get the authorization URL is 'getAuthorizationUrl'. The scopes for GitHub OAuth to read user info and email are 'read:user user:email'. The state parameter is often a JSON string with CSRF token for security.
