Kakao

Kakao 提供者的配置和使用方法。

获取 Kakao 凭据

要使用 Kakao 登录,需要客户端 ID 和客户端密钥。可以从 Kakao 开发者门户获取这些信息。

对于本地开发,需要将重定向 URL 设置为 http://localhost:3000/api/auth/callback/kakao。在生产环境中,应将其设置为应用程序的 URL。如果更改了身份验证路由的默认路径,则需要相应地更新重定向 URL。

配置提供者

要配置提供者,需要导入提供者并将其传递给实例的 socialProviders 选项。

  • 默认范围为 account_email、profile_image、profile_nickname。
  • 请注意,要获取 account_email,应用必须是商业应用(已完成业务认证的应用)。详情请参阅 Kakao Login 范围文档。
auth.ts
import { betterAuth } from "better-auth"

export const auth = betterAuth({
    socialProviders: {
        kakao: { 
            clientId: process.env.KAKAO_CLIENT_ID as string, 
            clientSecret: process.env.KAKAO_CLIENT_SECRET as string, 
        }, 
    }
})

使用 Kakao 登录

要使用 Kakao 登录,可以在客户端使用提供的 signIn.social 函数。signIn 函数接收一个包含以下属性的对象:

  • provider:要使用的提供者。必须设置为 kakao。
auth-client.ts
import { createAuthClient } from "better-auth/client"
const authClient =  createAuthClient()

const signIn = async () => {
    const data = await authClient.signIn.social({
        provider: "kakao"
    })
}