LINE

LINE 提供者的设置和使用。

获取你的 LINE 凭证

  1. 在 LINE Developers Console 中创建一个频道。
  2. 记下你的 Channel ID(client_id)和 Channel secret(client_secret)。
  3. 在频道设置中,添加你的重定向 URI,例如用于本地开发的 http://localhost:3000/api/auth/callback/line。
  4. 启用所需的作用域(至少需要 openid;如果需要名称、头像或电子邮件,则添加 profile、email)。

详细信息请参阅 LINE Login v2.1 参考文档。

配置提供者

将你的 LINE 凭证添加到 socialProviders.line 的身份验证配置中。

auth.ts
import { betterAuth } from "better-auth";

export const auth = betterAuth({
  socialProviders: {
    line: {
      clientId: process.env.LINE_CLIENT_ID as string,
      clientSecret: process.env.LINE_CLIENT_SECRET as string,
      // redirectURI: "https://your.app/api/auth/callback/line", // 取消注释以使用自定义重定向 URI
      // scope: ["custom"], // 取消注释以添加额外作用域
      // disableDefaultScope: true, // 取消注释以替换默认作用域 [`openid`, `profile`, `email`]
    },
  },
});

使用方法

使用 LINE 登录

使用客户端的 signIn.social,并设置 provider: "line"。

auth-client.ts
import { createAuthClient } from "better-auth/client";
const authClient = createAuthClient();

async function signInWithLINE() {
  const res = await authClient.signIn.social({ provider: "line" });
}

使用 LINE ID Token 登录(可选)

如果你在客户端获得了 LINE 的 ID Token,可以直接使用它登录,无需重定向。

auth-client.ts
await authClient.signIn.social({
  provider: "line",
  idToken: {
    token: "<LINE_ID_TOKEN>",
    accessToken: "<LINE_ACCESS_TOKEN>",
  },
});

注意事项

  • 默认作用域包括 openid profile email。可根据需要通过提供者选项进行调整。
  • 确保重定向 URI 与 LINE Developers Console 中配置的值完全匹配。
  • LINE ID Token 验证使用官方端点,并按照规范检查受众(audience)和可选的 nonce。

要设计登录按钮?请遵循 LINE 按钮的指南。

多频道支持

LINE 要求针对不同国家(日本、泰国、台湾等)使用不同的 OAuth 频道,每个频道都有自己独立的 clientId 和 clientSecret。标准的 socialProviders.line 配置只支持单一频道。

要支持多个国家/频道,请使用 Generic OAuth 插件 结合 line() 辅助函数。这样你就可以用不同的 providerId 配置多个 LINE 提供者:

auth.ts
import { betterAuth } from "better-auth";
import { genericOAuth, line } from "better-auth/plugins";

export const auth = betterAuth({
  plugins: [
    genericOAuth({
      config: [
        // 日本频道
        line({
          providerId: "line-jp",
          clientId: process.env.LINE_JP_CLIENT_ID,
          clientSecret: process.env.LINE_JP_CLIENT_SECRET,
        }),
        // 泰国频道
        line({
          providerId: "line-th",
          clientId: process.env.LINE_TH_CLIENT_ID,
          clientSecret: process.env.LINE_TH_CLIENT_SECRET,
        }),
        // 台湾频道
        line({
          providerId: "line-tw",
          clientId: process.env.LINE_TW_CLIENT_ID,
          clientSecret: process.env.LINE_TW_CLIENT_SECRET,
        }),
      ],
    }),
  ],
});

登录时,使用对应的 providerId(例如 "line-jp"、"line-th"、"line-tw")来指定使用哪个频道:

auth-client.ts
import { createAuthClient } from "better-auth/client";

const authClient = createAuthClient();

// 使用日本频道登录
await authClient.signIn.social({ provider: "line-jp" });

// 使用泰国频道登录
await authClient.signIn.social({ provider: "line-th" });