LINE

获取你的 LINE 凭证

  1. 在 LINE 开发者控制台中创建一个通道。
  2. 请记下你的通道 ID(client_id)和通道密钥(client_secret)。
  3. 在通道设置中,添加你的重定向 URI,例如本地开发时使用 http://localhost:3000/api/auth/callback/line
  4. 启用所需的权限范围(至少 openid;如果需要名称、头像、电子邮件,可添加 profileemail)。

详情请参阅 LINE 登录 v2.1 参考:[https://developers.line.biz/en/reference/line-login/#issue-access-token]

配置提供者

在你的身份验证配置中,将你的 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", // uncomment to use a custom redirect URI
      // scope: ["custom"], // uncomment to add additional scopes
      // disableDefaultScope: true, // uncomment to replace default scopes [`openid`, `profile`, `email`]
    },
  },
});

用法

【Usage】

使用 LINE 登录

【Sign In with LINE】

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

【Use the client signIn.social with 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" });
}

使用 ID 令牌登录 LINE(可选)

【Sign In with LINE using ID Token (optional)】

如果你在客户端获取了 LINE ID 令牌,你可以直接登录,无需重定向。

【If you obtain the LINE ID token on the client, you can sign in directly without redirection.】

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

注意

【Notes】

  • 默认作用域包括 openid profile email。可根据需要通过提供商选项进行调整。
  • 请验证重定向 URI 是否与 LINE 开发者控制台中配置的值完全匹配。
  • LINE ID 令牌验证使用官方端点,并根据规范检查受众和可选的随机数(nonce)。

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

【Designing a login button? Follow LINE's button guidelines.】

多渠道支持

【Multi-Channel Support】

LINE 对不同国家(日本、泰国、台湾等)需要单独的 OAuth 渠道,每个渠道都有自己的 clientIdclientSecret。标准的 socialProviders.line 配置只支持单个渠道。

【LINE requires separate OAuth channels for different countries (Japan, Thailand, Taiwan, etc.), each with its own clientId and clientSecret. The standard socialProviders.line configuration only supports a single channel.】

要支持多个国家/渠道,请使用 通用 OAuth 插件 结合 line() 辅助函数。这允许你使用不同的 providerId 配置多个 LINE 提供者:

【To support multiple countries/channels, use the Generic OAuth plugin with the line() helper function. This allows you to configure multiple LINE providers with different providerIds:】

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

export const auth = betterAuth({
  plugins: [
    genericOAuth({
      config: [
        // Japan channel
        line({
          providerId: "line-jp",
          clientId: process.env.LINE_JP_CLIENT_ID,
          clientSecret: process.env.LINE_JP_CLIENT_SECRET,
        }),
        // Thailand channel
        line({
          providerId: "line-th",
          clientId: process.env.LINE_TH_CLIENT_ID,
          clientSecret: process.env.LINE_TH_CLIENT_SECRET,
        }),
        // Taiwan channel
        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")来确定使用哪个通道:

【When signing in, use the appropriate providerId (e.g., "line-jp", "line-th", "line-tw") to identify which channel to use:】

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

const authClient = createAuthClient({
  plugins: [genericOAuthClient()],
});

// Sign in with Japan channel
await authClient.signIn.social({ provider: "line-jp" });

// Sign in with Thailand channel
await authClient.signIn.social({ provider: "line-th" });

On this page