Notion
获取你的 Notion 凭据
要使用 Notion 作为社交提供者,你需要获取你的 Notion OAuth 凭据。你可以通过在 Notion 开发者门户 创建一个新的集成来获取。
在 Notion 集成设置 > OAuth 域名和 URI 中,确保在本地开发时将重定向 URL 设置为 http://localhost:3000/api/auth/callback/notion。对于生产环境,确保将重定向 URL 设置为你的应用域名,例如 https://example.com/api/auth/callback/notion。如果你更改了认证路由的基本路径,应相应更新重定向 URL。
确保你的 Notion 集成启用了适当的功能。对于用户身份验证,你需要启用“读取用户信息,包括电子邮件地址”的功能。
配置提供者
要配置提供者,你需要在身份验证配置中将 clientId 和 clientSecret 传递给 socialProviders.notion。
import { betterAuth } from "better-auth"
export const auth = betterAuth({
socialProviders: {
notion: {
clientId: process.env.NOTION_CLIENT_ID as string,
clientSecret: process.env.NOTION_CLIENT_SECRET as string,
},
},
})用法
【Usage】
使用 Notion 登录
【Sign In with Notion】
要使用 Notion 登录,你可以使用客户端提供的 signIn.social 函数。signIn 函数接收一个包含以下属性的对象:
provider:要使用的提供者。它应设置为notion。
【To sign in with Notion, you can use the signIn.social function provided by the client. The signIn function takes an object with the following properties:
provider: The provider to use. It should be set tonotion.】
import { createAuthClient } from "better-auth/client"
const authClient = createAuthClient()
const signIn = async () => {
const data = await authClient.signIn.social({
provider: "notion"
})
}Notion 集成类型
【Notion Integration Types】
Notion 支持不同类型的集成。在创建集成时,你可以选择以下类型:
【Notion supports different integration types. When creating your integration, you can choose between:】
- 公共集成:任何 Notion 工作区都可以安装
- 内部集成:仅限于你自己的工作区
对于大多数身份验证使用场景,你需要创建一个公共集成,以允许来自不同工作区的用户登录。
【For most authentication use cases, you'll want to create a public integration to allow users from different workspaces to sign in.】
请求额外的 Notion 权限
【Requesting Additional Notion Scopes】
如果你的应用在用户已经注册后需要额外的 Notion 功能,你可以使用相同的 Notion 提供者和额外权限范围,通过 linkSocial 方法来请求它们。
【If your application needs additional Notion capabilities after the user has already signed up, you can request them using the linkSocial method with the same Notion provider and additional scopes.】
const requestNotionAccess = async () => {
await authClient.linkSocial({
provider: "notion",
// Notion automatically provides access based on integration capabilities
});
};
// Example usage in a React component
return <button onClick={requestNotionAccess}>Connect Notion Workspace</button>;身份验证后,你可以使用访问令牌与 Notion API 交互,以读取和写入用户授权访问的页面、数据库及其他内容。