「Unity/Firebase/Authentication」の版間の差分
(→匿名認証を使う場合) |
|||
| (同じ利用者による、間の52版が非表示) | |||
| 行10: | 行10: | ||
#web側のfirebaseコンソールで匿名認証を有効にする(Authentication/ログイン方法/匿名/有効) | #web側のfirebaseコンソールで匿名認証を有効にする(Authentication/ログイン方法/匿名/有効) | ||
| − | + | ===アカウント作成処理=== | |
<pre> | <pre> | ||
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; | Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; | ||
auth.SignInAnonymouslyAsync().ContinueWith(task => { | auth.SignInAnonymouslyAsync().ContinueWith(task => { | ||
| + | // この中はUIスレッドで通る | ||
| + | // 同じアカウントが既にあってもこの中は通る。 | ||
if (task.IsCanceled) | if (task.IsCanceled) | ||
{ | { | ||
| 行24: | 行26: | ||
return; | return; | ||
} | } | ||
| − | Firebase.Auth.FirebaseUser newUser = task.Result; | + | Firebase.Auth.FirebaseUser newUser = task.Result.User; |
| + | Debug.Log("User signed in successfully"); | ||
| + | Debug.Log("userName=" + newUser.DisplayName); // 匿名では空だった | ||
| + | Debug.Log("userId=" + newUser.UserId); // こちらはX3TUGaMPQPaN1rmGL2CxhQ4z4712と表示 | ||
| + | }, TaskScheduler.FromCurrentSynchronizationContext()); | ||
| + | </pre> | ||
| + | |||
| + | #上の情報は作成処理をするたびに、UserIdは変わる。 | ||
| + | |||
| + | 公式:https://firebase.google.com/docs/auth/unity/anonymous-auth?hl=ja | ||
| + | |||
| + | ===ユーザ情報取得=== | ||
| + | <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> | ||
| + | #ユーザ情報は、再起動やキャッシュ削除してもUserIdは変わらないが、アプリデータ削除や再インストールではUserIdは変わる。 | ||
| + | |||
| + | ==メールアドレス認証を使う場合== | ||
| + | #web側のfirebaseコンソールでメール認証を有効にする(Authentication/ログイン方法/匿名/有効) | ||
| + | #"ユーザーがメールアドレスとパスワードを使用してログインできるようにします。"側だけon | ||
| + | |||
| + | ===アカウント作成処理=== | ||
| + | <pre> | ||
| + | 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.User; | ||
| + | Debug.LogFormat("Firebase user created successfully: ({0}) ({1})", | ||
| + | newUser.DisplayName, newUser.UserId); | ||
| + | }, TaskScheduler.FromCurrentSynchronizationContext()); | ||
| + | </pre> | ||
| + | アカウントを作成してもメールは飛ばない、アカウントを作成した後、上の処理をすると既に同じアカウントがあるということで、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.User; | ||
| + | Debug.LogFormat("User signed in successfully: {0} ({1})", | ||
| + | newUser.DisplayName, newUser.UserId); | ||
| + | }, TaskScheduler.FromCurrentSynchronizationContext()); | ||
| + | </pre> | ||
| + | |||
| + | ===メール認証のパスワード変更=== | ||
| + | firebaseコンソール側から管理者がパスワード変更用のurlをメールすることができる。 | ||
| + | |||
| + | ===ユーザ情報取得=== | ||
| + | <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 | ||
| + | |||
| + | ==匿名アカウントからメール認証アカウントに移行する== | ||
| + | 上記の匿名アカウント作成の後にメール認証アカウントを作成してしまうと、 | ||
| + | メールアカウント作成時に別のuserIdが作られてしまうので、 | ||
| + | 以下のようにAuthCredentialを使ってつなぎこむ必要がある。 | ||
| + | |||
| + | 公式:https://firebase.google.com/docs/auth/unity/account-linking?hl=ja | ||
| + | |||
| + | 以下AuthCredentialの使い方 | ||
| + | |||
| + | ===まずは匿名アカウントを作る=== | ||
| + | <pre> | ||
| + | Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; | ||
| + | auth.SignInAnonymouslyAsync().ContinueWith(task => { | ||
| + | // この中はUIスレッドで通る | ||
| + | // 同じアカウントが既にあってもこの中は通る。 | ||
| + | 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.User; | ||
Debug.Log("User signed in successfully"); | Debug.Log("User signed in successfully"); | ||
Debug.Log("user=" + newUser.DisplayName); // 匿名では空だった | Debug.Log("user=" + newUser.DisplayName); // 匿名では空だった | ||
Debug.Log("user=" + newUser.UserId); // こちらはX3TUGaMPQPaN1rmGL2CxhQ4z4712と表示 | Debug.Log("user=" + newUser.UserId); // こちらはX3TUGaMPQPaN1rmGL2CxhQ4z4712と表示 | ||
| + | }, TaskScheduler.FromCurrentSynchronizationContext()); | ||
| + | </pre> | ||
| + | |||
| + | ===メール認証アカウントを匿名アカウントにつなぎこむ=== | ||
| + | 上記処理の後に、以下処理をする(この処理の場合、メアドは未登録である必要がある) | ||
| + | <pre> | ||
| + | Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; | ||
| + | string email = "hogehoge@example.com"; | ||
| + | string password = "hogehoge"; // 6文字以上でないとisFaulted側を通る | ||
| + | Firebase.Auth.Credential credential = Firebase.Auth.EmailAuthProvider.GetCredential(email, password); | ||
| + | Firebase.Auth.FirebaseUser user = auth.CurrentUser; | ||
| + | if (user == null) { | ||
| + | Debug.LogError("no user."); | ||
| + | return; | ||
| + | } | ||
| + | user.LinkWithCredentialAsync(credential).ContinueWith(task => { | ||
| + | if (task.IsCanceled) { | ||
| + | Debug.LogError("LinkWithCredentialAsync was canceled."); | ||
| + | return; | ||
| + | } | ||
| + | if (task.IsFaulted) { | ||
| + | Debug.LogError("LinkWithCredentialAsync encountered an error: " + task.Exception); // 既にemailが登録されてる時はここを通る | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | Firebase.Auth.FirebaseUser newUser = task.Result.User; | ||
| + | Debug.LogFormat("Credentials successfully linked to Firebase user: {0} ({1})", | ||
| + | newUser.DisplayName, newUser.UserId); | ||
| + | }, TaskScheduler.FromCurrentSynchronizationContext()); | ||
| + | </pre> | ||
| + | |||
| + | ==ログアウト== | ||
| + | Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; | ||
| + | auth.SignOut(); | ||
| + | |||
| + | ==戻りコールバック内でUIThreadを処理する== | ||
| + | <pre> | ||
| + | auth.SignInAnonymouslyAsync().ContinueWith(task => | ||
| + | { | ||
| + | // callback処理 | ||
}); | }); | ||
</pre> | </pre> | ||
| − | + | 以下のように第2引数にFromCurrentSynchronizationContextを追加する | |
| + | <pre> | ||
| + | using System.Threading.Tasks; | ||
| + | auth.SignInAnonymouslyAsync().ContinueWith(task => | ||
| + | { | ||
| + | // callback処理 | ||
| + | }, TaskScheduler.FromCurrentSynchronizationContext()); | ||
| + | </pre> | ||
| − | 公式:https://firebase.google.com/docs/auth/unity/ | + | ==ユーザーのメールアドレス設定について== |
| + | 公式:https://firebase.google.com/docs/auth/unity/manage-users?hl=ja | ||
| + | |||
| + | UpdateEmailAsyncで処理できる。 | ||
| + | |||
| + | ==="RequiresRecentLogin = 9"のExceptionが出る場合=== | ||
| + | ユーザーのメールアドレスを設定の際に、再認証をしばらくやってない場合は、再認証(ReauthenticateAsync)をする必要がある | ||
| + | |||
| + | 公式ユーザー再認証:https://firebase.google.com/docs/auth/unity/manage-users?hl=ja#re-authenticate_a_user | ||
| + | |||
| + | ==="OperationNotAllowed = 7"のExceptionが出る場合=== | ||
| + | UpdateEmailAsync の Exceptionエラーメッセージ | ||
| + | One or more errors occurred. (This operation is not allowed. You must enable this service in the console.) | ||
| + | |||
| + | 解決策:Firebaseのauth設定の“メール列挙保護“をonからoffへ変更すればよい。 | ||
| + | |||
| + | ==メアド更新== | ||
| + | user.SendEmailVerificationBeforeUpdatingEmailAsync(email).ContinueWith(task => | ||
| + | |||
| + | https://firebase.google.com/docs/reference/unity/class/firebase/auth/firebase-user | ||
| + | |||
| + | firebaseのauthenticationのidのメアドは変わらないが、多分内部的には、メアドは変わってる。 | ||
| + | |||
| + | ==PlayGame認証を設定する== | ||
| + | [[Unity/GooglePlayGames]] [ショートカット] | ||
| + | |||
| + | 参考:https://firebase.google.com/docs/auth/unity/play-games?hl=ja | ||
| + | |||
| + | ==AppleId認証を設定する== | ||
| + | [[Unity/AppleAuth]] [ショートカット] | ||
2025年8月13日 (水) 18:25時点における最新版
目次
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 => {
// この中はUIスレッドで通る
// 同じアカウントが既にあってもこの中は通る。
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.User;
Debug.Log("User signed in successfully");
Debug.Log("userName=" + newUser.DisplayName); // 匿名では空だった
Debug.Log("userId=" + newUser.UserId); // こちらはX3TUGaMPQPaN1rmGL2CxhQ4z4712と表示
}, TaskScheduler.FromCurrentSynchronizationContext());
- 上の情報は作成処理をするたびに、UserIdは変わる。
公式:https://firebase.google.com/docs/auth/unity/anonymous-auth?hl=ja
ユーザ情報取得
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;
}
- ユーザ情報は、再起動やキャッシュ削除してもUserIdは変わらないが、アプリデータ削除や再インストールではUserIdは変わる。
メールアドレス認証を使う場合
- 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.User;
Debug.LogFormat("Firebase user created successfully: ({0}) ({1})",
newUser.DisplayName, newUser.UserId);
}, TaskScheduler.FromCurrentSynchronizationContext());
アカウントを作成してもメールは飛ばない、アカウントを作成した後、上の処理をすると既に同じアカウントがあるということで、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.User;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
}, TaskScheduler.FromCurrentSynchronizationContext());
メール認証のパスワード変更
firebaseコンソール側から管理者がパスワード変更用のurlをメールすることができる。
ユーザ情報取得
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
匿名アカウントからメール認証アカウントに移行する
上記の匿名アカウント作成の後にメール認証アカウントを作成してしまうと、 メールアカウント作成時に別のuserIdが作られてしまうので、 以下のようにAuthCredentialを使ってつなぎこむ必要がある。
公式:https://firebase.google.com/docs/auth/unity/account-linking?hl=ja
以下AuthCredentialの使い方
まずは匿名アカウントを作る
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task => {
// この中はUIスレッドで通る
// 同じアカウントが既にあってもこの中は通る。
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.User;
Debug.Log("User signed in successfully");
Debug.Log("user=" + newUser.DisplayName); // 匿名では空だった
Debug.Log("user=" + newUser.UserId); // こちらはX3TUGaMPQPaN1rmGL2CxhQ4z4712と表示
}, TaskScheduler.FromCurrentSynchronizationContext());
メール認証アカウントを匿名アカウントにつなぎこむ
上記処理の後に、以下処理をする(この処理の場合、メアドは未登録である必要がある)
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
string email = "hogehoge@example.com";
string password = "hogehoge"; // 6文字以上でないとisFaulted側を通る
Firebase.Auth.Credential credential = Firebase.Auth.EmailAuthProvider.GetCredential(email, password);
Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user == null) {
Debug.LogError("no user.");
return;
}
user.LinkWithCredentialAsync(credential).ContinueWith(task => {
if (task.IsCanceled) {
Debug.LogError("LinkWithCredentialAsync was canceled.");
return;
}
if (task.IsFaulted) {
Debug.LogError("LinkWithCredentialAsync encountered an error: " + task.Exception); // 既にemailが登録されてる時はここを通る
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result.User;
Debug.LogFormat("Credentials successfully linked to Firebase user: {0} ({1})",
newUser.DisplayName, newUser.UserId);
}, TaskScheduler.FromCurrentSynchronizationContext());
ログアウト
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; auth.SignOut();
戻りコールバック内でUIThreadを処理する
auth.SignInAnonymouslyAsync().ContinueWith(task =>
{
// callback処理
});
以下のように第2引数にFromCurrentSynchronizationContextを追加する
using System.Threading.Tasks;
auth.SignInAnonymouslyAsync().ContinueWith(task =>
{
// callback処理
}, TaskScheduler.FromCurrentSynchronizationContext());
ユーザーのメールアドレス設定について
公式:https://firebase.google.com/docs/auth/unity/manage-users?hl=ja
UpdateEmailAsyncで処理できる。
"RequiresRecentLogin = 9"のExceptionが出る場合
ユーザーのメールアドレスを設定の際に、再認証をしばらくやってない場合は、再認証(ReauthenticateAsync)をする必要がある
公式ユーザー再認証:https://firebase.google.com/docs/auth/unity/manage-users?hl=ja#re-authenticate_a_user
"OperationNotAllowed = 7"のExceptionが出る場合
UpdateEmailAsync の Exceptionエラーメッセージ
One or more errors occurred. (This operation is not allowed. You must enable this service in the console.)
解決策:Firebaseのauth設定の“メール列挙保護“をonからoffへ変更すればよい。
メアド更新
user.SendEmailVerificationBeforeUpdatingEmailAsync(email).ContinueWith(task =>
https://firebase.google.com/docs/reference/unity/class/firebase/auth/firebase-user
firebaseのauthenticationのidのメアドは変わらないが、多分内部的には、メアドは変わってる。
PlayGame認証を設定する
Unity/GooglePlayGames [ショートカット]
参考:https://firebase.google.com/docs/auth/unity/play-games?hl=ja
AppleId認証を設定する
Unity/AppleAuth [ショートカット]
