「Unity/Firebase/Authentication」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→メールアドレス認証を使う場合) |
(→メールアドレス認証を使う場合) |
||
| 行62: | 行62: | ||
アカウントを作成してもメールは飛ばない、アカウントを作成した後、上の処理をすると既に同じアカウントがあるということで、IsFaulted側を通る。 | アカウントを作成してもメールは飛ばない、アカウントを作成した後、上の処理をすると既に同じアカウントがあるということで、IsFaulted側を通る。 | ||
| + | |||
| + | ===メール認証のログイン=== | ||
| + | <pre> | ||
| + | auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => { | ||
| + | // この中はUIが表示できない。 | ||
| + | if (task.IsCanceled) { | ||
| + | Debug.LogError("SignInWithEmailAndPasswordAsync was canceled."); | ||
| + | return; | ||
| + | } | ||
| + | if (task.IsFaulted) { | ||
| + | Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception); | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | Firebase.Auth.FirebaseUser newUser = task.Result; | ||
| + | Debug.LogFormat("User signed in successfully: {0} ({1})", | ||
| + | newUser.DisplayName, newUser.UserId); | ||
| + | }); | ||
| + | </pre> | ||
| + | |||
| + | ==ユーザ情報取得== | ||
| + | <pre> | ||
| + | Firebase.Auth.FirebaseUser user = auth.CurrentUser; | ||
| + | if (user != null) { | ||
| + | string name = user.DisplayName; | ||
| + | string email = user.Email; | ||
| + | System.Uri photo_url = user.PhotoUrl; | ||
| + | // The user's Id, unique to the Firebase project. | ||
| + | // Do NOT use this value to authenticate with your backend server, if you | ||
| + | // have one; use User.TokenAsync() instead. | ||
| + | string uid = user.UserId; | ||
| + | } | ||
| + | </pre> | ||
| + | 公式:https://firebase.google.com/docs/auth/unity/password-auth?hl=ja | ||
2019年6月22日 (土) 23:04時点における版
Firebase設定
unity/Firebase/基本 [ショートカット]
認証プラグインインストール
FirebaseAuth.unitypackageをAssets/Importからインストールする
https://firebase.google.com/download/unity?hl=ja
匿名認証を使う場合
- web側のfirebaseコンソールで匿名認証を有効にする(Authentication/ログイン方法/匿名/有効)
以下を実装
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task => {
if (task.IsCanceled)
{
Debug.LogError("SignInAnonymouslyAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInAnonymouslyAsync encountered an error: " + task.Exception); // 匿名が有効になって場合はこちらを通る。
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.Log("User signed in successfully");
Debug.Log("user=" + newUser.DisplayName); // 匿名では空だった
Debug.Log("user=" + newUser.UserId); // こちらはX3TUGaMPQPaN1rmGL2CxhQ4z4712と表示
});
再起動やキャッシュ削除してもUserIdは変わらないが、アプリデータ削除や再インストールではUserIdは変わる。
公式:https://firebase.google.com/docs/auth/unity/anonymous-auth?hl=ja
メールアドレス認証を使う場合
- web側のfirebaseコンソールでメール認証を有効にする(Authentication/ログイン方法/匿名/有効)
- "ユーザーがメールアドレスとパスワードを使用してログインできるようにします。"側だけon
以下アカウント作成処理
string email = "hogehoge@example.com"; string password = "hogehoge"; // 6文字以上でないとisFaulted側を通る auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
// この中はUIが表示できない。
if (task.IsCanceled)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
return;
}
// Firebase user has been created.
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("Firebase user created successfully: ({0}) ({1})",
newUser.DisplayName, newUser.UserId);
});
アカウントを作成してもメールは飛ばない、アカウントを作成した後、上の処理をすると既に同じアカウントがあるということで、IsFaulted側を通る。
メール認証のログイン
auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
// この中はUIが表示できない。
if (task.IsCanceled) {
Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted) {
Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
ユーザ情報取得
Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null) {
string name = user.DisplayName;
string email = user.Email;
System.Uri photo_url = user.PhotoUrl;
// The user's Id, unique to the Firebase project.
// Do NOT use this value to authenticate with your backend server, if you
// have one; use User.TokenAsync() instead.
string uid = user.UserId;
}
公式:https://firebase.google.com/docs/auth/unity/password-auth?hl=ja
