Logicky Blog

Logickyの開発ブログです

Android - Firebase Auth ログアウト

FirebaseAuth auth = FirebaseAuth.getInstance(); auth.signOut();

public class MainActivity extends AppCompatActivity{
    private static final String TAG = "MainActivity";
    private FirebaseAuth auth;
    private FirebaseUser user;
    private String name;
    private String photo;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();

        if (user == null) {
            startActivity(new Intent(this, SignInActivity.class));
            finish();
            return;
        } else {
            name = user.getDisplayName();
            photo = user.getPhotoUrl().toString();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.sign_out_menu:
                Log.d(TAG, "Sign Out");
                auth.signOut();
                startActivity(new Intent(this, SignInActivity.class));
                finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}