如何在Android上自动登录FirebaseAuth(电子邮件/密码)?

时间:2017-08-01 20:36:44

标签: java android firebase firebase-authentication

我尝试自动登录FirebaseAuth,但这不起作用。 当用户输入电子邮件/密码并检查"记住我"我想保存令牌以便在下次登录时使用(自动)。

我不知道是否将令牌保存在onAuthStateChanged(FirebaseAuth.AuthStateListener())或onComplete(signInWithEmailAndPassword)中。

所以,应用程序保持这样:

prefs = getActivity().getSharedPreferences("quinta", Context.MODE_PRIVATE);
editor = prefs.edit();
mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                user = firebaseAuth.getCurrentUser();

                if ( user != null ) {
                    Log.i(TAG,"User: " + user.getDisplayName() + ", " + user.getEmail() + " Sign In");
                    String token = user.getToken(true).toString();
                    editor.putString("token",token);
                    editor.commit();
                } else {
                    Log.i(TAG,"User: " + user.getDisplayName() + " Sign Out");
                }
            }
        };

因此,当应用程序打开时,我尝试登录:

if( prefs.getString("token","aa").length() > 2 ) {
            Log.i(TAG,prefs.getString("token","aa"));
            mAuth.signInWithCustomToken(prefs.getString("token",""));
}

并且是的,在Log I中检查prefs.getString(" token","")是否包含令牌! 最后,我重新打开应用程序,并收到以下错误:

I/AuthChimeraService: Error description received from server: {
                                                      "error": {
                                                       "errors": [
                                                        {
                                                         "domain": "global",
                                                         "reason": "invalid",
                                                         "message": "INVALID_CUSTOM_TOKEN"
                                                        }
                                                       ],
                                                       "code": 400,
                                                       "message": "INVALID_CUSTOM_TOKEN"
                                                      }
                                                     }

0 个答案:

没有答案