4

Firebase のおかげで、ユーザーは G+、Facebook、または Twitter の助けを借りてログインできます。それらがログに記録されると、すべて問題ありません。

Android アプリを閉じて再度開いたときに、Firebase API を使用して以前の成功したログ ユーザーを再度有効にする方法。アプリのデモでもドキュメントでも説明されていません。

たとえば、Facebook の場合、sdk はトークンを保存しているように見えます。そのため、ボタンは接続状態になっています (切断できることを示しています)。しかし、Firebase やその他の認証システムについてはどうでしょうか。

4

4 に答える 4

2

を追加する必要がありますAuthStateListener。これは、Monitoring Authentication に関する Firebase ドキュメントで説明されています。そこから:

Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.addAuthStateListener(new Firebase.AuthStateListener() {
    @Override
    public void onAuthStateChanged(AuthData authData) {
        if (authData != null) {
            // user is logged in
        } else {
            // user is not logged in
        }
    }
});

Android での Firebase Authentication に関連するものについては、専用のデモ アプリが次の目的地として最適です。しかし、必ず最初にドキュメントを読んでください。ドキュメントに関する限り、それらは半分も悪くありません。

于 2015-10-02T14:34:52.180 に答える
1

BaseActivity クラスを作成し、アプリ内の他のすべてのアクティビティがそのクラスを拡張していることを確認します。authData が AuthListener から null の場合、'instanceOf' を使用してユーザーを LoginActivity に送信します。

package com.mabiri.mabiristores;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.mabiri.mabiristores.login.CreateAccount2Activity;
import com.mabiri.mabiristores.login.CreateAccountActivity;
import com.mabiri.mabiristores.login.LoginActivity;
import com.mabiri.mabiristores.login.MapsActivity;
import com.mabiri.mabiristores.utils.Utils;


public class BaseActivity extends AppCompatActivity {
protected Firebase.AuthStateListener mAuthListener;
protected Firebase mFirebaseRef;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mFirebaseRef = new Firebase(YOUR_FIREBASE_URL);



    if (!((this instanceof LoginActivity) || (this instanceof CreateAccountActivity)
            || (this instanceof CreateAccount2Activity) || (this instanceof MapsActivity))) {
        mAuthListener = new Firebase.AuthStateListener() {
            @Override
            public void onAuthStateChanged(AuthData authData) {
                 /* The user has been logged out */
                if (authData == null) {
                    //Stop services and clear sharedPreferences if any

                    /*Take user to login screen*/
                    takeUserToLoginScreenOnUnAuth();
                }
            }
        };
        mFirebaseRef.addAuthStateListener(mAuthListener);
    }

}

@Override
protected void onResume() {
    super.onResume();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home) {
        super.onBackPressed();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    /* Inflate the menu; this adds items to the action bar if it is present. */
    getMenuInflater().inflate(R.menu.menu_base, menu);
    return true;
}

private void takeUserToLoginScreenOnUnAuth() {

    /** Move user to LoginActivity, and remove the backstack */
    Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |    Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
    finish();
}

protected void logout() {
/**Unauthenticate user from firebase*/
    mFirebaseRef.unauth();
}

/**
 * Show error toast to users
 */
protected void showErrorToast(Context context, String message) {
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}

}

于 2016-03-28T07:27:23.110 に答える