VK

获取 VK ID 凭证

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

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

配置提供商

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

auth.ts
import { betterAuth } from "better-auth";
 
export const auth = betterAuth({
  socialProviders: {
    vk: { 
      clientId: process.env.VK_CLIENT_ID as string, 
      clientSecret: process.env.VK_CLIENT_SECRET as string, 
    },
  },
});

使用 VK 登录

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

  • provider:要使用的提供商。应该设置为 vk
auth-client.ts
import { createAuthClient } from "better-auth/client";
const authClient = createAuthClient();
 
const signIn = async () => {
  const data = await authClient.signIn.social({
    provider: "vk",
  });
};

On this page