Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final int RC_SIGN_IN = 9001;
- private SignInButton btnInicioSesionGoogle;
- private EditText edtEmail , edtPass;
- private Button btnInicioSesion;
- private TextView txtRegistro;
- private ProgressDialog progressDialog;
- private FirebaseAuth firebaseAuth;
- private GoogleApiClient mGoogleApiClient;
- public View onCreateView(LayoutInflater inflater, ViewGroup container
- , Bundle savedInstanceState){
- View view = inflater.inflate(R.layout.fragment_inicio_sesion
- ,container , false);
- progressDialog = new ProgressDialog(getActivity());
- firebaseAuth = FirebaseAuth.getInstance();
- edtEmail = view.findViewById(R.id.edtEmail);
- edtPass = view.findViewById(R.id.edtPassword);
- btnInicioSesion = view.findViewById(R.id.btnInicioSesion);
- btnInicioSesion.setOnClickListener(this);
- //////////////GOOGLE///////////////////////////////////////////
- btnInicioSesionGoogle = view.findViewById(R.id.btnInicioSesionGoogle);
- btnInicioSesionGoogle.setOnClickListener(this);
- return view;
- }
- public void iniciarSesionCorreoYPass(){
- final String email = edtEmail.getText().toString().trim();
- final String password = edtPass.getText().toString().trim();
- if (TextUtils.isEmpty(email)){
- Toast.makeText(getActivity(),"Introduce su correo electronico",Toast.LENGTH_LONG);
- return;
- }
- if (TextUtils.isEmpty(password)){
- Toast.makeText(getActivity(),"Introduce contraseña",Toast.LENGTH_LONG);
- }
- progressDialog.setMessage("Iniciando Sesion");
- progressDialog.show();
- //Login
- firebaseAuth.signInWithEmailAndPassword(email , password).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- FirebaseUser usuario = firebaseAuth.getCurrentUser();
- progressDialog.dismiss();
- Toast.makeText(getActivity(), "Hola de nuevo: "+usuario.getEmail() , Toast.LENGTH_LONG).show();
- Intent intent = new Intent(getActivity(), DrawerActivity.class);
- startActivity(intent);
- }
- if(task.isCanceled())
- {
- progressDialog.dismiss();
- Toast.makeText(getActivity(), "ERROR. Datos Incorrectos" , Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- //Google
- private void iniciarSesionGoogle() {
- if(mGoogleApiClient != null) {
- mGoogleApiClient.disconnect();
- }
- GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();
- //GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
- mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
- final 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) {
- Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
- try {
- //final GoogleApiClient client = mGoogleApiClient;
- // Signed in successfully, show authenticated UI.
- GoogleSignInAccount account = task.getResult(ApiException.class);
- firebaseAuthWithGoogle(account);
- } catch (ApiException e) {
- Log.w(TAG, "Google sign in failed", e);
- }
- }
- }
- private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
- Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
- AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
- firebaseAuth.signInWithCredential(credential)
- .addOnCompleteListener(getActivity(), 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
- FirebaseUser user = firebaseAuth.getCurrentUser();
- Toast.makeText(getActivity(), "GOOOOOOGLEEEEEEE", Toast.LENGTH_LONG).show();
- Intent intent = new Intent(getActivity(), DrawerActivity.class);
- startActivity(intent);
- } else {
- // If sign in fails, display a message to the user.
- Log.w(TAG, "signInWithCredential:failure", task.getException());
- }
- }
- });
- }
- @Override
- public void onClick(View v) {
- switch (v.getId())
- {
- case R.id.btnInicioSesion:
- iniciarSesionCorreoYPass();
- case R.id.btnInicioSesionGoogle:
- iniciarSesionGoogle();
- break;
- }
- }
Add Comment
Please, Sign In to add comment