2017-11-24 28 views
1

与えられたuserIdが存在しない場合、ユーザを作成するfirebaseサーバ側(firebase関数)コードがあります。存在しないユーザの作成時にuid-already-existエラーが発生しました

通常正常に動作しますが、ほとんど失敗しません。

function createUserIfNotExist(userId, userName) { 
    admin.auth().getUser(userId).then(function (userRecord) { 
     return userRecord; 
    }).catch(function (error) { 
     return admin.auth().createUser({ 
      uid: userId, 
      displayName: userName, 
     }) 
    }) 
} 

指定したユーザIDが存在しない

は、admin.auth()。getUserメソッドは()だから、admin.auth()。のcreateUser()

{ code: 'auth/user-not-found', message: 'There is no user record corresponding to the provided identifier.' } 

をスローするcatch節で呼ばれています。しかし、次のエラーで失敗することがあります。

{ Error: The user with the provided uid already exists. 
    at FirebaseAuthError.Error (native) 
    at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) 
    at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:104:23) 
    at Function.FirebaseAuthError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:128:16) 
    at /user_code/node_modules/firebase-admin/lib/auth/auth-api-request.js:399:45 
    at process._tickDomainCallback (internal/process/next_tick.js:135:7) 
    errorInfo: 
    { code: 'auth/uid-already-exists', 
    message: 'The user with the provided uid already exists.' } }  

私のコードにFirebaseのバグや何か問題がありますか?

+0

UIDはかなりランダムなので、まだ存在しないユーザーのUIDを取得するにはどうすればよいですか?私。これらの値は 'createUserIfNotExist'を呼び出すときにどこから来ますか? –

+0

@FrankvanPuffelen UIDはサードパーティ認証(facebookメッセンジャープラットフォーム)によって生成されます。私たちはカスタム認証を使用します。おかげさまで – grayger

答えて

1

そこはgetUser()への呼び出しが失敗し、明示的に新しいユーザの作成を要求する前にauth/user-not-foundをチェックする方が安全だろう、なぜauth/internal-error以外の他のdocumented reasonsの多くではありません。

function createUserIfNotExist(userId, userName) { 
    admin.auth().getUser(userId).then(function (userRecord) { 
     return userRecord; 
    }).catch(function (error) { 
     if (error.code === 'auth/user-not-found') { 
      return admin.auth().createUser({ 
       uid: userId, 
       displayName: userName, 
      }); 
     } else { 
      console.error("Error getting user data:", error); 
     } 
    }) 
} 
+0

私はあなたのerror.codeチェックを含んでいませんでしたが、それはcatch節にスローされるとき、常に 'auth/user-not-found'です。 – grayger

関連する問題