|
| 1 | +# GitHub (Supabase) |
| 2 | + |
| 3 | +To enable GitHub Auth for your project, you need to set up a GitHub OAuth application and add the application credentials to your Supabase Dashboard. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Setting up GitHub logins for your application consists of 3 parts: |
| 8 | + |
| 9 | +- Create and configure a GitHub OAuth App on [GitHub](https://github.com/) |
| 10 | +- Add your GitHub OAuth keys to your [Supabase Project](https://supabase.com/dashboard) |
| 11 | +- Add the login code to your [Supabase JS Client App](https://github.com/supabase/supabase-js) |
| 12 | + |
| 13 | +## Find your callback URL |
| 14 | + |
| 15 | +The next step requires a callback URL, which looks like this: `https://<project-ref>.supabase.co/auth/v1/callback` |
| 16 | + |
| 17 | +- Go to your [Supabase Project Dashboard](https://supabase.com/dashboard) |
| 18 | +- Click on the `Authentication` icon in the left sidebar |
| 19 | +- Click on [`Providers`](https://supabase.com/dashboard/project/_/auth/providers) under the Configuration section |
| 20 | +- Click on **GitHub** from the accordion list to expand and you'll find your **Callback URL**, you can click `Copy` to copy it to the clipboard |
| 21 | + |
| 22 | +For testing OAuth locally with the Supabase CLI see the [local development docs](https://supabase.com/docs/guides/cli/local-development#use-auth-locally). |
| 23 | + |
| 24 | +## Register a new OAuth application on GitHub |
| 25 | + |
| 26 | +- Navigate to the [OAuth apps page](https://github.com/settings/developers) |
| 27 | +- Click `Register a new application`. If you've created an app before, click `New OAuth App` here. |
| 28 | +- In `Application name`, type the name of your app. |
| 29 | +- In `Homepage URL`, type the full URL to your app's website. |
| 30 | +- In `Authorization callback URL`, type the callback URL of your app. |
| 31 | +- Leave `Enable Device Flow` unchecked. |
| 32 | +- Click `Register Application`. |
| 33 | + |
| 34 | +Copy your new OAuth credentials |
| 35 | + |
| 36 | +- Copy and save your `Client ID`. |
| 37 | +- Click `Generate a new client secret`. |
| 38 | +- Copy and save your `Client secret`. |
| 39 | + |
| 40 | +## Enter your GitHub credentials into your Supabase project |
| 41 | + |
| 42 | +- Go to your [Supabase Project Dashboard](https://supabase.com/dashboard) |
| 43 | +- In the left sidebar, click the `Authentication` icon (near the top) |
| 44 | +- Click on [`Providers`](https://supabase.com/dashboard/project/_/auth/providers) under the Configuration section |
| 45 | +- Click on **GitHub** from the accordion list to expand and turn **GitHub Enabled** to ON |
| 46 | +- Enter your **GitHub Client ID** and **GitHub Client Secret** saved in the previous step |
| 47 | +- Click `Save` |
| 48 | + |
| 49 | +## Add login code to your client app |
| 50 | + |
| 51 | +Make sure you're using the right `supabase` client in the following code. |
| 52 | + |
| 53 | +If you're not using Server-Side Rendering or cookie-based Auth, you can directly use the `createClient` from `@supabase/supabase-js`. If you're using Server-Side Rendering, see the [Server-Side Auth guide](https://supabase.com/docs/guides/auth/server-side/creating-a-client) for instructions on creating your Supabase client. |
| 54 | + |
| 55 | +When your user signs in, call [`signInWithOAuth()`](https://supabase.com/docs/reference/javascript/auth-signinwithoauth) with `github` as the `provider`: |
| 56 | + |
| 57 | +```ts |
| 58 | +async function signInWithGithub() { |
| 59 | + const { data, error } = await supabase.auth.signInWithOAuth({ |
| 60 | + provider: "github", |
| 61 | + }); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +For a PKCE flow, for example in Server-Side Auth, you need an extra step to handle the code exchange. When calling `signInWithOAuth`, provide a `redirectTo` URL which points to a callback route. This redirect URL should be added to your [redirect allow list](https://supabase.com/docs/guides/auth/redirect-urls). |
| 66 | + |
| 67 | +In the browser, `signInWithOAuth` automatically redirects to the OAuth provider's authentication endpoint, which then redirects to your endpoint. |
| 68 | + |
| 69 | +```ts |
| 70 | +await supabase.auth.signInWithOAuth({ |
| 71 | + provider, |
| 72 | + options: { |
| 73 | + redirectTo: `http://example.com/auth/callback`, |
| 74 | + }, |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +At the callback endpoint, handle the code exchange to save the user session. |
| 79 | + |
| 80 | +Create a new file at `app/auth/callback/route.ts` and populate with the following: |
| 81 | + |
| 82 | +app/auth/callback/route.ts |
| 83 | + |
| 84 | +```ts |
| 85 | +import { NextResponse } from "next/server"; |
| 86 | +// The client you created from the Server-Side Auth instructions |
| 87 | +import { createClient } from "@/utils/supabase/server"; |
| 88 | + |
| 89 | +export async function GET(request: Request) { |
| 90 | + const { searchParams, origin } = new URL(request.url); |
| 91 | + const code = searchParams.get("code"); |
| 92 | + // if "next" is in param, use it as the redirect URL |
| 93 | + const next = searchParams.get("next") ?? "/"; |
| 94 | + |
| 95 | + if (code) { |
| 96 | + const supabase = await createClient(); |
| 97 | + const { error } = await supabase.auth.exchangeCodeForSession(code); |
| 98 | + if (!error) { |
| 99 | + const forwardedHost = request.headers.get("x-forwarded-host"); // original origin before load balancer |
| 100 | + const isLocalEnv = process.env.NODE_ENV === "development"; |
| 101 | + if (isLocalEnv) { |
| 102 | + // we can be sure that there is no load balancer in between, so no need to watch for X-Forwarded-Host |
| 103 | + return NextResponse.redirect(`${origin}${next}`); |
| 104 | + } else if (forwardedHost) { |
| 105 | + return NextResponse.redirect(`https://${forwardedHost}${next}`); |
| 106 | + } else { |
| 107 | + return NextResponse.redirect(`${origin}${next}`); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // return the user to an error page with instructions |
| 113 | + return NextResponse.redirect(`${origin}/auth/auth-code-error`); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +When your user signs out, call [signOut()](https://supabase.com/docs/reference/javascript/auth-signout) to remove them from the browser session and any objects from localStorage: |
| 118 | + |
| 119 | +```ts |
| 120 | +async function signOut() { |
| 121 | + const { error } = await supabase.auth.signOut(); |
| 122 | +} |
| 123 | +``` |
0 commit comments