Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
- Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
- AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
- firebaseAuth.signInWithCredential(credential)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // Sign in success, update UI with the signed-in user's information
- Log.d(TAG, "signInWithCredential:success");
- FirebaseUser user = firebaseAuth.getCurrentUser();
- databaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(user.getUid());
- databaseReference.child("fullName").setValue(user.getDisplayName());
- databaseReference.child("email").setValue(user.getEmail());
- databaseReference.child("profile").setValue(user.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null);
- databaseReference.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- Profile profile = dataSnapshot.getValue(Profile.class);
- if (dataSnapshot.exists()) {
- if (profile.getUserType() != null) {
- Log.e("key", dataSnapshot.getKey());
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(SignInActivity.this);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- if (profile != null) {
- editor.putString(Preferences.EMAIL, profile.getEmail());
- editor.putString(Preferences.NAME, profile.getFullName());
- editor.putString(Preferences.USER_TYPE, profile.getUserType());
- }
- editor.putString(Preferences.USERID, getUid());
- editor.apply();
- Intent loginIntent = new Intent(SignInActivity.this, MainMenuActivity.class);
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- startActivity(loginIntent);
- finish();
- } else {
- Intent contactIntent = new Intent(SignInActivity.this, ContactDetailsActivity.class);
- contactIntent.putExtra("userEmailAddress", profile.getEmail());
- startActivity(contactIntent);
- finish();
- }
- } else {
- Toast.makeText(SignInActivity.this, "Snapshot not yet saved", Toast.LENGTH_SHORT).show();
- }
- Log.e("key", dataSnapshot.getKey());
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- //updateUI(user);
- } else {
- // If sign in fails, display a message to the user.
- Log.w(TAG, "signInWithCredential:failure", task.getException());
- Toast.makeText(SignInActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- //updateUI(null);
- }
- }
- });
- }
- // Google Sign In Integration - End
- private void signInUser() {
- String email = emailLoginTextInputEditText.getText().toString();
- String password = passwordLoginEditText.getText().toString();
- if (!validateEmail(email)) {
- return;
- }
- if (!validateSetPass(password)) {
- return;
- }
- showProgressDialog("Signing in...");
- firebaseAuth.signInWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (!task.isSuccessful()) {
- hideProgressDialog();
- Toast.makeText(SignInActivity.this, task.getException().getMessage(),
- Toast.LENGTH_SHORT).show();
- } else if (task.isSuccessful()) {
- hideProgressDialog();
- DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(getUid());
- databaseReference.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- Profile profile = dataSnapshot.getValue(Profile.class);
- Log.e("key", dataSnapshot.getKey());
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(SignInActivity.this);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- if (profile != null) {
- editor.putString(Preferences.EMAIL, profile.getEmail());
- editor.putString(Preferences.NAME, profile.getFullName());
- editor.putString(Preferences.USER_TYPE, profile.getUserType());
- }
- editor.putString(Preferences.USERID, getUid());
- editor.apply();
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- Intent loginIntent = new Intent(SignInActivity.this, MainMenuActivity.class);
- loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- startActivity(loginIntent);
- finish();
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement