Advertisement
Guest User

Untitled

a guest
Sep 10th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. dependencies {
  2. implementation fileTree(dir: 'libs', include: ['*.jar'])
  3. implementation 'com.android.support:appcompat-v7:26.0.2'
  4. implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  5. testImplementation 'junit:junit:4.12'
  6. androidTestImplementation 'com.android.support.test:runner:1.0.1'
  7. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
  8. //Firebaase
  9. compile 'com.google.android.gms:play-services-auth:11.2.2'
  10. compile 'com.google.firebase:firebase-auth:11.2.2'
  11. compile 'com.google.android.gms:play-services-auth:11.2.2'
  12. //suport
  13. compile 'com.android.support:appcompat-v7:26.0.2'
  14. compile 'com.android.support:multidex:1.0.2'
  15. testCompile 'junit:junit:4.12'}
  16.  
  17. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  18. .requestIdToken(getString(R.string.default_web_client_id))
  19. .requestEmail()
  20. .build();
  21.  
  22.  
  23. mGoogleApiClient = new GoogleApiClient.Builder(this)
  24. .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
  25. @Override
  26. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  27. Toast.makeText(MainActivity.this, "Algo Salio Mal", Toast.LENGTH_SHORT).show();
  28. }
  29. }).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
  30.  
  31.  
  32.  
  33. btnCuenta.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. Intent intent = new Intent(v.getContext(),CorreoElectronico.class);
  37. startActivity(intent);
  38. }
  39. });
  40.  
  41. btnCorreo.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View v) {
  44. Intent i = new Intent(v.getContext(),CorreoElectronico.class);
  45. startActivity(i);
  46. }
  47. });
  48.  
  49.  
  50. }
  51.  
  52. private void inicializarAutenticacion() {
  53. mAuth = FirebaseAuth.getInstance();
  54. mAuthListener = new FirebaseAuth.AuthStateListener() {
  55. @Override
  56. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  57. FirebaseUser usr = firebaseAuth.getCurrentUser();
  58. if(usr != null)
  59. {
  60. startActivity(new Intent(MainActivity.this,Main2Activity.class));
  61.  
  62. }else{
  63. Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
  64. }
  65. }
  66. };
  67. }
  68.  
  69.  
  70.  
  71. private void cerrarSesionFirebase()
  72. {
  73. mAuth.signOut();
  74. }
  75.  
  76.  
  77.  
  78.  
  79. private void goMainScreen() {
  80. // Intent in = new Intent(MainActivity.this,Main2Activity.class);
  81. Intent in = new Intent();
  82. in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  83. startActivity(in);
  84. }
  85.  
  86.  
  87. // GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  88. // .requestIdToken(getString(R.string.default_web_client_id))
  89. // .requestEmail()
  90. // .build();
  91. private void signIn()
  92. {
  93. Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
  94. startActivityForResult(signInIntent,RC_SIGN_IN);
  95. }
  96.  
  97.  
  98. public void onActivityResult(int requestCode, int resultCode, Intent data)
  99. {
  100.  
  101. super.onActivityResult(requestCode,resultCode,data);
  102.  
  103.  
  104.  
  105. if(requestCode == RC_SIGN_IN)
  106. {
  107. GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
  108. if(result.isSuccess())
  109. {
  110. GoogleSignInAccount account = result.getSignInAccount();
  111. firebaseAuthWithGoogle(account);
  112. startActivity(new Intent(MainActivity.this,Main2Activity.class));
  113. }else
  114. {
  115. Toast.makeText(MainActivity.this, "Autentificacion Fallida", Toast.LENGTH_SHORT).show();
  116. }
  117. }
  118. }
  119. private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
  120. AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(),null);
  121. mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  122. @Override
  123. public void onComplete(@NonNull Task<AuthResult> task) {
  124. if(task.isSuccessful())
  125. {
  126. Log.d( "TAG", "signInWithCredential:success");
  127. FirebaseUser user = mAuth.getCurrentUser();
  128. // updateUI(user);
  129.  
  130. }else{
  131. Log.w("TAG","signWithCredential:failure",task.getException());
  132. Toast.makeText(MainActivity.this, "Autentificacion fallida", Toast.LENGTH_SHORT).show();
  133. //updateUI(null);
  134. }
  135. }
  136. });
  137. }
  138.  
  139. createUser.setOnClickListener(new View.OnClickListener() {
  140. @Override
  141. public void onClick(View view) {
  142. userEmail = email.getText().toString().trim();
  143. userPass = password.getText().toString().trim();
  144. if(userEmail.equals("") || userPass.equals(""))
  145. {
  146. Toast.makeText(CorreoElectronico.this, "Se dejaron campos vacios", Toast.LENGTH_SHORT).show();
  147. }
  148.  
  149. if(!TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userPass))
  150. {
  151. mAuth.createUserWithEmailAndPassword(userEmail,userPass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  152. @Override
  153. public void onComplete(@NonNull Task<AuthResult> task) {
  154. // startActivity(new Intent(CorreoElectronico.this,Main2Activity.class));
  155. if(task.isSuccessful())
  156. Toast.makeText(CorreoElectronico.this, "Usuario Creado", Toast.LENGTH_SHORT).show();
  157. else
  158. {
  159. Toast.makeText(CorreoElectronico.this, "Error al Crear el Usuario", Toast.LENGTH_SHORT).show();
  160. }
  161. }
  162. });
  163. }
  164.  
  165.  
  166. }
  167. });
  168.  
  169.  
  170.  
  171. //move to login
  172. login.setOnClickListener((View v) -> {
  173. //startActivity(new Intent(CorreoElectronico.this,Main2Activity.class));
  174.  
  175. userEmail = email.getText().toString().trim();
  176. userPass = password.getText().toString().trim();
  177.  
  178. if(userEmail.equals("")||userPass.equals(""))
  179. {
  180. Toast.makeText(CorreoElectronico.this, "Existen Campos Vacios", Toast.LENGTH_SHORT).show();
  181. }
  182.  
  183.  
  184. if(!TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userPass))
  185. mAuth.signInWithEmailAndPassword(userEmail, userPass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  186. @Override
  187. public void onComplete(@NonNull Task<AuthResult> task) {
  188. if (task.isSuccessful()) {
  189. startActivity(new Intent(CorreoElectronico.this, Main2Activity.class));
  190. Toast.makeText(CorreoElectronico.this, "Bienvenido", Toast.LENGTH_SHORT).show();
  191. } else {
  192. Toast.makeText(CorreoElectronico.this, "Fallo al Ingresar", Toast.LENGTH_SHORT).show();
  193. }
  194. }
  195. });
  196.  
  197. });
  198.  
  199.  
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement