メインコンテンツにジャンプ

より良い認証統合

GitHub

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

このページでは、最も適切な2つの統合パターンについて説明します。

  • 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 が所有する完全な OAuth リダイレクト フローを使用します。

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サインインの最も綺麗な統合パスです:

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',
});

iOS Limited Login: nonceを

  • アクセストークンフロー: アクセストークンを idToken
  • そして token and accessToken

Capgoで使用するには、以下のレスポンス形状から @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の汎用OAuthプロバイダー

セクション「Better Authの汎用OAuthプロバイダー」

Auth0、Okta、Keycloak、GitHub、Microsoft Entra ID、などのプロバイダーに対して、Better Authの汎用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 は、複数のプロバイダ用に事前設定されたヘルパーを提供しています。これらは、ソーシャルログイン プラグイン ドキュメントで見られる追加のプロバイダ例と最も近いものです。

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',
});
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には idTokengoogle.mode: 'offline' が必要です。

  2. したがって はこのハンドオフフローに適していません。

  3. Apple nonceを再利用します 一度生成し、Appleのネイティブログインに送信し、同じ値をBetter Authに送信します。

  4. Facebookをプラットフォームごとに異なるように扱います iOSのLimited LoginではIDトークンが提供されます。 他のフローではアクセストークンしか提供されません。

Generic OAuthフローを混ぜるのは理由がある場合のみです

関連情報

より良い認証統合から続けてください。

Better Auth Integrationから続ける

Capgoを使用している場合 より良い認証統合 認証とアカウントフローの計画に役立つため、接続してください。 Capgoで使用する@capgo/capacitor-social-login Capgoのネイティブ機能の使用における@capgo/capacitor-social-loginのために @capgo/capacitor-social-login Capgoの実装詳細の@capgo/capacitor-social-loginのために @capgo/capacitor-passkey Capgoの実装詳細の@capgo/capacitor-passkeyのために Capgoの実装詳細の@capgo/capacitor-native-biometricのために Capgoの実装詳細の@capgo/capacitor-native-biometric、 2要素認証 Capgoの実装詳細の2要素認証のために