Auth

Login with Kakao


To enable Kakao Auth for your project, you need to set up a Kakao OAuth application and add the application credentials to your Supabase Dashboard.

Overview

Kakao OAuth consists of six broad steps:

  • Create and configure your app in the Kakao Developers Portal.
  • Obtain a REST API key - this serves as the client_id.
  • Obtain a Kakao Login Client Secret code - this serves as the client_secret.
  • Configure additional settings in the Kakao Developers Portal.
  • Add your client id and client secret keys to your Supabase Project.
  • Add the login code to your Supabase JS Client App.

Access your Kakao Developer account

Kakao Developers Portal

Create and configure your app

  • Go to App.
  • Click on Create app at the top.
  • Fill out your app information:
    • App icon.
    • App name.
    • Company name.
    • Category.
    • App primary domain.
  • Click Save at the bottom right.

Obtain a REST API key

This serves as the client_id when you make API calls to authenticate the user.

  • Go to App.
  • Click on your app.
  • Go to App Settings > App > Platform Key.
  • In the Platform Key section is REST API key. This will become your client_id later.

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 Authentication icon in the left sidebar
  • Click on Sign In / Providers under the Configuration section
  • Click on Kakao from the accordion list to expand and you'll find your Callback URL, you can click Copy to copy it to the clipboard
  • To add a callback URL on Kakao, go to App Settings > App > Platform Key.
  • Click on the REST API key you want to use.
  • In the edit page, enter your callback URL in the Kakao Login Redirect URI field.
  • Click Save in the bottom right.

Obtain a client secret

  • Go to App Settings > App > Platform Key.
  • Click on the REST API key you want to use.
  • Note the Kakao Login Client Secret code. This serves as a client_secret for your Supabase project.
  • Make sure you activate Kakao Login Client Secret.

Additional configurations on Kakao Developers portal

  • Go to Product Settings > Kakao Login > General.
  • Set State to "ON" in the Usage settings section to enable Kakao Login.
  • Go to Product Settings > Kakao Login > Consent Items.
  • Set the following scopes under the Consent Items:
    • account_email
    • profile_image
    • profile_nickname

Kakao consent items configuration

Add your OAuth credentials to Supabase

  • Go to your Supabase Project Dashboard
  • In the left sidebar, click the Authentication icon (near the top)
  • Click on Providers under the Configuration section
  • Click on Kakao from the accordion list to expand and turn Kakao Enabled to ON
  • Enter your Kakao Client ID and Kakao Client Secret saved in the previous step
  • Click Save

Add login code to your client app

When your user signs in, call signInWithOAuth() with kakao as the provider:

1
async function () {
2
const { , } = await ..({
3
: 'kakao',
4
})
5
}

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.

1
await ..({
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
1
import { NextResponse } from 'next/server'
2
// The client you created from the Server-Side Auth instructions
3
import { createClient } from '@/utils/supabase/server'
4
5
export 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 URL
9
let next = searchParams.get('next') ?? '/'
10
if (!next.startsWith('/')) {
11
// if "next" is not a relative URL, use the default
12
next = '/'
13
}
14
15
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 balancer
20
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-Host
23
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
}
31
32
// return the user to an error page with instructions
33
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:

1
async function signOut() {
2
const { error } = await supabase.auth.signOut()
3
}

Using Kakao Login JS SDK

Kakao Login JS SDK is an official Kakao SDK for authenticating Kakao users on websites.

Exchange the authorization code returned by Kakao API for an ID Token.

For example, this code shows a how to get ID Token:

1
const requestUrl = new URL(request.url);
2
const code = requestUrl.searchParams.get('code');
3
4
if (code) {
5
const res = await fetch('https://kauth.kakao.com/oauth/token', {
6
method: 'POST',
7
headers: {
8
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
9
},
10
body: new URLSearchParams({
11
grant_type: 'authorization_code',
12
client_id: '<CLIENT_ID>',
13
redirect_uri: '<url>/api/auth/kakao/oidc',
14
code,
15
client_secret: '<CLIENT_SECRET>',
16
}),
17
});
18
19
const {id_token} = await res.json();
20
}

Use the ID Token to sign in:

1
const res = await auth.signInWithIdToken({
2
provider: 'kakao',
3
token: id_token,
4
});

Configuration

  1. Set State to "ON" under OpenID Connect Activation on the Kakao Developers portal.
  2. Add openid to scope along with the scope values you wish to obtain consent for.

Resources