Repository navigation
feat: move sign-in to @convex-dev/auth 2.0 #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { getAuthUserId as getSessionUserId } from "@convex-dev/auth/server"; | ||
| import type { Auth } from "convex/server"; | ||
| import type { Id } from "../_generated/dataModel"; | ||
|
|
||
| type SessionLookupCtx = Parameters<typeof getSessionUserId>[0]; | ||
|
|
||
| /** | ||
| * The signed-in user, from the caller's session in the auth component, or | ||
| * null. Works in queries, mutations and actions: @convex-dev/auth types its | ||
| * context after a query's runQuery, which an action's does not match. | ||
| */ | ||
| export async function getAuthUserId(ctx: { | ||
| auth: Auth; | ||
| runQuery: unknown; | ||
| }): Promise<Id<"users"> | null> { | ||
| return await getSessionUserId<"users">(ctx as SessionLookupCtx); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,14 @@ | ||
| /** Keep web redirects same-origin and allow the CLI's exact loopback URL. */ | ||
| export function resolveAuthRedirect( | ||
| redirectTo: string, | ||
| siteUrl: string, | ||
| ): string { | ||
| const loopback = | ||
| /^http:\/\/127\.0\.0\.1:([1-9]\d{3,4})\/gitdog\/callback\?state=[a-f0-9]{64}$/.exec( | ||
| redirectTo, | ||
| ); | ||
| if ( | ||
| loopback && | ||
| loopback[0] === redirectTo && | ||
| Number(loopback[1]) >= 1024 && | ||
| Number(loopback[1]) <= 65535 | ||
| ) { | ||
| return redirectTo; | ||
| } | ||
| /** | ||
| * Local ports the CLI listens on for its sign-in callback, tried in order. | ||
| * @convex-dev/auth 2.0 returns only to exact origins, so the CLI cannot pick | ||
| * a random port. packages/cli/src/auth.ts lists the same ports. | ||
| */ | ||
| export const CLI_LOGIN_PORTS = [41721, 41722, 41723, 41724, 41725] as const; | ||
|
|
||
| const base = new URL(siteUrl); | ||
| const target = new URL(redirectTo, base); | ||
| if ( | ||
| (base.protocol === "https:" || base.protocol === "http:") && | ||
| target.origin === base.origin && | ||
| target.username === "" && | ||
| target.password === "" | ||
| ) { | ||
| return target.toString(); | ||
| } | ||
| throw new Error("Invalid authentication redirect destination"); | ||
| /** Where a sign-in may return: Gitdog's site or the CLI's loopback listener. */ | ||
| export function allowedSignInOrigins(siteUrl: string): string[] { | ||
| return [ | ||
| new URL(siteUrl).origin, | ||
| ...CLI_LOGIN_PORTS.map((port) => `http://127.0.0.1:${port}`), | ||
| ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,5 @@ | ||
| import { convexAuthProvider } from "@convex-dev/auth/server"; | ||
|
|
||
| export default { | ||
| providers: [ | ||
| { | ||
| domain: process.env.CONVEX_SITE_URL, | ||
| applicationID: "convex", | ||
| }, | ||
| ], | ||
| providers: [convexAuthProvider()], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,30 @@ | ||
| import { convexAuth } from "@convex-dev/auth/server"; | ||
| import GitHub from "@auth/core/providers/github"; | ||
| import type { MutationCtx } from "./_generated/server"; | ||
| import { resolveAuthRedirect } from "./_utils/authRedirect"; | ||
| import { | ||
| fetchAccessibleGitHubInstallationIds, | ||
| persistGitHubInstallationAttestation, | ||
| } from "./github/oauthInstallationAttestation"; | ||
| import { setupConvexAuth } from "@convex-dev/auth/server"; | ||
| import { setupGithub } from "@convex-dev/auth/methods/github/server"; | ||
| import { components, internal } from "./_generated/api"; | ||
| import { allowedSignInOrigins } from "./_utils/authRedirect"; | ||
|
|
||
| export const { auth, signIn, signOut, store } = convexAuth({ | ||
| providers: [ | ||
| GitHub({ | ||
| clientId: process.env.GITHUB_APP_CLIENT_ID, | ||
| clientSecret: process.env.GITHUB_APP_CLIENT_SECRET, | ||
| async profile(profile, tokens) { | ||
| const githubAccountId = profile.id.toString(); | ||
| const accessToken = tokens.access_token; | ||
| if (typeof accessToken !== "string" || accessToken.trim() === "") { | ||
| throw new Error("GitHub user authorization is unavailable"); | ||
| } | ||
| const githubInstallationIds = | ||
| await fetchAccessibleGitHubInstallationIds(accessToken); | ||
| return { | ||
| id: githubAccountId, | ||
| name: profile.login, // Use login (username) instead of display name | ||
| email: typeof profile.email === "string" ? profile.email : undefined, | ||
| image: profile.avatar_url, | ||
| githubAccountId, | ||
| githubInstallationIds, | ||
| }; | ||
| }, | ||
| }), | ||
| ], | ||
| callbacks: { | ||
| redirect: async ({ redirectTo }) => { | ||
| const siteUrl = process.env.SITE_URL; | ||
| if (!siteUrl) throw new Error("SITE_URL is not configured"); | ||
| return resolveAuthRedirect(redirectTo, siteUrl); | ||
| }, | ||
| createOrUpdateUser: async (ctx, args) => | ||
| await persistGitHubInstallationAttestation(ctx as MutationCtx, args), | ||
| const siteUrl = process.env.SITE_URL; | ||
| if (!siteUrl) throw new Error("SITE_URL is not configured"); | ||
|
|
||
| // One-hour access tokens, as before 2.0: GITDOG_TOKEN automation sends one | ||
| // without refreshing it. | ||
| const core = setupConvexAuth({ | ||
| component: components.auth, | ||
| accessTokenTtlSeconds: 60 * 60, | ||
| }); | ||
|
|
||
| export const { signOut, refreshSession, isAuthenticated } = core; | ||
|
|
||
| // Sign-in never sees the GitHub user token in 2.0, so it records no | ||
| // installation access. "Verify GitHub access" (github/accessVerification.ts) | ||
| // does, whenever installation recovery reports reauth_required. | ||
| export const { startSignInWithGithub, completeSignInWithGithub } = setupGithub( | ||
| core, | ||
| { | ||
| component: components.authGithub, | ||
| allowedRedirectOrigins: allowedSignInOrigins(siteUrl), | ||
| }, | ||
| ).attachUserCallbacks({ | ||
| createUser: internal.users.internalMutation.createUserFromGithub, | ||
| onSignIn: internal.users.internalMutation.updateUserFromGithub, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,24 @@ | ||
| import { defineApp } from "convex/server"; | ||
| import { v } from "convex/values"; | ||
| import auth from "@convex-dev/auth/convex.config"; | ||
| import authGithub from "@convex-dev/auth/components/github/convex.config"; | ||
|
|
||
| const app = defineApp(); | ||
| const app = defineApp({ | ||
| env: { | ||
| GITHUB_APP_CLIENT_ID: v.string(), | ||
| GITHUB_APP_CLIENT_SECRET: v.string(), | ||
| }, | ||
| }); | ||
|
|
||
| // Sessions and tokens live in the core; GitHub accounts in authGithub, whose | ||
| // OAuth callback is served at <site>/oauth/github/callback. | ||
| app.use(auth); | ||
| app.use(authGithub, { | ||
| httpPrefix: "/oauth/github", | ||
| env: { | ||
| CLIENT_ID: app.env.GITHUB_APP_CLIENT_ID, | ||
| CLIENT_SECRET: app.env.GITHUB_APP_CLIENT_SECRET, | ||
| }, | ||
| }); | ||
|
|
||
| export default app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.