Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.soyalab.askaruly.lor;
- import android.content.Intent;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.facebook.CallbackManager;
- import com.facebook.FacebookCallback;
- import com.facebook.FacebookException;
- import com.facebook.login.LoginResult;
- import com.facebook.login.widget.LoginButton;
- import com.google.android.gms.auth.api.Auth;
- import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
- import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
- import com.google.android.gms.auth.api.signin.GoogleSignInResult;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.SignInButton;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.AuthCredential;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.auth.GoogleAuthProvider;
- public class EnterActivity extends AppCompatActivity {
- SignInButton googleButton;
- FirebaseAuth mAuth;
- LoginButton fbButton;
- TextView loginStatus;
- CallbackManager callbackManager;
- private final static int RC_SIGN_IN = 2;
- GoogleApiClient mGoogleApiClient;
- FirebaseAuth.AuthStateListener mAuthListener;
- @Override
- protected void onStart() {
- super.onStart();
- mAuth.addAuthStateListener(mAuthListener);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_enter);
- fbButton = (LoginButton)findViewById(R.id.fbBtn);
- googleButton = (SignInButton)findViewById(R.id.googleBtn);
- loginStatus = (TextView)findViewById(R.id.loginStatus);
- callbackManager = CallbackManager.Factory.create();
- fbButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
- @Override
- public void onSuccess(LoginResult loginResult) {
- loginStatus.setText("Login Success\n" + loginResult.getAccessToken().getUserId());
- /*startActivity(new Intent(EnterActivity.this,MainActivity.class));*/
- startActivity(new Intent(EnterActivity.this,LoginActivity.class));
- }
- @Override
- public void onCancel() {
- loginStatus.setText("Login cancelled");
- }
- @Override
- public void onError(FacebookException error) {
- }
- });
- mAuth = FirebaseAuth.getInstance();
- googleButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- signIn();
- }
- });
- mAuthListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- if (firebaseAuth.getCurrentUser() != null) {
- /*startActivity(new Intent(EnterActivity.this,MainActivity.class));*/
- startActivity(new Intent(EnterActivity.this,LoginActivity.class));
- }
- }
- };
- // Configure Google Sign In
- GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id))
- .requestEmail()
- .build();
- // Build a GoogleApiClient with access to the Google Sign-In API and the
- // options specified by gso.
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- Toast.makeText(EnterActivity.this, "Something went wrong",Toast.LENGTH_SHORT).show();
- }
- })
- .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
- .build();
- }
- private void signIn() {
- Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
- 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) {
- 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, update UI appropriately
- // ...
- Toast.makeText(EnterActivity.this, "Auth went wrong",Toast.LENGTH_SHORT).show();
- }
- }
- }
- private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
- AuthCredential credential = GoogleAuthProvider.getCredential(account.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();
- //updateUI(user);
- } else {
- // If sign in fails, display a message to the user.
- Log.w("TAG", "signInWithCredential:failure", task.getException());
- Toast.makeText(EnterActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- //updateUI(null);
- }
- // ...
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement