Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'com.android.support:appcompat-v7:26.0.2'
- implementation 'com.android.support.constraint:constraint-layout:1.0.2'
- testImplementation 'junit:junit:4.12'
- androidTestImplementation 'com.android.support.test:runner:1.0.1'
- androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
- //Firebaase
- compile 'com.google.android.gms:play-services-auth:11.2.2'
- compile 'com.google.firebase:firebase-auth:11.2.2'
- compile 'com.google.android.gms:play-services-auth:11.2.2'
- //suport
- compile 'com.android.support:appcompat-v7:26.0.2'
- compile 'com.android.support:multidex:1.0.2'
- testCompile 'junit:junit:4.12'}
- GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id))
- .requestEmail()
- .build();
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- Toast.makeText(MainActivity.this, "Algo Salio Mal", Toast.LENGTH_SHORT).show();
- }
- }).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
- btnCuenta.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(v.getContext(),CorreoElectronico.class);
- startActivity(intent);
- }
- });
- btnCorreo.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent i = new Intent(v.getContext(),CorreoElectronico.class);
- startActivity(i);
- }
- });
- }
- private void inicializarAutenticacion() {
- mAuth = FirebaseAuth.getInstance();
- mAuthListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser usr = firebaseAuth.getCurrentUser();
- if(usr != null)
- {
- startActivity(new Intent(MainActivity.this,Main2Activity.class));
- }else{
- Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
- }
- }
- };
- }
- private void cerrarSesionFirebase()
- {
- mAuth.signOut();
- }
- private void goMainScreen() {
- // Intent in = new Intent(MainActivity.this,Main2Activity.class);
- Intent in = new Intent();
- in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(in);
- }
- // GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- // .requestIdToken(getString(R.string.default_web_client_id))
- // .requestEmail()
- // .build();
- private void signIn()
- {
- Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
- startActivityForResult(signInIntent,RC_SIGN_IN);
- }
- public void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode,resultCode,data);
- if(requestCode == RC_SIGN_IN)
- {
- GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
- if(result.isSuccess())
- {
- GoogleSignInAccount account = result.getSignInAccount();
- firebaseAuthWithGoogle(account);
- startActivity(new Intent(MainActivity.this,Main2Activity.class));
- }else
- {
- Toast.makeText(MainActivity.this, "Autentificacion Fallida", 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())
- {
- Log.d( "TAG", "signInWithCredential:success");
- FirebaseUser user = mAuth.getCurrentUser();
- // updateUI(user);
- }else{
- Log.w("TAG","signWithCredential:failure",task.getException());
- Toast.makeText(MainActivity.this, "Autentificacion fallida", Toast.LENGTH_SHORT).show();
- //updateUI(null);
- }
- }
- });
- }
- createUser.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- userEmail = email.getText().toString().trim();
- userPass = password.getText().toString().trim();
- if(userEmail.equals("") || userPass.equals(""))
- {
- Toast.makeText(CorreoElectronico.this, "Se dejaron campos vacios", Toast.LENGTH_SHORT).show();
- }
- if(!TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userPass))
- {
- mAuth.createUserWithEmailAndPassword(userEmail,userPass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- // startActivity(new Intent(CorreoElectronico.this,Main2Activity.class));
- if(task.isSuccessful())
- Toast.makeText(CorreoElectronico.this, "Usuario Creado", Toast.LENGTH_SHORT).show();
- else
- {
- Toast.makeText(CorreoElectronico.this, "Error al Crear el Usuario", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
- });
- //move to login
- login.setOnClickListener((View v) -> {
- //startActivity(new Intent(CorreoElectronico.this,Main2Activity.class));
- userEmail = email.getText().toString().trim();
- userPass = password.getText().toString().trim();
- if(userEmail.equals("")||userPass.equals(""))
- {
- Toast.makeText(CorreoElectronico.this, "Existen Campos Vacios", Toast.LENGTH_SHORT).show();
- }
- if(!TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userPass))
- mAuth.signInWithEmailAndPassword(userEmail, userPass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- startActivity(new Intent(CorreoElectronico.this, Main2Activity.class));
- Toast.makeText(CorreoElectronico.this, "Bienvenido", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(CorreoElectronico.this, "Fallo al Ingresar", Toast.LENGTH_SHORT).show();
- }
- }
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement