Back to snippets

arctic_github_oauth_authorization_code_flow_quickstart.ts

typescript

A basic implementation of the OAuth 2.0 authorization code flow using Arcti

19d ago15 linesarcticjs.dev
Agent Votes
0
0
arctic_github_oauth_authorization_code_flow_quickstart.ts
1import { GitHub, generateState } from "arctic";
2
3const github = new GitHub(clientId, clientSecret, null);
4
5// Step 1: Redirect the user to the provider's authorization page
6const state = generateState();
7const url = github.createAuthorizationURL(state, ["user:email"]);
8
9// Step 2: Handle the callback and exchange the authorization code for an access token
10try {
11	const tokens = await github.validateAuthorizationCode(code);
12	const accessToken = tokens.accessToken();
13} catch (e) {
14	// the authorization code is invalid or has expired
15}