获取你的 Reddit 凭证
要使用 Reddit 登录,你需要一个客户端 ID 和客户端密钥。你可以从 Reddit 开发者门户 获取它们。
- 点击“创建应用”或“创建另一个应用”
- 选择“Web 应用”作为应用类型
- 将重定向 URL 设置为
http://localhost:3000/api/auth/callback/reddit用于本地开发 - 在生产环境中,将其设置为你应用的域名(例如
https://example.com/api/auth/callback/reddit) - 创建应用后,你将获得客户端ID(位于应用名称下方)和客户端密钥
如果你更改了认证路由的基础路径,请务必相应地更新重定向 URL。
配置提供程序
要配置提供程序,你需要导入该提供程序并将其传递给身份验证实例的 socialProviders 选项。
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。
import { createAuthClient } from "better-auth/client"
const authClient = createAuthClient()
const signIn = async () => {
const data = await authClient.signIn.social({
provider: "reddit"
})
}额外配置
【Additional Configuration】
范围
【Scopes】
默认情况下,Reddit 提供基本的用户信息。如果你需要额外的权限,可以在你的认证配置中指定范围:
【By default, Reddit provides basic user information. If you need additional permissions, you can specify scopes in your auth configuration:】
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"] // Add required scopes
},
},
})常见的 Reddit 权限包括:
identity:访问基本账户信息read:访问帖子和评论submit:提交帖子和评论subscribe:管理子板块订阅history:访问投票历史
【Common Reddit scopes include:
identity: Access basic account informationread: Access posts and commentssubmit: Submit posts and commentssubscribe: Manage subreddit subscriptionshistory: Access voting history】
有关可用权限范围的完整列表,请参阅 Reddit OAuth2 文档。
【For a complete list of available scopes, refer to the Reddit OAuth2 documentation.】