42 School
OAuth 2.0 provider for 42 School.
Also see the OAuth 2.0 guide.
Initialization
FortyTwo
takes a client ID, client secret, and redirect URI.
import { FortyTwo } from "arctic";
const fortyTwo = new FortyTwo(clientId, clientSecret, redirectURI);
Create authorization URL
import { generateState } from "arctic";
const state = generateState();
const scopes = ["public", "projects"];
const url = fortyTwo.createAuthorizationURL(state, scopes);
Validate authorization code
validateAuthorizationCode()
will either return an OAuth2Tokens
, or throw one of OAuth2RequestError
, ArcticFetchError
, or a standard Error
(parse errors). 42 School will return an access token with an expiration.
import { OAuth2RequestError, ArcticFetchError } from "arctic";
try {
const tokens = await fortyTwo.validateAuthorizationCode(code);
const accessToken = tokens.accessToken();
const accessToken = tokens.accessTokenExpiresAt();
} catch (e) {
if (e instanceof OAuth2RequestError) {
// Invalid authorization code, credentials, or redirect URI
const code = e.code;
// ...
}
if (e instanceof ArcticFetchError) {
// Failed to call `fetch()`
const cause = e.cause;
// ...
}
// Parse error
}
Get user profile
You can retrieve user information via the /v2/me
endpoint.
const response = await fetch("https://api.intra.42.fr/v2/me", {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
const user = await response.json();