Guest User

Untitled

a guest
Dec 26th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. package pk.edu.pcsm.pcsm_auth;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.WindowManager;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.ProgressBar;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import com.google.android.gms.tasks.OnCompleteListener;
  17. import com.google.android.gms.tasks.OnFailureListener;
  18. import com.google.android.gms.tasks.Task;
  19. import com.google.firebase.auth.AuthResult;
  20. import com.google.firebase.auth.FirebaseAuth;
  21. import com.google.firebase.auth.FirebaseUser;
  22.  
  23.  
  24. public class LoginActivity extends AppCompatActivity {
  25.  
  26. private static final String TAG = "LoginActivity";
  27.  
  28. //Firebase
  29. private FirebaseAuth.AuthStateListener mAuthListener;
  30.  
  31.  
  32. // widgets
  33. private EditText mEmail, mPassword;
  34. private ProgressBar mProgressBar;
  35.  
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_login);
  40. mEmail = (EditText) findViewById(R.id.email);
  41. mPassword = (EditText) findViewById(R.id.password);
  42. mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
  43.  
  44. setupFirebaseAuth();
  45.  
  46. Button signIn = (Button) findViewById(R.id.email_sign_in_button);
  47. signIn.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View view) {
  50.  
  51. //check if the fields are filled out
  52. if (!isEmpty(mEmail.getText().toString())
  53. && !isEmpty(mPassword.getText().toString())) {
  54. Log.d(TAG, "onClick: attempting to authenticate.");
  55.  
  56. showDialog();
  57.  
  58. FirebaseAuth.getInstance().signInWithEmailAndPassword(mEmail.getText().toString(), mPassword.getText().toString())
  59. .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  60. @Override
  61. public void onComplete(@NonNull Task<AuthResult> task) {
  62. hideDialog();
  63.  
  64. if (task.isSuccessful()) {
  65. Timber.d("onPasswordEntered: linkWithCredential:success");
  66. FirebaseUser user = task.getResult().getUser();
  67. if (user.isEmailVerified()) {
  68. Log.d(TAG, "onAuthStateChanged: signed_in: " + user.getUid());
  69. Toast.makeText(LoginActivity.this, "Authenticated with: " + user.getEmail(), Toast.LENGTH_SHORT).show();
  70. Intent intent = new Intent(mContext, MainActivity.class);
  71. startActivity(intent);
  72. finish();
  73. }
  74. else {
  75. Toast.makeText(LoginActivity.this, "Check Your Email Inbox for a Verification link", Toast.LENGTH_SHORT).show();
  76. FirebaseAuth.getInstance().signOut();
  77. }
  78. }
  79. }
  80. }).addOnFailureListener(new OnFailureListener() {
  81. @Override
  82. public void onFailure(@NonNull Exception e) {
  83. Toast.makeText(LoginActivity.this, "Authentication Failed", Toast.LENGTH_SHORT).show();
  84. hideDialog();
  85. }
  86. });
  87.  
  88. } else {
  89. Toast.makeText(LoginActivity.this, "You didn't fill in all the fields.", Toast.LENGTH_SHORT).show();
  90. }
  91. }
  92. });
  93.  
  94. TextView register = (TextView) findViewById(R.id.link_register);
  95. register.setOnClickListener(new View.OnClickListener() {
  96. @Override
  97. public void onClick(View view) {
  98. Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  99. startActivity(intent);
  100. }
  101. });
  102.  
  103. TextView resetPassword = (TextView) findViewById(R.id.forgot_password);
  104. resetPassword.setOnClickListener(new View.OnClickListener() {
  105. @Override
  106. public void onClick(View v) {
  107.  
  108. }
  109. });
  110.  
  111. TextView resendEmailVerification = (TextView) findViewById(R.id.resend_verification_email);
  112. resendEmailVerification.setOnClickListener(new View.OnClickListener() {
  113. @Override
  114. public void onClick(View v) {
  115. ResendVerificationDialog dialog = new ResendVerificationDialog();
  116. dialog.show(getSupportFragmentManager(), "dialog_resend_email_verification");
  117. }
  118. });
  119.  
  120. hideSoftKeyboard();
  121.  
  122. }
  123.  
  124. /**
  125. * Return true if the @param is null
  126. *
  127. * @param string
  128. * @return
  129. */
  130. private boolean isEmpty(String string) {
  131. return string.equals("");
  132. }
  133.  
  134.  
  135. private void showDialog() {
  136. mProgressBar.setVisibility(View.VISIBLE);
  137.  
  138. }
  139.  
  140. private void hideDialog() {
  141. if (mProgressBar.getVisibility() == View.VISIBLE) {
  142. mProgressBar.setVisibility(View.INVISIBLE);
  143. }
  144. }
  145.  
  146. private void hideSoftKeyboard() {
  147. this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  148. }
  149.  
  150. /*
  151. ----------------------------- Firebase setup ---------------------------------
  152. */
  153.  
  154. private void setupFirebaseAuth() {
  155. Log.d(TAG, "setupFirebaseAuth: started.");
  156.  
  157. mAuthListener = new FirebaseAuth.AuthStateListener() {
  158. @Override
  159. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  160. FirebaseUser user = firebaseAuth.getCurrentUser();
  161. if (user != null) {
  162. Log.d(TAG, "onAuthStateChanged: signed_out: ");
  163.  
  164. }else {
  165. Log.d(TAG, "onAuthStateChanged: signed_out: ");
  166.  
  167. }
  168. }
  169. };
  170. }
  171.  
  172. @Override
  173. protected void onStart() {
  174. super.onStart();
  175. FirebaseAuth.getInstance().addAuthStateListener(mAuthListener);
  176. }
  177.  
  178. @Override
  179. protected void onStop() {
  180. super.onStop();
  181. if (mAuthListener != null) {
  182. FirebaseAuth.getInstance().removeAuthStateListener(mAuthListener);
  183. }
  184. }
  185. }
Add Comment
Please, Sign In to add comment