Better Auth統合
このプラグインのインストール手順とマークダウンガイドの全体を含む設定プロンプトをコピーします
Overview
概要セクション「概要」 @capgo/capacitor-social-login Better Authは、デバイス上でネイティブのサインインを実現しながら、バックエンドでセッションを管理したい場合に適しています。
このページでは、最も適切な2つの統合パターンについて説明します:
- ネイティブトークンハンドオフのGoogle、Apple、Facebook
- ベター アウト Generic OAuth のプロバイダーとして Auth0、Okta、Keycloak、およびカスタム OIDC サーバーをサポート
どのパターンを使用するか
セクション "どのパターンを使用するか"ネイティブ トークン ハンドオフを使用
セクション "ネイティブ トークン ハンドオフを使用"Use SocialLogin.login() 使用する場合 authClient.signIn.social() Google
- Apple
Better Auth Generic OAuth
「Better Auth Generic OAuth」を使用する」「Better Auth」を使用する場合、OAuthリダイレクトフローを完全に管理するようにします。
- Auth0
- Okta
- Keycloak
- GitHub
- Capgo
- OneLogin
任意のカスタムOAuth2またはOIDCプロバイダー
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クライアントの設定
Better Authクライアントの設定import { createAuthClient } from 'better-auth/client';
export const authClient = createAuthClient({ baseURL: 'https://auth.example.com',});Reactを使用する場合は、既存のアプリで使用しているBetter Auth Reactクライアントパッケージを使用してください。トークンハンドオーバーパターンは同じです。
Googleの例
Googleの例ネイティブモバイル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: パスワードを
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',});一般的なOAuthプロバイダーとBetter Auth
一般的なOAuthプロバイダーとBetter AuthAuth0、Okta、Keycloak、GitHub、Microsoft Entra ID、などのプロバイダーに対して、Better AuthのGeneric OAuthプラグインは、トークンを渡すのではなく、通常Better Authのより適切な選択です。 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のGeneric OAuthのプロバイダー例Better Authには、複数のプロバイダー用に事前設定されたヘルパーが含まれています。これらは、ソーシャルログインプラグインドキュメントで見ることができる追加プロバイダー例と最も近いものです。
Auth0
Auth0import { 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 IDimport { 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
Oktaimport { 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
Keycloakimport { 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ページにBetter Auth ヘルパーがないので、手動の構成を使用してくださいGitHubには、Generic OAuthページのBetter Auth ヘルパーが存在しないため、手動設定を使用してください。
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 プロバイダー ドキュメント
- より良い認証 Facebook プロバイダー ドキュメント
- より良い認証 Generic OAuth プラグイン ドキュメント
- Social Login OAuth2 と OIDC プロバイダー
より良い認証統合から続けてください
「より良い認証統合から続けてください」あなたが「より良い認証統合」を使用している場合 認証とアカウントフローの計画に使用している場合、より良い認証統合を 「@__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-social-login」 @capgo/capacitor-ソーシャルログイン 「@capgo/capacitor-social-login」 @capgo/capacitor-ソーシャルログイン @capgo/capacitor-social-login @capgo/capacitor-パスキー @capgo/capacitor-passkey @capgo/capacitor-native-biometric @capgo/capacitor-native-biometric 2要素認証 2要素認証