より良い認証統合
このプラグインのセットアップ手順とフルマークダウンガイドをコピーします。
Better Authは、デバイス上でネイティブのサインインを実現したい場合でも、バックエンドでセッションを管理したい場合に適しています。 @capgo/capacitor-social-login このページでは、最も適切な2つの統合パターンについて説明します。
このページは、Better Authを使用してデバイス上でネイティブのサインインを実現したい場合でも、バックエンドでセッションを管理したい場合に適しています。
- Native token handoff for Google, Apple, and Facebook
- Better Auth Generic OAuth for providers like Auth0, Okta, Keycloak, and custom OIDC servers
どのパターンを使用するか
Section titled “どのパターンを使用するか”Native token handoffを使用する
Section titled “Native token handoffを使用する”使用する SocialLogin.login() 最初に、返されたトークンをBetter Authに送信する authClient.signIn.social() when you use:
- Apple
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の例
Better Authクライアントの設定ネイティブモバイル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の例
「Appleの例」のセクション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',});Facebookの例
「Facebookの例」のセクションBetter AuthはFacebookのハンドオフモードを2つドキュメントしています:
- iOS Limited Login: pass the
idToken - アクセストークンフロー: アクセストークンを両方の
tokenそしてaccessToken
この機能は、次のレスポンス形状から動作します。 @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',});Better AuthのGeneric OAuthプロバイダー
Better AuthのGeneric OAuthプロバイダーのセクションAuth0、Okta、Keycloak、GitHub、Microsoft Entra ID、などのプロバイダーに対しては、Better AuthのGeneric OAuthプラグインが、トークンをパスするのではなく、通常はより適切な選択となります。 SocialLogin.login({ provider: 'oauth2' }).
Better Authサーバー
Better Authサーバーのセクション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, }, ], }), ],});Better Authクライアント
Better Authクライアントのセクション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 のプロバイダ例」Better Auth は、複数のプロバイダ用に事前設定されたヘルパーを提供しています。これらは、ソーシャルログイン プラグイン ドキュメントで見られる追加のプロバイダ例に最も近いものです。
Auth0
「Auth0」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',});Okta
「Okta」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',});Keycloak
Keycloakセクション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の手動で一般的なOAuth構成
GitHubの手動で一般的なOAuth構成セクションGitHubにはBetter Auth ヘルパーが一般的な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',});注意と注意事項
「注意事項と注意」セクション-
Google オンラインモードを使用 Better Authには
idTokenが必要ですのでgoogle.mode: 'offline'はこのハンドオフフローに適していません。 -
Apple の nonce を再利用 一度生成し、Apple のネイティブログインに送信し、同じ値を Better Auth に送信します。
-
Facebook をプラットフォームごとに異なるように扱います iOS の Limited Login では ID トークンが提供されますが、他のフローではアクセストークンしか提供されません。
-
Generic OAuth フローを混ぜるのは、理由がある場合のみ Better Auth が OAuth プロバイダー構成を所有している場合、Better Auth がリダイレクトフローも所有するようにしてください。
関連する記事を参照
関連情報- Google プロバイダーのドキュメント
- Apple プロバイダー ドキュメント
- Better Auth Facebook プロバイダー ドキュメント
- より良い認証プラグインのドキュメント
- ソーシャルログイン OAuth2 および OIDC プロバイダー
より良い認証統合から続けてください。
Better Auth Integrationから続くCapacitor より良い認証統合 認証とアカウントフローの計画に役立つため、接続してください。 Using @capgo/capacitor-social-login native機能の使用に@capgo/capacitor-social-loginを使用します。 @capgo/capacitor-social-login native機能の実装詳細については@capgo/capacitor-social-loginを参照してください。 @capgo/capacitor-passkey 実装詳細については@capgo/capacitor-passkeyを参照してください。 @capgo/capacitor-native-biometric 実装詳細については@capgo/capacitor-native-biometricを参照してください。 native機能の実装詳細については@__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-native-biometric、 2要素認証