Created
April 26, 2018 13:00
Star
You must be signed in to star a gist
This file contains 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
public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,View.OnClickListener { | |
private FirebaseAuth mAuth; | |
SignInButton signInButton; | |
GoogleApiClient mGoogleApliClient; | |
private static final int RC_SIGN_IN = 9001; | |
private static final String TAG = "LoginActivity.class"; | |
private Context mContext; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
this.mContext = this; | |
FirebaseApp.initializeApp(mContext); | |
mAuth = FirebaseAuth.getInstance(); | |
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
.requestIdToken(getString(R.string.default_web_client_id)) | |
.requestEmail() | |
.build(); | |
mGoogleApliClient = new GoogleApiClient.Builder(this) | |
.enableAutoManage(this,this) | |
.addApi(Auth.GOOGLE_SIGN_IN_API,gso) | |
.build(); | |
signInButton = (SignInButton) findViewById(R.id.sign_in_button); | |
signInButton.setOnClickListener(this); | |
} | |
private void signIn() { | |
if(!NetworkConnection.isConnectingToInternet(mContext)) { | |
DynamicToast.makeError(mContext,"No internet connection",Toast.LENGTH_SHORT); | |
return; | |
} | |
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApliClient); | |
startActivityForResult(signInIntent, RC_SIGN_IN); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); | |
if (requestCode == RC_SIGN_IN) { | |
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); | |
try { | |
// Google Sign In was successful, authenticate with Firebase | |
GoogleSignInAccount account = task.getResult(ApiException.class); | |
firebaseAuthWithGoogle(account); | |
} catch (ApiException e) { | |
// Google Sign In failed, update UI appropriately | |
Log.w(TAG, "Google sign in failed", e.getCause()); | |
DynamicToast.makeError(mContext,getResources().getString(R.string.google_sign_in_failed) + e.getCause(), Toast.LENGTH_SHORT).show(); | |
MyApplication.getInstance().trackEvent("Login", "Connecting to Google", "Track event sign in failed"); | |
// ... | |
} | |
} | |
} | |
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { | |
//Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); | |
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); | |
mAuth.signInWithCredential(credential) | |
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
// Sign in success, update UI with the signed-in user's information | |
Log.d(TAG, "signInWithCredential:success"); | |
FirebaseUser user = mAuth.getCurrentUser(); | |
if(user!=null){ | |
//user.getDisplayName() is the name of user logged | |
DatabaseReference ref= FirebaseDatabase.getInstance().getReference().child(GlobalConstants.FIREBASE_CHILD_USERS).child(user.getUid()); | |
ref.child("name").setValue(user.getDisplayName()); | |
ref.child("email").setValue(user.getEmail()); | |
DynamicToast.makeSuccess(mContext,"Login successfully!",Toast.LENGTH_SHORT).show(); | |
MyApplication.getInstance().trackEvent("Login", "Connecting to Firebase", "Track event connection successfully"); | |
goToMain(); | |
} | |
} else { | |
// If sign in fails, display a message to the user. | |
Log.w(TAG, "signInWithCredential:failure", task.getException()); | |
DynamicToast.makeError(mContext, getResources().getString(R.string.google_sign_in_failed),Toast.LENGTH_SHORT).show(); | |
//Toast.makeText(mContext, "signInWithCredential:failure "+ task.getException(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
private void goToMain(){ | |
startActivity(new Intent(mContext,MainActivity.class)); | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
// Check if user is signed in (non-null) and update UI accordingly. | |
FirebaseUser currentUser = mAuth.getCurrentUser(); | |
if(currentUser!=null) goToMain(); | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()){ | |
case R.id.sign_in_button: | |
signIn(); | |
break; | |
default: | |
break; | |
} | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
DynamicToast.makeError(mContext,"Connection failed", Toast.LENGTH_SHORT).show(); | |
MyApplication.getInstance().trackEvent("Login", "Connecting to Firebase", "Track event connection failed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment