Last active
October 24, 2018 11:04
-
-
Save JobGetabu/6dae14e4f5df0dd08af25641153c4f0c to your computer and use it in GitHub Desktop.
Login implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <android.support.constraint.ConstraintLayout | |
| xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:app="http://schemas.android.com/apk/res-auto" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| tools:context=".ui.LoginActivity" | |
| > | |
| <ScrollView | |
| android:layout_width="0dp" | |
| android:layout_height="0dp" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toTopOf="parent"> | |
| <android.support.constraint.ConstraintLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent"> | |
| <ImageView | |
| android:id="@+id/imageView2" | |
| android:layout_width="0dp" | |
| android:layout_height="250dp" | |
| android:background="@drawable/studentbg" | |
| android:contentDescription="@string/content_decription" | |
| android:scaleType="centerCrop" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" /> | |
| <TextView | |
| android:id="@+id/head" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginEnd="16dp" | |
| android:layout_marginStart="16dp" | |
| android:layout_marginTop="8dp" | |
| android:fontFamily="@font/nunito_bold" | |
| android:gravity="center" | |
| android:text="@string/student_login" | |
| android:textColor="#4c565f" | |
| android:textSize="24sp" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/imageView2" /> | |
| <android.support.design.widget.TextInputLayout | |
| android:id="@+id/login_email" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:layout_marginEnd="16dp" | |
| android:layout_marginStart="16dp" | |
| android:layout_marginTop="8dp" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/head"> | |
| <android.support.design.widget.TextInputEditText | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:drawableStart="@drawable/user" | |
| android:drawablePadding="12dp" | |
| android:hint="@string/email" | |
| android:inputType="textEmailAddress" /> | |
| </android.support.design.widget.TextInputLayout> | |
| <android.support.design.widget.TextInputLayout | |
| android:id="@+id/login_password" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginEnd="16dp" | |
| android:layout_marginStart="16dp" | |
| android:layout_marginTop="16dp" | |
| app:passwordToggleEnabled="true" | |
| app:passwordToggleTint="@color/colorAccent" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintHorizontal_bias="0.0" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/login_email"> | |
| <android.support.design.widget.TextInputEditText | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:drawableStart="@drawable/pass" | |
| android:drawablePadding="12dp" | |
| android:hint="@string/password" | |
| android:inputType="textPassword" /> | |
| </android.support.design.widget.TextInputLayout> | |
| <TextView | |
| android:id="@+id/forgotpass" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_gravity="center" | |
| android:layout_marginEnd="16dp" | |
| android:layout_marginStart="16dp" | |
| android:layout_marginTop="8dp" | |
| android:text="@string/forgot_password" | |
| android:textAlignment="center" | |
| android:textColor="@color/colorhint" | |
| android:textSize="14sp" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/login_password" /> | |
| <Button | |
| android:id="@+id/login_button" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:layout_marginEnd="32dp" | |
| android:layout_marginStart="32dp" | |
| android:layout_marginTop="16dp" | |
| android:background="@drawable/rect1" | |
| android:gravity="center" | |
| android:padding="12dp" | |
| android:text="@string/sign_in" | |
| android:textColor="#fff" | |
| android:textSize="16sp" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/forgotpass" /> | |
| <TextView | |
| android:id="@+id/textView5" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:layout_marginEnd="16dp" | |
| android:layout_marginStart="16dp" | |
| android:layout_marginTop="8dp" | |
| android:text="@string/or" | |
| android:textAlignment="center" | |
| android:textColor="@color/colorhint" | |
| android:textSize="14sp" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/login_button" /> | |
| <LinearLayout | |
| android:id="@+id/login_via_google" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginBottom="16dp" | |
| android:layout_marginEnd="60dp" | |
| android:layout_marginStart="60dp" | |
| android:layout_marginTop="8dp" | |
| android:layout_weight="1" | |
| android:background="@drawable/rectgoogle" | |
| android:clickable="true" | |
| android:elevation="4dp" | |
| android:focusable="true" | |
| android:orientation="horizontal" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/textView5"> | |
| <ImageView | |
| android:id="@+id/login_via_google_image" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:contentDescription="@string/content_decription" | |
| android:padding="24dp" | |
| android:src="@drawable/google" /> | |
| </LinearLayout> | |
| </android.support.constraint.ConstraintLayout> | |
| </ScrollView> | |
| </android.support.constraint.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.job.darasastudent.ui; | |
| import android.content.ActivityNotFoundException; | |
| import android.content.Intent; | |
| import android.graphics.Color; | |
| import android.os.Bundle; | |
| import android.support.annotation.NonNull; | |
| import android.support.design.widget.Snackbar; | |
| import android.support.design.widget.TextInputLayout; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.widget.Button; | |
| import android.widget.LinearLayout; | |
| import android.widget.TextView; | |
| import com.google.android.gms.auth.api.signin.GoogleSignIn; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInAccount; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInClient; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInOptions; | |
| import com.google.android.gms.common.api.ApiException; | |
| import com.google.android.gms.tasks.OnCompleteListener; | |
| import com.google.android.gms.tasks.OnFailureListener; | |
| import com.google.android.gms.tasks.OnSuccessListener; | |
| import com.google.android.gms.tasks.Task; | |
| import com.google.firebase.auth.AuthCredential; | |
| import com.google.firebase.auth.AuthResult; | |
| import com.google.firebase.auth.FirebaseAuth; | |
| import com.google.firebase.auth.FirebaseUser; | |
| import com.google.firebase.auth.GoogleAuthProvider; | |
| import com.google.firebase.firestore.DocumentReference; | |
| import com.google.firebase.firestore.DocumentSnapshot; | |
| import com.google.firebase.firestore.FirebaseFirestore; | |
| import com.google.firebase.iid.FirebaseInstanceId; | |
| import com.job.darasastudent.R; | |
| import com.job.darasastudent.model.StudentDetails; | |
| import com.job.darasastudent.util.AppStatus; | |
| import com.job.darasastudent.util.DoSnack; | |
| import butterknife.BindView; | |
| import butterknife.ButterKnife; | |
| import butterknife.OnClick; | |
| import cn.pedant.SweetAlert.SweetAlertDialog; | |
| import static com.job.darasastudent.util.Constants.STUDENTDETAILSCOL; | |
| public class LoginActivity extends AppCompatActivity { | |
| @BindView(R.id.login_email) | |
| TextInputLayout loginEmail; | |
| @BindView(R.id.login_password) | |
| TextInputLayout loginPassword; | |
| @BindView(R.id.forgotpass) | |
| TextView forgotpass; | |
| @BindView(R.id.login_button) | |
| Button loginButton; | |
| @BindView(R.id.login_via_google) | |
| LinearLayout loginViaGoogle; | |
| private static final String TAG = "login"; | |
| public static final int RC_SIGN_IN = 1001; | |
| private FirebaseAuth mAuth; | |
| private FirebaseFirestore mFirestore; | |
| private DoSnack doSnack; | |
| private GoogleSignInClient mGoogleSignInClient; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.login_xml); | |
| ButterKnife.bind(this); | |
| //firebase | |
| mAuth = FirebaseAuth.getInstance(); | |
| mFirestore = FirebaseFirestore.getInstance(); | |
| // Configure Google Sign In | |
| GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
| .requestIdToken(getString(R.string.default_web_client_id)) | |
| .requestEmail() | |
| .build(); | |
| // Build a GoogleSignInClient with the options specified by gso. | |
| mGoogleSignInClient = GoogleSignIn.getClient(this, gso); | |
| doSnack = new DoSnack(this, LoginActivity.this); | |
| } | |
| @Override | |
| protected void onStart() { | |
| super.onStart(); | |
| if (FirebaseAuth.getInstance().getCurrentUser() != null) { | |
| sendToMain(); | |
| } | |
| } | |
| @OnClick(R.id.forgotpass) | |
| public void onForgotpassClicked() { | |
| final String email = loginEmail.getEditText().getText().toString(); | |
| if (email.isEmpty() || !isEmailValid(email)) { | |
| loginEmail.setError("enter valid email to reset password"); | |
| return; | |
| } else { | |
| loginEmail.setError(null); | |
| } | |
| mAuth.sendPasswordResetEmail(email) | |
| .addOnCompleteListener(new OnCompleteListener<Void>() { | |
| @Override | |
| public void onComplete(@NonNull Task<Void> task) { | |
| if (task.isSuccessful()) { | |
| Log.d(TAG, "Email sent."); | |
| doSnack.showSnackbar("Email sent to " + email, "Check", new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| Intent intent = new Intent(Intent.ACTION_MAIN); | |
| intent.addCategory(Intent.CATEGORY_APP_EMAIL); | |
| try { | |
| //startActivity(intent); | |
| startActivity(Intent.createChooser(intent, getString(R.string.chooseEmailClient))); | |
| } catch (ActivityNotFoundException e) { } | |
| } | |
| }); | |
| }else { | |
| doSnack.showShortSnackbar("You're not registered :("); | |
| } | |
| } | |
| }); | |
| } | |
| @OnClick(R.id.login_button) | |
| public void onLoginButtonClicked() { | |
| if (!AppStatus.getInstance(getApplicationContext()).isOnline()) { | |
| doSnack.showSnackbar("You're offline", "Retry", new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| onLoginButtonClicked(); | |
| } | |
| }); | |
| return; | |
| } | |
| String email = loginEmail.getEditText().getText().toString(); | |
| String password = loginPassword.getEditText().getText().toString(); | |
| if (validate()) { | |
| final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE); | |
| pDialog.getProgressHelper().setBarColor(Color.parseColor("#FF5521")); | |
| pDialog.setTitleText("Logging in..."); | |
| pDialog.setCancelable(false); | |
| pDialog.show(); | |
| mAuth.signInWithEmailAndPassword(email, password) | |
| .addOnCompleteListener(new OnCompleteListener<AuthResult>() { | |
| @Override | |
| public void onComplete(@NonNull Task<AuthResult> authtask) { | |
| if (authtask.isSuccessful()) { | |
| //login successful | |
| //update device token | |
| String devicetoken = FirebaseInstanceId.getInstance().getToken(); | |
| String mCurrentUserid = FirebaseAuth.getInstance().getCurrentUser().getUid(); | |
| // Set the value of 'Users' | |
| DocumentReference usersRef = mFirestore.collection(STUDENTDETAILSCOL).document(mCurrentUserid); | |
| usersRef.update("devicetoken", devicetoken) | |
| .addOnSuccessListener(new OnSuccessListener<Void>() { | |
| @Override | |
| public void onSuccess(Void aVoid) { | |
| pDialog.dismissWithAnimation(); | |
| sendToMain(); | |
| } | |
| }).addOnFailureListener(new OnFailureListener() { | |
| @Override | |
| public void onFailure(@NonNull Exception e) { | |
| pDialog.dismiss(); | |
| doSnack.errorPrompt("Oops...", e.getMessage()); | |
| } | |
| }); | |
| } else { | |
| pDialog.dismiss(); | |
| doSnack.UserAuthToastExceptions(authtask); | |
| } | |
| } | |
| }); | |
| } | |
| } | |
| @OnClick({R.id.login_via_google, R.id.login_via_google_image}) | |
| public void onLoginViaGoogleClicked() { | |
| if (!AppStatus.getInstance(getApplicationContext()).isOnline()) { | |
| doSnack.showSnackbar("You're offline", "Retry", new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| onLoginViaGoogleClicked(); | |
| } | |
| }); | |
| return; | |
| } | |
| Intent signInIntent = mGoogleSignInClient.getSignInIntent(); | |
| startActivityForResult(signInIntent, RC_SIGN_IN); | |
| loginViaGoogle.setEnabled(false); | |
| } | |
| private void sendToMain() { | |
| Intent mainIntent = new Intent(this, MainActivity.class); | |
| mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
| mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| startActivity(mainIntent); | |
| finish(); | |
| } | |
| private boolean validate() { | |
| boolean valid = true; | |
| String email = loginEmail.getEditText().getText().toString(); | |
| String password = loginPassword.getEditText().getText().toString(); | |
| if (email.isEmpty() || !isEmailValid(email)) { | |
| loginEmail.setError("enter valid email"); | |
| valid = false; | |
| } else { | |
| loginEmail.setError(null); | |
| } | |
| if (password.isEmpty() || password.length() < 6) { | |
| loginPassword.setError("at least 6 characters"); | |
| valid = false; | |
| } else { | |
| loginPassword.setError(null); | |
| } | |
| return valid; | |
| } | |
| private boolean isEmailValid(CharSequence email) { | |
| return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches(); | |
| } | |
| @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 { | |
| // Google Sign In was successful, authenticate with Firebase | |
| GoogleSignInAccount account = task.getResult(ApiException.class); | |
| firebaseAuthWithGoogle(account); | |
| } catch (ApiException e) { | |
| // Google Sign In failed, update UI appropriately | |
| Log.w(TAG, "Google sign in failed", e); | |
| // ... | |
| Snackbar.make(findViewById(android.R.id.content), "Google sign in failed", Snackbar.LENGTH_LONG).show(); | |
| loginViaGoogle.setEnabled(true); | |
| } | |
| } | |
| } | |
| private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { | |
| Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); | |
| final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE); | |
| pDialog.getProgressHelper().setBarColor(Color.parseColor("#FF5521")); | |
| pDialog.setTitleText("Logging in..."); | |
| pDialog.setCancelable(false); | |
| pDialog.show(); | |
| AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); | |
| mAuth.signInWithCredential(credential) | |
| .addOnCompleteListener(this, 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 | |
| Log.d(TAG, "signInWithCredential:success"); | |
| final FirebaseUser user = mAuth.getCurrentUser(); | |
| final String device_token = FirebaseInstanceId.getInstance().getToken(); | |
| final String mCurrentUserid = mAuth.getCurrentUser().getUid(); | |
| // refactor this not to write to DB each time...check if account exists | |
| DocumentReference docReference = mFirestore.collection(STUDENTDETAILSCOL).document(mCurrentUserid); | |
| docReference.get() | |
| .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { | |
| @Override | |
| public void onComplete(@NonNull Task<DocumentSnapshot> task) { | |
| if (task.isSuccessful()) { | |
| DocumentSnapshot document = task.getResult(); | |
| if (document.exists()) { | |
| Log.d(TAG, "DocumentSnapshot data: " + document.getData()); | |
| //update token only | |
| updateTokenOnly(mCurrentUserid, device_token, pDialog); | |
| } else { | |
| Log.d(TAG, "No such document"); | |
| //logging in with no pre account | |
| //region create fresh account | |
| pDialog.changeAlertType(SweetAlertDialog.SUCCESS_TYPE); | |
| pDialog.setTitleText("Account doesn't exists! \n Creating one..."); | |
| //write to db | |
| writingToStudUsers(pDialog, device_token, user, mCurrentUserid); | |
| //endregion | |
| } | |
| } else { | |
| Log.d(TAG, "get failed with ", task.getException()); | |
| } | |
| } | |
| }); | |
| } else { | |
| // If sign in fails, display a message to the user. | |
| pDialog.dismissWithAnimation(); | |
| Log.w(TAG, "signInWithCredential:failure", task.getException()); | |
| Snackbar.make(findViewById(android.R.id.content), "Authentication Failed.", Snackbar.LENGTH_SHORT).show(); | |
| loginViaGoogle.setEnabled(true); | |
| } | |
| } | |
| }); | |
| } | |
| private void writingToStudUsers(final SweetAlertDialog pDialog, String device_token, FirebaseUser user, String mCurrentUserid) { | |
| StudentDetails studentDetails = new StudentDetails(); | |
| studentDetails.setDevicetoken(device_token); | |
| studentDetails.setPhotourl(user.getPhotoUrl().toString()); | |
| studentDetails.setFirstname(user.getDisplayName()); | |
| studentDetails.setStudentid(mCurrentUserid); | |
| // Set the value of 'Users' | |
| DocumentReference usersRef = mFirestore.collection(STUDENTDETAILSCOL).document(mCurrentUserid); | |
| usersRef.set(studentDetails) | |
| .addOnSuccessListener(new OnSuccessListener<Void>() { | |
| @Override | |
| public void onSuccess(Void aVoid) { | |
| pDialog.dismissWithAnimation(); | |
| sendToAccountSetup(); | |
| } | |
| }).addOnFailureListener(new OnFailureListener() { | |
| @Override | |
| public void onFailure(@NonNull Exception e) { | |
| pDialog.dismiss(); | |
| doSnack.errorPrompt("Oops...", e.getMessage()); | |
| } | |
| }); | |
| } | |
| private void updateTokenOnly(String mCurrentUserid, String device_token, final SweetAlertDialog pDialog) { | |
| //update device token | |
| // Set the value of 'Users' | |
| DocumentReference usersRef = mFirestore.collection(STUDENTDETAILSCOL).document(mCurrentUserid); | |
| usersRef.update("devicetoken", device_token) | |
| .addOnSuccessListener(new OnSuccessListener<Void>() { | |
| @Override | |
| public void onSuccess(Void aVoid) { | |
| pDialog.dismissWithAnimation(); | |
| sendToMain(); | |
| } | |
| }).addOnFailureListener(new OnFailureListener() { | |
| @Override | |
| public void onFailure(@NonNull Exception e) { | |
| pDialog.dismiss(); | |
| doSnack.errorPrompt("Oops...", e.getMessage()); | |
| } | |
| }); | |
| } | |
| private void sendToAccountSetup() { | |
| Intent aIntent = new Intent(this, AccountSetupActivity.class); | |
| aIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
| aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| startActivity(aIntent); | |
| finish(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment