Guest User

Untitled

a guest
Nov 14th, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. private static final int RC_SIGN_IN = 9001;
  2.  
  3.  
  4. private SignInButton btnInicioSesionGoogle;
  5. private EditText edtEmail , edtPass;
  6. private Button btnInicioSesion;
  7. private TextView txtRegistro;
  8.  
  9. private ProgressDialog progressDialog;
  10. private FirebaseAuth firebaseAuth;
  11. private GoogleApiClient mGoogleApiClient;
  12.  
  13.  
  14.  
  15. public View onCreateView(LayoutInflater inflater, ViewGroup container
  16. , Bundle savedInstanceState){
  17.  
  18. View view = inflater.inflate(R.layout.fragment_inicio_sesion
  19. ,container , false);
  20.  
  21.  
  22. progressDialog = new ProgressDialog(getActivity());
  23. firebaseAuth = FirebaseAuth.getInstance();
  24.  
  25. edtEmail = view.findViewById(R.id.edtEmail);
  26. edtPass = view.findViewById(R.id.edtPassword);
  27.  
  28. btnInicioSesion = view.findViewById(R.id.btnInicioSesion);
  29. btnInicioSesion.setOnClickListener(this);
  30.  
  31.  
  32. //////////////GOOGLE///////////////////////////////////////////
  33.  
  34. btnInicioSesionGoogle = view.findViewById(R.id.btnInicioSesionGoogle);
  35. btnInicioSesionGoogle.setOnClickListener(this);
  36.  
  37. return view;
  38. }
  39.  
  40. public void iniciarSesionCorreoYPass(){
  41.  
  42. final String email = edtEmail.getText().toString().trim();
  43. final String password = edtPass.getText().toString().trim();
  44.  
  45. if (TextUtils.isEmpty(email)){
  46. Toast.makeText(getActivity(),"Introduce su correo electronico",Toast.LENGTH_LONG);
  47. return;
  48. }
  49. if (TextUtils.isEmpty(password)){
  50. Toast.makeText(getActivity(),"Introduce contraseña",Toast.LENGTH_LONG);
  51. }
  52.  
  53. progressDialog.setMessage("Iniciando Sesion");
  54. progressDialog.show();
  55.  
  56. //Login
  57.  
  58. firebaseAuth.signInWithEmailAndPassword(email , password).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
  59. @Override
  60. public void onComplete(@NonNull Task<AuthResult> task) {
  61. if (task.isSuccessful()) {
  62.  
  63. FirebaseUser usuario = firebaseAuth.getCurrentUser();
  64. progressDialog.dismiss();
  65.  
  66. Toast.makeText(getActivity(), "Hola de nuevo: "+usuario.getEmail() , Toast.LENGTH_LONG).show();
  67.  
  68. Intent intent = new Intent(getActivity(), DrawerActivity.class);
  69. startActivity(intent);
  70. }
  71.  
  72. if(task.isCanceled())
  73. {
  74. progressDialog.dismiss();
  75. Toast.makeText(getActivity(), "ERROR. Datos Incorrectos" , Toast.LENGTH_LONG).show();
  76.  
  77. }
  78. }
  79. });
  80. }
  81.  
  82. //Google
  83. private void iniciarSesionGoogle() {
  84.  
  85. if(mGoogleApiClient != null) {
  86. mGoogleApiClient.disconnect();
  87. }
  88.  
  89. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  90. .requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();
  91. //GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
  92. mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
  93.  
  94. final Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
  95. startActivityForResult(signInIntent, RC_SIGN_IN);
  96. }
  97.  
  98. @Override
  99. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  100. super.onActivityResult(requestCode, resultCode, data);
  101.  
  102. // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
  103. if (requestCode == RC_SIGN_IN) {
  104. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
  105.  
  106. try {
  107. //final GoogleApiClient client = mGoogleApiClient;
  108.  
  109. // Signed in successfully, show authenticated UI.
  110. GoogleSignInAccount account = task.getResult(ApiException.class);
  111.  
  112. firebaseAuthWithGoogle(account);
  113. } catch (ApiException e) {
  114. Log.w(TAG, "Google sign in failed", e);
  115.  
  116. }
  117.  
  118.  
  119. }
  120. }
  121. private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
  122. Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
  123.  
  124. AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
  125. firebaseAuth.signInWithCredential(credential)
  126. .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
  127. @Override
  128. public void onComplete(@NonNull Task<AuthResult> task) {
  129. if (task.isSuccessful()) {
  130. // Sign in success, update UI with the signed-in user's information
  131.  
  132. FirebaseUser user = firebaseAuth.getCurrentUser();
  133. Toast.makeText(getActivity(), "GOOOOOOGLEEEEEEE", Toast.LENGTH_LONG).show();
  134. Intent intent = new Intent(getActivity(), DrawerActivity.class);
  135. startActivity(intent);
  136.  
  137. } else {
  138. // If sign in fails, display a message to the user.
  139. Log.w(TAG, "signInWithCredential:failure", task.getException());
  140. }
  141. }
  142. });
  143. }
  144.  
  145. @Override
  146. public void onClick(View v) {
  147.  
  148. switch (v.getId())
  149. {
  150. case R.id.btnInicioSesion:
  151. iniciarSesionCorreoYPass();
  152. case R.id.btnInicioSesionGoogle:
  153. iniciarSesionGoogle();
  154. break;
  155. }
  156. }
Add Comment
Please, Sign In to add comment