Kakao

获取你的 Kakao 凭证

要使用 Kakao 登录,你需要一个客户端 ID 和客户端密钥。你可以从 Kakao 开发者门户获取它们。

请确保在本地开发时将重定向 URL 设置为 http://localhost:3000/api/auth/callback/kakao。对于生产环境,你应将其设置为你应用的 URL。如果更改了认证路由的基础路径,你也应该相应更新重定向 URL。

配置提供者

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

  • 默认的权限范围是 account_emailprofile_imageprofile_nickname
  • 请注意,获取 account_email 需要应用是 企业应用(已完成企业验证的应用)。更多详情,请参阅 Kakao 登录权限范围文档
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, 
        }, 
    }
})

Sign In with Kakao

To sign in with Kakao, you can use the signIn.social function provided by the client. The signIn function takes an object with the following properties:

  • provider: The provider to use. It should be set to kakao.
auth-client.ts
import { createAuthClient } from "better-auth/client"
const authClient =  createAuthClient()

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

On this page