コンテンツにスキップ

Better Auth Integration

GitHub

Better Authは、デバイス上でネイティブのサインインを実現したい場合でも、バックエンドでセッションを管理したい場合に適しています。 @capgo/capacitor-social-login このページでは、最も適切な統合パターン2つについて説明します。

このページは、Better Authを使用してデバイス上でネイティブのサインインを実現したい場合でも、バックエンドでセッションを管理したい場合に適しています。

  • Google、Apple、Facebookのネイティブトークンハンドオフ
  • Auth0、Okta、Keycloak、カスタムOIDCサーバーなどの提供者向けのBetter Authの汎用OAuth

ネイティブトークンハンドオフを使用する

「ネイティブトークンハンドオフを使用する」のセクション

最初に、返されたトークンをBetter Authに送信する SocialLogin.login() 使用する場合 authClient.signIn.social() Google

  • Apple
  • Facebook
  • __CAPGO_KEEP_0__

Better Auth の一般 OAuth を使用

「Better Auth の一般 OAuth を使用」

Better Auth が OAuth の完全なリダイレクトフローを所有するように、次の場合に使用してください:

  • Auth0
  • Okta
  • Keycloak
  • GitHub
  • OneLogin
  • 任意のカスタム OAuth2 または OIDC プロバイダー

セッションの交換を Better Auth の側に保ち、システム間でリダイレクトロジックを重複するのを避けるために、

Better Auth サーバーの設定

「Better Auth サーバーの設定」

Better Authを設定するには、サポートするソーシャル プロバイダーを選択してください:

import { betterAuth } from 'better-auth';
export const auth = betterAuth({
baseURL: process.env.BETTER_AUTH_URL,
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
},
apple: {
clientId: process.env.APPLE_CLIENT_ID as string,
clientSecret: process.env.APPLE_CLIENT_SECRET as string,
appBundleIdentifier: process.env.APPLE_APP_BUNDLE_IDENTIFIER as string,
},
facebook: {
clientId: process.env.FACEBOOK_CLIENT_ID as string,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET as string,
},
},
trustedOrigins: ['https://appleid.apple.com'],
});

Better Authクライアントの設定

クリップボードにコピー
import { createAuthClient } from 'better-auth/client';
export const authClient = createAuthClient({
baseURL: 'https://auth.example.com',
});

Googleの例

Googleの例

__CAPGO_KEEP_0__

このページは、ネイティブモバイルのGoogleサインインの最も簡潔な統合パスです。

import { SocialLogin } from '@capgo/capacitor-social-login';
import { authClient } from '@/lib/auth-client';
const googleResult = await SocialLogin.login({
provider: 'google',
options: {
scopes: ['profile', 'email'],
},
});
if (googleResult.result.responseType !== 'online' || !googleResult.result.idToken) {
throw new Error('Google online mode with idToken is required for Better Auth.');
}
await authClient.signIn.social({
provider: 'google',
idToken: {
token: googleResult.result.idToken,
accessToken: googleResult.result.accessToken?.token,
},
callbackURL: '/dashboard',
});

Appleの場合、ネイティブログインリクエストとBetter Authに同じnonceを渡します。

import { SocialLogin } from '@capgo/capacitor-social-login';
import { authClient } from '@/lib/auth-client';
const nonce = crypto.randomUUID();
const appleResult = await SocialLogin.login({
provider: 'apple',
options: {
scopes: ['email', 'name'],
nonce,
},
});
if (!appleResult.result.idToken) {
throw new Error('Apple idToken is required for Better Auth.');
}
await authClient.signIn.social({
provider: 'apple',
idToken: {
token: appleResult.result.idToken,
nonce,
accessToken: appleResult.result.accessToken?.token,
},
callbackURL: '/dashboard',
});

Better AuthはFacebookのハンドオフモードを2つドキュメントしています。

  • iOS Limited Login: パス idToken
  • アクセストークンフロー: アクセストークンを両方 token そして accessToken

CapgoのGeneric OAuthプロバイダーとBetter Authを使用します。 @capgo/capacitor-social-login:

import { SocialLogin } from '@capgo/capacitor-social-login';
import { authClient } from '@/lib/auth-client';
const facebookResult = await SocialLogin.login({
provider: 'facebook',
options: {
permissions: ['email', 'public_profile'],
},
});
const betterAuthToken = facebookResult.result.idToken
? {
token: facebookResult.result.idToken,
}
: facebookResult.result.accessToken?.token
? {
token: facebookResult.result.accessToken.token,
accessToken: facebookResult.result.accessToken.token,
}
: null;
if (!betterAuthToken) {
throw new Error('Facebook idToken or access token is required for Better Auth.');
}
await authClient.signIn.social({
provider: 'facebook',
idToken: betterAuthToken,
callbackURL: '/dashboard',
});

Auth0、Okta、Keycloak、GitHub、Microsoft Entra ID、などのプロバイダーに対しては、Better AuthのGeneric OAuthプラグインが、トークンをパスするのではなく、通常はBetter AuthのGeneric OAuthプラグインが適切な選択です。 SocialLogin.login({ provider: 'oauth2' }).

import { betterAuth } from 'better-auth';
import { genericOAuth } from 'better-auth/plugins';
export const auth = betterAuth({
plugins: [
genericOAuth({
config: [
{
providerId: 'keycloak',
discoveryUrl: 'https://sso.example.com/realms/mobile/.well-known/openid-configuration',
clientId: process.env.KEYCLOAK_CLIENT_ID as string,
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET as string,
},
],
}),
],
});
import { createAuthClient } from 'better-auth/client';
import { genericOAuthClient } from 'better-auth/client/plugins';
export const authClient = createAuthClient({
baseURL: 'https://auth.example.com',
plugins: [genericOAuthClient()],
});
await authClient.signIn.oauth2({
providerId: 'keycloak',
callbackURL: '/dashboard',
});

Better Auth Generic OAuthのプロバイダー例

「Better Authのプロバイダ例:Generic OAuth」

Better Authは、複数のプロバイダ用に事前設定されたヘルパーを搭載しています。これらは、ソーシャルログインプラグインドキュメントで見られる追加プロバイダ例と最も近いものです。

import { betterAuth } from 'better-auth';
import { auth0, genericOAuth } from 'better-auth/plugins';
export const auth = betterAuth({
plugins: [
genericOAuth({
config: [
auth0({
providerId: 'auth0',
domain: 'dev-example.eu.auth0.com',
clientId: process.env.AUTH0_CLIENT_ID as string,
clientSecret: process.env.AUTH0_CLIENT_SECRET as string,
scopes: ['openid', 'profile', 'email', 'offline_access'],
}),
],
}),
],
});
await authClient.signIn.oauth2({
providerId: 'auth0',
callbackURL: '/dashboard',
});

Microsoft Entra ID

「Microsoft Entra ID」
import { betterAuth } from 'better-auth';
import { genericOAuth, microsoftEntraId } from 'better-auth/plugins';
export const auth = betterAuth({
plugins: [
genericOAuth({
config: [
microsoftEntraId({
providerId: 'entra',
tenantId: 'common',
clientId: process.env.AZURE_CLIENT_ID as string,
clientSecret: process.env.AZURE_CLIENT_SECRET as string,
scopes: ['openid', 'profile', 'email', 'User.Read'],
}),
],
}),
],
});
await authClient.signIn.oauth2({
providerId: 'entra',
callbackURL: '/dashboard',
});
import { betterAuth } from 'better-auth';
import { genericOAuth, okta } from 'better-auth/plugins';
export const auth = betterAuth({
plugins: [
genericOAuth({
config: [
okta({
providerId: 'okta',
issuer: 'https://dev-12345.okta.com/oauth2/default',
clientId: process.env.OKTA_CLIENT_ID as string,
clientSecret: process.env.OKTA_CLIENT_SECRET as string,
scopes: ['openid', 'profile', 'email', 'offline_access'],
}),
],
}),
],
});
await authClient.signIn.oauth2({
providerId: 'okta',
callbackURL: '/dashboard',
});
import { betterAuth } from 'better-auth';
import { genericOAuth, keycloak } from 'better-auth/plugins';
export const auth = betterAuth({
plugins: [
genericOAuth({
config: [
keycloak({
providerId: 'keycloak',
issuer: 'https://sso.example.com/realms/mobile',
clientId: process.env.KEYCLOAK_CLIENT_ID as string,
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET as string,
scopes: ['openid', 'profile', 'email', 'offline_access'],
}),
],
}),
],
});
await authClient.signIn.oauth2({
providerId: 'keycloak',
callbackURL: '/dashboard',
});

GitHubの手動のGeneric OAuth設定

GitHubの手動のGeneric OAuth設定セクション

GitHubにはBetter Auth ヘルパーがGeneric OAuth ページにないので、手動の設定を使用します。

import { betterAuth } from 'better-auth';
import { genericOAuth } from 'better-auth/plugins';
export const auth = betterAuth({
plugins: [
genericOAuth({
config: [
{
providerId: 'github',
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
authorizationUrl: 'https://github.com/login/oauth/authorize',
tokenUrl: 'https://github.com/login/oauth/access_token',
userInfoUrl: 'https://api.github.com/user',
scopes: ['read:user', 'user:email'],
pkce: true,
},
],
}),
],
});
await authClient.signIn.oauth2({
providerId: 'github',
callbackURL: '/dashboard',
});

注意と注意点

注意と注意事項
  1. Google オンラインモードを使用 Better Authには idTokenが必要です google.mode: 'offline' なので

  2. はこのハンドオフフローに適していません。 Apple の nonce を再利用

  3. 一度生成し、Apple のネイティブログインに送信し、同じ値を Better Auth に送信します。 Facebook をプラットフォームごとに異なるように扱います

  4. iOS の Limited Login では ID トークンが提供されますが、他のフローではアクセストークンしか提供されません。 Generic OAuth フローを混ぜるのは避けるべきです。理由がある場合を除きます。

Better Auth が OAuth プロバイダーの構成を所有している場合、リダイレクトフローも Better Auth が所有するようにしてください。

「続けてください」セクション

Better Auth インテグレーションから続けてください

「Better Auth インテグレーションから続けてください」セクション

Better Auth インテグレーションを使用している場合 Better Auth インテグレーション 認証とアカウントフローの計画に使用し、@__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-social-loginと接続する Using @capgo/capacitor-social-login @capgo/capacitor-social-login @capgo/capacitor-social-login @capgo/capacitor-social-login @capgo/capacitor-passkey @capgo/capacitor-passkey @capgo/capacitor-native-biometric @capgo/capacitor-native-biometric、 Two-factor authentication Two-factor authentication