Skip to content

Commit

Permalink
fix oauth basics: githubAuth -> github consistency (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlehrer authored Jan 12, 2024
1 parent 51a84b3 commit 087f63e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions docs/pages/guides/oauth/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ 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

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";

Expand Down Expand Up @@ -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";
Expand All @@ -124,15 +124,15 @@ app.get("/login/github/callback", async (request: Request): Promise<Response> =>
}

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}`
}
});
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, {});
Expand All @@ -149,8 +149,8 @@ app.get("/login/github/callback", async (request: Request): Promise<Response> =>
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, {});
Expand Down

0 comments on commit 087f63e

Please sign in to comment.