コンテンツへスキップ

Googleログインの設定

このガイドでは、Capgo Social LoginでGoogleログインを設定する方法を学びます。Googleログインを設定するには、次のものが必要です:

  • Googleアカウント

このパートでは、Googleが表示するログイン画面を設定します。

  1. console.cloud.google.comにアクセスしてください
  2. プロジェクトセレクターをクリックします Google Console Project Selector
  3. まだプロジェクトがない場合は、新しいプロジェクトを作成してください。
    1. New projectをクリックします New Project button in Google Console
    2. プロジェクトに名前を付けてCreateをクリックします Project naming screen showing name field and Create button
    3. 正しいプロジェクトにいることを確認します Project name showing in the selector indicating correct project selection
  4. OAuth consent screenの設定を開始します
    1. 検索バーをクリックします

      Google Console search bar
    2. OAuth consent screenを検索してクリックします

      Search results showing OAuth consent screen option
    3. 同意画面を設定します

      createをクリックします

      OAuth consent screen user type selection with External and Internal options
  5. アプリに関する情報を入力します
    1. App Informationから始めましょう

      App Information section showing App name and User support email fields
      • App Nameを入力してください
      • user support emailを入力します
      1. アプリのロゴを追加できます

        App logo upload section in OAuth consent screen
      2. App domainを設定する必要があります

        App domain configuration section with authorized domains field
      3. 開発者のメールを提供する必要があります

        Developer contact information section with email field
      4. save and continueをクリックします

        Save and Continue button at bottom of form
  6. スコープを設定します
    1. add or remove scopesをクリックします

      Add or remove scopes button in scopes configuration screen
    2. 次のスコープを選択してupdateをクリックします

      Scope selection dialog with email and profile scopes selected
    3. save and continueをクリックします

      Save and Continue button in scopes screen
  7. テストユーザーを追加します
    1. add usersをクリックします Add users button in test users section
    2. Googleメールを入力してenterを押し、addをクリックします Email input field and Add button for test users
    3. save and continueをクリックします Save and Continue button in test users screen
  8. back to dashboardをクリックします Back to dashboard button at bottom of completion page
  9. 検証用にアプリを提出します

オンラインアクセスとオフラインアクセスの違い

Section titled “オンラインアクセスとオフラインアクセスの違い”

CapacitorでGoogleログインを使用する方法は複数あります。次の表は、2つの違いをまとめたものです:

オンラインアクセスオフラインアクセス
バックエンドが必要
長期間有効なアクセストークン
簡単な設定

どちらを選択すべきかまだわからない場合は、次のシナリオを検討してください:

  1. ユーザーにログインしてもらい、すぐにカスタムJWTを発行します。アプリはGoogle APIを呼び出しません

    この場合は、オンラインアクセスを選択してください。

  2. アプリはクライアントからいくつかのGoogle APIを呼び出しますが、バックエンドからは呼び出しません

    この場合は、オンラインアクセスを選択してください

  3. アプリはバックエンドからいくつかのGoogle APIを呼び出しますが、ユーザーがアプリを積極的に使用している場合のみです

    この場合は、オンラインアクセスを選択してください

  4. アプリは、ユーザーがアプリを積極的に使用していない場合でも、ユーザーのカレンダーを定期的にチェックします

    この場合は、オフラインアクセスを選択してください

オンラインアクセスのバックエンド例

Section titled “オンラインアクセスのバックエンド例”

このチュートリアルのこのパートでは、バックエンドでユーザーを検証する方法を示します。

この例は非常にシンプルで、次の技術に基づいています:

この例のコードはこちらで見つけることができます

ご覧のとおり:

VS Code showing Google authentication code that verifies tokens

アイデアは非常にシンプルです。https://www.googleapis.com/oauth2/v3/tokeninfoに単純なGETリクエストを送信すると、トークンが有効かどうか、および有効な場合はユーザーのメールが返されます。また、ユーザートークンに関する他の情報も提供されます

Google OAuth Playground showing token information response with user details

そこから、独自のJWTをユーザーに発行したり、何らかのセッションCookieを発行したりできます。最終的な認証実装の可能性は無限です。

Google APIを呼び出したい場合は、GoogleのOAuth 2.0 Playgroundを確認することを強くお勧めします。そこから、呼び出すことができるAPIを簡単に確認できます。

独自のバックエンドでオフラインアクセスを使用する

Section titled “独自のバックエンドでオフラインアクセスを使用する”

オンラインアクセスを使用するには、次のものが必要です:

  • HTTPサーバー

この例では、次の技術を使用してアプリでオフラインアクセスを提供します:

この例のコードはこちらで見つけることができます

クライアントコードについては、次のようになります:

import { Capacitor } from '@capacitor/core';
import { GoogleLoginOfflineResponse, SocialLogin } from '@capgo/capacitor-social-login';
import { usePopoutStore } from '@/popoutStore'; // <-- アプリ固有
const baseURL = "[redacted]";
async function fullLogin() {
await SocialLogin.initialize({
google: {
webClientId: '[redacted]',
iOSClientId: '[redacted]',
iOSServerClientId: 'webClientIdと同じ値',
mode: 'offline' // <-- 重要
}
})
const response = await SocialLogin.login({
provider: 'google',
options: {
forceRefreshToken: true // <-- 重要
}
})
if (response.provider === 'google') {
const result = response.result as GoogleLoginOfflineResponse
const res = await fetch(`${baseURL}/auth/google_offline`, {
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
serverAuthCode: result.serverAuthCode,
platform: Capacitor.getPlatform()
}),
method: "POST"
})
if (res.status !== 200) {
popoutStore.popout("Full google login failed", "check console");
return
}
const { jwt } = await res.json();
const userinfo = await fetch(`${baseURL}/auth/get_google_user`, {
headers: {
Authorization: `Bearer ${jwt}`
}
})
if (userinfo.status !== 200) {
popoutStore.popout("Full google (userinfo) login failed", "check console");
return
}
popoutStore.popout("userinfo res", await userinfo.text());
}
}