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 * as arctic from "arctic";
const fortyTwo = new arctic.FortyTwo(clientId, clientSecret, redirectURI);
Create authorization URL
import * as arctic from "arctic";
const state = arctic.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
, UnexpectedResponseError
, or UnexpectedErrorResponseBodyError
. 42 School will return an access token with an expiration.
import * as arctic from "arctic";
try {
const tokens = await fortyTwo.validateAuthorizationCode(code);
const accessToken = tokens.accessToken();
const accessToken = tokens.accessTokenExpiresAt();
} catch (e) {
if (e instanceof arctic.OAuth2RequestError) {
// Invalid authorization code, credentials, or redirect URI
const code = e.code;
// ...
}
if (e instanceof arctic.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();