web-dev-qa-db-ja.com

Firebase:Androidユーザーのログインを維持する方法は?

Firebase SimpleLoginを使用してメール/パスワード認証を有効にしています。ユーザーの作成とその後のログインはすべて正常に機能しています。ただし、アプリを離れるときはいつでも(たとえ数秒間であっても)、ユーザーは私の帰宅時にログインすることはありません...

authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler())...

常にnullユーザーを返します。

APIを介してユーザーをログアウトしていません。また、Firebaseコンソールでユーザーがログインする日数を21に設定しました。

JSドキュメントでremember-me paramについて言及しましたが、Android/Javaに相当するものはありません。

ドキュメントに何か欠けているのか、それともAndroidではそれができないのだろうか?

ご協力いただきありがとうございます、

ニール。

編集:コードサンプルを追加しました。

ユーザー作成..。

public void registerUserForChat(final MyApplication application, String email, String password) {
    Firebase ref = new Firebase(FIREBASE_URL);
    SimpleLogin authClient = new SimpleLogin(ref);
    authClient.createUser(email, password, new SimpleLoginAuthenticatedHandler() {
        @Override
        public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
            if(error != null) {
                Log.e(TAG, "Error attempting to create new Firebase User: " + error);
            }
            else {
                Log.d(TAG, "User successfully registered for Firebase");
                application.setLoggedIntoChat(true);
            }
        }
    });
}

ユーザーログイン..。

public void loginUserForChat(final MyApplication application,  String email, String password) {
    Log.d(TAG, "Attempting to login Firebase user...");
    Firebase ref = new Firebase(FirebaseService.FIREBASE_URL);
    final SimpleLogin authClient = new SimpleLogin(ref);
    authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler() {
        @Override
        public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
            if (error != null) {
                Log.d(TAG, "error performing check: " + error);
            } else if (user == null) {
                Log.d(TAG, "no user logged in. Will login...");
                authClient.loginWithEmail(email, password, new SimpleLoginAuthenticatedHandler() {
                    @Override
                    public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
                        if(error != null) {
                            if(com.firebase.simplelogin.enums.Error.UserDoesNotExist == error) {
                                Log.e(TAG, "UserDoesNotExist!");
                            } else {
                                Log.e(TAG, "Error attempting to login Firebase User: " + error);
                            }
                        }
                        else {
                            Log.d(TAG, "User successfully logged into Firebase");
                            application.setLoggedIntoChat(true);
                        }
                    }
                });
            } else {
                Log.d(TAG, "user is logged in");
            }
        }
    });
}

そのため、loginUserForChatメソッドはまず、ログインしているユーザーがいるかどうかを確認し、ない場合はログインを実行します。アプリを起動するたびに、表示されるログは...です。

  1. Firebaseユーザーにログインしようとしています...
  2. ログインしているユーザーはいません。ログインします...
  3. ユーザーがFirebaseにログインしました

数秒でもアプリを終了して戻ると、同じログが表示されます。

私が気づいたことの1つは、checkAuthStatusへの呼び出しがユーザー資格情報を取得しないことです-私はanyをローカルでログインしているユーザーをチェックするだけだと思いますか?

とても有難い。

16
Neil

[Firebaseのエンジニア] Firebase Simple Login Javaクライアントで永続的なセッションを透過的に処理するには、Androidを受け入れる2つの引数のコンストラクタを使用する必要があります。 =コンテキスト、つまりSimpleLogin(com.firebase.client.Firebase ref, Android.content.Context context)シンプルログインをインスタンス化するたびにJavaクライアント。

完全なAPIリファレンスについては、 https://www.firebase.com/docs/Java-simple-login-api/javadoc/com/firebase/simplelogin/SimpleLogin.html を参照してください。

12
Rob DiMarco

別の方法-onCreateでこのコードを試してください:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
    Intent i = new Intent(LoginActivity.this, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
} else {
    // User is signed out
    Log.d(TAG, "onAuthStateChanged:signed_out");
}

これにより、登録アクティビティで停止することなく、ユーザーをメインアクティビティに直接誘導することで、ユーザーがログインしたままになります。したがって、ユーザーがサインアウトをクリックしない限り、ユーザーはログインします。

11
Leenah

これを行う適切な方法は、oAuth authenticationを使用することです。

1. The user logs in.
2. You generate an access token(oAuth2).
3. Android app saves the token locally.
4. Each time the comes back to the auth, he can use the token to to log in, unless the token has been revoked by you, or he changed his
password.

幸いなことに、firebaseはすぐにそれをサポートしています。ドキュメント:

https://www.firebase.com/docs/security/custom-login.htmlhttps://www.firebase.com/docs/security/authentication.html

6
Oleg Belousov

これは、ユーザーがすでにログインしている場合に、このアプローチを使用してlogiページをエスケープすることで実行できます。

private FirebaseAuth auth;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    auth = FirebaseAuth.getInstance();

    if (auth.getCurrentUser() != null) {
        startActivity(new Intent(Login_Activity.this, Home.class));
        finish();
    }
    setContentView(R.layout.activity_login_);
1
rajeev ranjan