diff --git a/docs/pages/guides/oauth/basics.md b/docs/pages/guides/oauth/basics.md index 944f222d5..ba3cdc239 100644 --- a/docs/pages/guides/oauth/basics.md +++ b/docs/pages/guides/oauth/basics.md @@ -62,7 +62,7 @@ Import `GitHub` from Arctic and initialize it with the client ID and secret. // auth.ts import { GitHub } from "arctic"; -export const githubAuth = new GitHub(clientId, clientSecret); +export const github = new GitHub(clientId, clientSecret); ``` ## Creating authorization URL @@ -70,7 +70,7 @@ export const githubAuth = new GitHub(clientId, clientSecret); Create a route to handle authorization. Generate a new state, create a new authorization URL with `createAuthorizationURL()`, store the state, and redirect the user to the authorization URL. The user will be prompted to sign in with GitHub. ```ts -import { githubAuth } from "./auth.js"; +import { github } from "./auth.js"; import { generateState } from "arctic"; import { serializeCookie } from "oslo/cookie"; @@ -103,7 +103,7 @@ You can now create a sign in button with just an anchor tag. In the callback route, first get the state from the cookie and the search params and compare them. Validate the authorization code in the search params with `validateAuthorizationCode()`. This will throw a [`OAuth2RequestError`](https://oslo.js.org/reference/oauth2/OAuth2RequestError) if the code or credentials are invalid. After validating the code, get the user's profile using the access token. Check if the user is already registered with the GitHub ID and create a new user if not. Finally, create a new session and set the session cookie. ```ts -import { githubAuth, lucia } from "./auth.js"; +import { github, lucia } from "./auth.js"; import { OAuth2RequestError } from "arctic"; import { generateId } from "lucia"; import { parseCookies } from "oslo/cookie"; @@ -124,7 +124,7 @@ app.get("/login/github/callback", async (request: Request): Promise => } try { - const tokens = await githubAuth.validateAuthorizationCode(code); + const tokens = await github.validateAuthorizationCode(code); const githubUserResponse = await fetch("https://api.github.com/user", { headers: { Authorization: `Bearer ${tokens.accessToken}` @@ -132,7 +132,7 @@ app.get("/login/github/callback", async (request: Request): Promise => }); const githubUserResult: GitHubUserResult = await githubUserResponse.json(); - const existingUser = await db.table("user").where("github_id", "=", githubUser.id).get(); + const existingUser = await db.table("user").where("github_id", "=", githubUserResult.id).get(); if (existingUser) { const session = await lucia.createSession(existingUser.id, {}); @@ -149,8 +149,8 @@ app.get("/login/github/callback", async (request: Request): Promise => const userId = generateId(15); await db.table("user").insert({ id: userId, - username: github.login, - github_id: github.id + username: githubUserResult.login, + github_id: githubUserResult.id }); const session = await lucia.createSession(userId, {});