Login with X / Twitter
To enable X / Twitter Auth for your project, you need to set up an X OAuth 2.0 application and add the application credentials in the Supabase Dashboard.
Overview
We recommend using the X / Twitter (OAuth 2.0) provider. The legacy Twitter (OAuth 1.0a) provider will be deprecated in future releases.
Setting up X / Twitter logins for your application consists of 3 parts:
- Create and configure an X Project and App on the X Developer Dashboard.
- Add your X
API KeyandAPI Secret Keyto your Supabase Project. - Add the login code to your Supabase JS Client App.
Access your X developer account
- Go to developer.x.com.
- Click on
Sign inat the top right to log in.
Find your callback URL
The next step requires a callback URL, which looks like this: https://<project-ref>.supabase.co/auth/v1/callback
- Go to your Supabase Project Dashboard
- Click on the
Authenticationicon in the left sidebar - Click on
Sign In / Providersunder the Configuration section - Click on X / Twitter (OAuth 2.0) from the accordion list to expand and you'll find your Callback URL, you can click
Copyto copy it to the clipboard
For testing OAuth locally with the Supabase CLI see the local development docs.
Create an X OAuth app
- Click
+ Create Project.- Enter your project name, click
Next. - Select your use case, click
Next. - Enter a description for your project, click
Next. - Enter a name for your app, click
Next. - Copy and save your
API Key(this is yourclient_id). - Copy and save your
API Secret Key(this is yourclient_secret). - Click on
App settingsto proceed to next steps.
- Enter your project name, click
- At the bottom, you will find
User authentication settings. Click onSet up. - Under
User authentication settings, you can configureApp permissions. - Make sure you turn ON
Request email from users. - Select
Web App...as theType of App. - Under
App infoconfigure the following.- Enter your
Callback URL. Check the Find your callback URL section above to learn how to obtain your callback URL. - Enter your
Website URL(tip: tryhttp://127.0.0.1:portorhttp://www.localhost:portduring development) - Enter your
Terms of service URL. - Enter your
Privacy policy URL.
- Enter your
- Click
Save.
Enter your X credentials into your Supabase project
- Go to your Supabase Project Dashboard
- In the left sidebar, click the
Authenticationicon (near the top) - Click on
Providersunder the Configuration section - Click on X / Twitter (OAuth 2.0) from the accordion list to expand and turn X / Twitter (OAuth 2.0) Enabled to ON
- Enter your X / Twitter (OAuth 2.0) Client ID and X / Twitter (OAuth 2.0) Client Secret saved in the previous step
- Click
Save
You can also configure the X / Twitter (OAuth 2.0) auth provider using the Management API:
1# Get your access token from https://supabase.com/dashboard/account/tokens2export SUPABASE_ACCESS_TOKEN="your-access-token"3export PROJECT_REF="your-project-ref"45# Configure X / Twitter (OAuth 2.0) auth provider6curl -X PATCH "https://api.supabase.com/v1/projects/$PROJECT_REF/config/auth" \7 -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \8 -H "Content-Type: application/json" \9 -d '{10 "external_x_enabled": true,11 "external_x_client_id": "your-x-api-key",12 "external_x_secret": "your-x-api-secret-key"13 }'Add login code to your client app
Make sure you're using the right supabase client in the following code.
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 for instructions on creating your Supabase client.
When your user signs in, call signInWithOAuth() with x as the provider:
1import { createClient } from '@supabase/supabase-js'2const supabase = createClient(3 'https://your-project-id.supabase.co',4 'sb_publishable_... or anon key'5)67// ---cut---8async function signInWithX() {9 const { data, error } = await supabase.auth.signInWithOAuth({10 provider: 'x',11 })12}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.
In the browser, signInWithOAuth automatically redirects to the OAuth provider's authentication endpoint, which then redirects to your endpoint.
1await ..({2 ,3 : {4 : `http://example.com/auth/callback`,5 },6})At the callback endpoint, handle the code exchange to save the user session.
Create a new file at app/auth/callback/route.ts and populate with the following:
app/auth/callback/route.ts
1import { NextResponse } from 'next/server'2// The client you created from the Server-Side Auth instructions3import { createClient } from '@/utils/supabase/server'45export async function GET(request: Request) {6 const { searchParams, origin } = new URL(request.url)7 const code = searchParams.get('code')8 // if "next" is in param, use it as the redirect URL9 let next = searchParams.get('next') ?? '/'10 if (!next.startsWith('/')) {11 // if "next" is not a relative URL, use the default12 next = '/'13 }1415 if (code) {16 const supabase = await createClient()17 const { error } = await supabase.auth.exchangeCodeForSession(code)18 if (!error) {19 const forwardedHost = request.headers.get('x-forwarded-host') // original origin before load balancer20 const isLocalEnv = process.env.NODE_ENV === 'development'21 if (isLocalEnv) {22 // we can be sure that there is no load balancer in between, so no need to watch for X-Forwarded-Host23 return NextResponse.redirect(`${origin}${next}`)24 } else if (forwardedHost) {25 return NextResponse.redirect(`https://${forwardedHost}${next}`)26 } else {27 return NextResponse.redirect(`${origin}${next}`)28 }29 }30 }3132 // return the user to an error page with instructions33 return NextResponse.redirect(`${origin}/auth/auth-code-error`)34}When your user signs out, call signOut() to remove them from the browser session and any objects from localStorage:
1async function () {2 const { } = await ..()3}