Notion
Get your Notion credentials
To use Notion as a social provider, you need to get your Notion OAuth credentials. You can get them by creating a new integration in the Notion Developers Portal.
In the Notion integration settings > OAuth Domain & URIs, make sure to set the redirect URL to http://localhost:3000/api/auth/callback/notion for local development. For production, make sure to set the redirect URL as your application domain, e.g. https://example.com/api/auth/callback/notion. If you change the base path of the auth routes, you should update the redirect URL accordingly.
Make sure your Notion integration has the appropriate capabilities enabled. For user authentication, you'll need the "Read user information including email addresses" capability.
Configure the provider
To configure the provider, you need to pass the client_id and client_secret to BetterAuth::SocialProviders.notion in your auth configuration.
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
notion: BetterAuth::SocialProviders.notion(
client_id: ENV.fetch("NOTION_CLIENT_ID"),
client_secret: ENV.fetch("NOTION_CLIENT_SECRET")
)
}
)Usage
Sign In with Notion
To sign in with Notion, call auth.api.sign_in_social on your Ruby auth instance. The endpoint body takes the following properties:
provider: The provider to use. It should be set tonotion.
response = auth.api.sign_in_social(
body: {
provider: "notion",
callback_url: "/dashboard",
error_callback_url: "/login",
disable_redirect: true
}
)
redirect_url = response.fetch(:url)Notion Integration Types
Notion supports different integration types. When creating your integration, you can choose between:
- Public integrations: Can be installed by any Notion workspace
- Internal integrations: Limited to your own workspace
For most authentication use cases, you'll want to create a public integration to allow users from different workspaces to sign in.
Requesting Additional Notion Scopes
If your application needs additional Notion capabilities after the user has already signed up, you can request them using the auth.api.link_social method with the same Notion provider and additional scopes.
response = auth.api.link_social(
headers: {
"cookie" => request.env.fetch("HTTP_COOKIE", "")
},
body: {
provider: "notion",
callback_url: "/dashboard"
}
)
redirect_url = response.fetch(:url)After authentication, you can use the access token to interact with the Notion API to read and write pages, databases, and other content that the user has granted access to.