Created
March 3, 2017 09:01
-
-
Save menuka94/502ddb17f9c4c218ffdba7d4374d4f86 to your computer and use it in GitHub Desktop.
Android Firebase Authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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