Reddit

获取 Reddit 凭证

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

  1. 点击"Create App"或"Create Another App"
  2. 选择"web app"作为应用程序类型
  3. 对于本地开发,将重定向 URL 设置为 http://localhost:3000/api/auth/callback/reddit
  4. 对于生产环境,将其设置为你的应用程序域名(例如 https://example.com/api/auth/callback/reddit
  5. 创建应用后,你将获得客户端 ID(在应用名称下方)和客户端密钥

如果你更改了认证路由的基础路径,请确保相应地更新重定向 URL。

配置提供商

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

auth.ts
import { betterAuth } from "better-auth"
 
export const auth = betterAuth({
    socialProviders: {
        reddit: {
            clientId: process.env.REDDIT_CLIENT_ID as string,
            clientSecret: process.env.REDDIT_CLIENT_SECRET as string,
        },
    },
})

使用 Reddit 登录

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

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

额外配置

作用域

默认情况下,Reddit 提供基本的用户信息。如果你需要额外的权限,你可以在 auth 配置中指定作用域:

auth.ts
export const auth = betterAuth({
    socialProviders: {
        reddit: {
            clientId: process.env.REDDIT_CLIENT_ID as string,
            clientSecret: process.env.REDDIT_CLIENT_SECRET as string,
            duration: "permanent",
            scope: ["read", "submit"] // 添加所需的作用域
        },
    },
})

常见的 Reddit 作用域包括:

  • identity:访问基本账户信息
  • read:访问帖子和评论
  • submit:提交帖子和评论
  • subscribe:管理 subreddit 订阅
  • history:访问投票历史

有关可用作用域的完整列表,请参考 Reddit OAuth2 文档

On this page