Skip to content

Instantly share code, notes, and snippets.

@menuka94
Created March 3, 2017 09:01
Show Gist options
  • Save menuka94/502ddb17f9c4c218ffdba7d4374d4f86 to your computer and use it in GitHub Desktop.
Save menuka94/502ddb17f9c4c218ffdba7d4374d4f86 to your computer and use it in GitHub Desktop.
Android Firebase Authentication
// Firebase instance variables
private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
private String mUsername;
private String mPhotoUrl;
@Override
protected void onCreate(Bundle savedInstanceState){
// Initialize Firebase Auth
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
if(mFirebaseUser == null){
// Not signed in, launch the Sign In activity
startActivity(new Intent(this, SignInActivity.class));
finish();
return;
}else{
mUsername = mFirebaseUser.getDisplayName();
if(mFirebaseUser.getPhotoUrl() != null){
mPhotoUrl = mFirebaseUser.getPhotoUrl().toString();
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.sign_out_menu:
mFirebaseAuth.signOut();
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
mUsername = ANONYMOUS;
startActivity(new Intent(this, SignIntActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment