Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * First activity to show (LOGIN/REGISTER)
- * Made by Arturo Salinas
- * Contact me if any issue...
- */
- package sofex.foodhausv1;
- import android.content.Intent;
- import android.icu.util.Freezable;
- import android.nfc.Tag;
- 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.Button;
- import android.widget.ImageButton;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.Arrays;
- import com.facebook.AccessToken;
- import com.facebook.CallbackManager;
- import com.facebook.FacebookCallback;
- import com.facebook.FacebookException;
- import com.facebook.login.LoginManager;
- 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.GoogleSignIn;
- import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
- import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.SignInButton;
- import com.google.android.gms.common.api.ApiException;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.internal.SignInButtonImpl;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.FirebaseApp;
- import com.google.firebase.auth.AuthCredential;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FacebookAuthProvider;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.auth.GoogleAuthProvider;
- import java.sql.Array;
- public class StarterActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
- private GoogleApiClient mGoogleSignInClient;
- public static final int RC_SIGN_IN = 1;
- private ProgressBar starter_progress;
- private FirebaseAuth mAuth;
- private FirebaseAuth.AuthStateListener mAuthListener;
- //Google Auth
- private Button btn_google;
- private GoogleApiClient mGoogleApiClient;
- //FB Auth
- private Button btn_fb;
- private CallbackManager mCallbackManager;
- //Forgot
- private TextView forgot_pass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_starter);
- mAuth = FirebaseAuth.getInstance();
- FirebaseApp.initializeApp(this);
- btn_fb = (Button)findViewById(R.id.btn_fb);
- forgot_pass = (TextView)findViewById(R.id.forgot_pass);
- forgot_pass.setOnClickListener(this);
- starter_progress = (ProgressBar)findViewById(R.id.starter_progress);
- starter_progress.setVisibility(View.INVISIBLE);
- //Fb Auth
- // Initialize Facebook Login button
- mCallbackManager = CallbackManager.Factory.create();
- //FbAuth
- btn_fb.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- btn_fb.setEnabled(false);
- starter_progress.setVisibility(View.VISIBLE);
- LoginManager.getInstance().logInWithReadPermissions(StarterActivity.this, Arrays.asList("email","public_profile"));
- LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
- @Override
- public void onSuccess(LoginResult loginResult) {
- Log.d("Fb", "facebook:onSuccess:" + loginResult);
- handleFacebookAccessToken(loginResult.getAccessToken());
- }
- @Override
- public void onCancel() {
- Log.d("Fb", "facebook:onCancel");
- }
- @Override
- public void onError(FacebookException error) {
- Log.d("Fb", "facebook:onError", error);
- }
- });
- }
- });
- //Google auth
- btn_google = (Button) findViewById(R.id.btn_google);
- // Configure Google Sign In
- GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id))
- .requestEmail()
- .build();
- mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()).enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- //Error
- }
- })
- .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
- .build();
- btn_google.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(StarterActivity.this, DashboardActivity.class));
- }
- }
- };
- Button btnStartLogin = findViewById(R.id.buttonLogin);
- btnStartLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- logIn();
- }
- });
- Button btnStartRegister = findViewById(R.id.buttonRegister);
- btnStartRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- toRegisterPhone();
- }
- });
- }
- //Google auth
- private void signIn() {
- Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
- startActivityForResult(signInIntent, RC_SIGN_IN);
- }
- //Fb and google auth
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- Log.d("Google", "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
- 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("Google", "Google sign in failed", e);
- // ...
- }
- }else{
- //If not request code is RC_SIGN_IN it must be facebook
- mCallbackManager.onActivityResult(requestCode, resultCode, data);
- }
- }
- private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
- Log.d("Google", "firebaseAuthWithGoogle:" + acct.getId());
- starter_progress.setVisibility(View.VISIBLE);
- 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("google", "signInWithCredential:success");
- FirebaseUser user = mAuth.getCurrentUser();
- updateUI();
- starter_progress.setVisibility(View.INVISIBLE);
- } else {
- // If sign in fails, display a message to the user.
- Log.w("Google", "signInWithCredential:failure", task.getException());
- starter_progress.setVisibility(View.INVISIBLE);
- }
- }
- });
- }
- @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){
- updateUI();
- }
- }
- //Fb auth sends to dash
- private void updateUI() {
- //Lgoin
- sendToDash();
- }
- //FB Auth
- private void handleFacebookAccessToken(AccessToken token) {
- Log.d("Fb", "handleFacebookAccessToken:" + token);
- AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
- 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("Fb", "signInWithCredential:success");
- FirebaseUser user = mAuth.getCurrentUser();
- btn_fb.setEnabled(true);
- starter_progress.setVisibility(View.INVISIBLE);
- updateUI();
- } else {
- // If sign in fails, display a message to the user.
- Log.w("Fb", "signInWithCredential:failure", task.getException());
- btn_fb.setEnabled(true);
- starter_progress.setVisibility(View.INVISIBLE);
- }
- }
- });
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- Toast.makeText(StarterActivity.this, "Fallo en conexión con google...", Toast.LENGTH_LONG).show();
- }
- private void logIn() {
- Intent intent = new Intent(StarterActivity.this, LoginActivity.class);
- startActivity(intent);
- }
- private void sendToDash() {
- Intent intent = new Intent(StarterActivity.this, DashboardActivity.class);
- startActivity(intent);
- finish();
- }
- private void toRegisterPhone() {
- Intent registerIntent = new Intent(StarterActivity.this, RegisterPhoneActivity.class);
- startActivity(registerIntent);
- }
- @Override
- public void onClick(View view) {
- switch(view.getId()){
- case R.id.forgot_pass:
- Intent registerIntent = new Intent(StarterActivity.this, ForgotPassword.class);
- startActivity(registerIntent);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement