3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

UnityでFirebaseを利用したemail/passwordログインを実装する方法

Last updated at Posted at 2022-10-24

Firebaseを利用することで、Unityでも複数に認証プロバイダを簡単 に提供できると思って、トライしてみました。結論的にはemail/password認証だけなら簡単だけど、twitterなどの他の認証プロバイダを提供しようとすると、かなり難しいということがわかりました・・・。

ここではまずemail/passwordによる認証方法を実施する手順を紹介しています。
twitterでの認証は次回です。

前提

  • Unity 2021.3.0f1
    • Firebase sdk 9.6.0

手順

  1. Unityで認証するプロジェクトを作成します
    1. 今回利用するために新規作成で問題ありません
    2. ただ、iOSの場合バンドルID、Androidの場合パッケージ名などが必要なので、設定しておきます
  2. Firebase プロジェクトを作成します
  3. アプリを Firebase に登録します
  4. Firebase 構成ファイルを追加します
  5. FirebaseAuth.unitypackage をインストールします
    1. https://firebase.google.com/download/unity?hl=ja からfirebase_unity_sdk_9.6.0.zipをダウンロード(9.6.0はバージョンだと思うので、実施するタイミングで違うと思います)
    2. 解凍するとdotnet4とdotnet3とありますが、3は旧バージョン用に残してあるだけだと思うので、4のほう(firebase_unity_sdk/dotnet4/FirebaseAuth.unitypackage)を使います
  6. ユーザ登録用のプログラムを実行します
    1. 以下プログラムをコピペ
      using UnityEngine;
      using Firebase.Auth;
      
      public class NormalAuthentication : MonoBehaviour
      {
          void Start()
          {
              var email = "demo.user@normal.com";
              var password = "password";
              CreateUserWithEmailAndPassword(email, password);
              // SingIn(email, password);
          }
      
          private void CreateUserWithEmailAndPassword(string email, string password)
          {
              var auth = FirebaseAuth.DefaultInstance;
              auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
              {
                  if (task.IsCanceled)
                  {
                      Debug.LogWarning("SignInWithCredentialAsync was canceled.");
                      return;
                  }
      
                  if (task.IsFaulted)
                  {
                      Debug.LogWarning($"SignInWithCredentialAsync was fault. {task.Exception}");
                      return;
                  }
      
                  var newUser = task.Result;
                  Debug.Log($"Firebase上でメールアドレス認証によるユーザ作成に成功しました。 UserId is {newUser.UserId}");
              });
          }
      
          private void SingIn(string email, string password)
          {
              var auth = FirebaseAuth.DefaultInstance;
              auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
                  if (task.IsCanceled) {
                      Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
                      return;
                  }
                  if (task.IsFaulted) {
                      Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
                      return;
                  }
      
                  var newUser = task.Result;
                  Debug.Log($"サインインに成功しました。UserId is {newUser.UserId}");
              });
          }
      }
      
    2. Hierarchy上に空のGameObjectを作って、InspectorからNormalAuthenticationを追加します
    3. 実行してコンソールに Firebase上でメールアドレス認証によるユーザ作成に成功しました。UserId is zrTwK79hohXxxxxxSxxxxXXjIdh2 と表示されれば成功です。UserIdは架空のものです。
      エラーFirebaseCppApp-9_6_0.bundle”は、開発元を検証できないため開けません。が出る場合は、エラー対応を参照ください。
    4. Firebase を確認すると以下のようにユーザが登録されているはずです。AuthenticationFirebase_コンソール.png
  7. サインインのプログラムを実行します。
    1. 以下のような感じで修正。
      void Start()
      {
          var email = "demo.user@normal.com";
          var password = "password";
          // CreateUserWithEmailAndPassword(email, password);
          SingIn(email, password);
      }
      
    2. 実行してコンソールに サインインに成功しました。UserId is zrTwK79hohXxxxxxSxxxxXXjIdh2 と表示されれば成功です。

エラー対応

実行しようとするとbundleが利用できないエラー

エラー概要

Macで実行しようとすると、FirebaseCppApp-9_6_0.bundle”は、開発元を検証できないため開けません。エラーが発生しました。
スクリーンショット 2022-10-21 22.16.56.png

対応方法

「システム環境設定」→「セキュリティとプライバシー」 →「一般」を選択し、下部に出ている「このまま許可」ボタンを押してください。その上で実行すれば、正常に動作するはずです。

参考

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?