Skip to content

Instantly share code, notes, and snippets.

@huewu
Last active November 4, 2016 23:40
Show Gist options
  • Save huewu/da67add635832fecb4bdb1a52046a6cd to your computer and use it in GitHub Desktop.
Save huewu/da67add635832fecb4bdb1a52046a6cd to your computer and use it in GitHub Desktop.
Firebase Workshop Signin 관련 코드 조각들
//in MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
showMessageListFragment();
} else {
FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
tr.add(R.id.activity_main, new LoginFragment());
tr.commit();
}
}
//in MessageListFragment.java
private void signInAsGuest() {
// TODO implement Guest sign in
FirebaseAuth.getInstance().signInAnonymously()
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
notifyLoginCompleted(authResult.getUser());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Fail to Login:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
//in MessageListFragment.java
private static final int RC_GOOGLE_SIGN_IN = 1013;
private void signInWithGoogle() {
// ID Token is required to create AuthCredential for Firebase
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getContext().getString(R.string.default_web_client_id))
.requestEmail()
.build();
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(getContext())
.enableAutoManage(getActivity(), null)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
Intent signInIntent
= Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(signInIntent, RC_GOOGLE_SIGN_IN);
}
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RC_GOOGLE_SIGN_IN:
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed
Log.e(TAG, "Google Sign In failed.");
notifyLoginFailed();
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
//Create AuthCredential by using IdToken from GoogleSignInAccount
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mFirebaseAuth.signInWithCredential(credential)
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
notifyLoginCompleted(authResult.getUser());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Fail to Login:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
//in MessageListFragment.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sign_out_menu:
//TODO implement sign out feature here
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user.getProviderId() != null) {
googleAccountSignOut();
}
FirebaseAuth.getInstance().signOut();
loginListener.onLogoutCompleted(null);
return true;
}
return super.onOptionsItemSelected(item);
}
private void googleAccountSignOut() {
Auth.GoogleSignInApi.signOut(GoogleApiClientUtil.getInstance());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment