Guest User

Untitled

a guest
Mar 31st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. private FirebaseAuth mAuth;
  2. private FirebaseAuth.AuthStateListener mAuthListener;
  3.  
  4. private String email;
  5. private String password;
  6.  
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_login);
  12.  
  13. final TextView emailEditText = (EditText) findViewById(R.id.emailField);
  14. final TextView passwordEditText = (EditText) findViewById(R.id.passwordField);
  15. Button loginButton = (Button) findViewById(R.id.loginButton);
  16. Button registerButton = (Button) findViewById(R.id.registerButton);
  17.  
  18. // Getting the firebase reference url
  19.  
  20. final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
  21. mAuth = FirebaseAuth.getInstance();
  22.  
  23.  
  24. loginButton.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27.  
  28. // Retrieves user inputs
  29. email = emailEditText.getText().toString();
  30. password = passwordEditText.getText().toString();
  31.  
  32. // trims the input
  33. email = email.trim();
  34. password = password.trim();
  35. // When a user signs in to your app, pass the user's email address and password to signInWithEmailAndPassword
  36. mAuth.signInWithEmailAndPassword(email, password)
  37. .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
  38. @Override
  39. public void onComplete(@NonNull Task<AuthResult> task) {
  40. Log.d("TAG", "signInWithEmail:onComplete:" + task.isSuccessful());
  41.  
  42. // If sign in fails, display a message to the user. If sign in succeeds
  43. // the auth state listener will be notified and logic to handle the
  44. // signed in user can be handled in the listener.
  45. if (!task.isSuccessful()) {
  46. Log.w("TAG", "signInWithEmail", task.getException());
  47. Toast.makeText(LoginActivity.this, "Authentication failed.",
  48. Toast.LENGTH_SHORT).show();
  49. }
  50. }
  51. });
  52.  
  53. }
  54. });
  55. // responds to changes in the user's sign-in state
  56. mAuthListener = new FirebaseAuth.AuthStateListener() {
  57. @Override
  58. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  59. FirebaseUser user = firebaseAuth.getCurrentUser();
  60. if (user != null) {
  61. // User is signed in
  62. Log.d("TAG", "onAuthStateChanged:signed_in:" + user.getUid());
  63.  
  64. // Authenticated successfully with authData
  65. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  66. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  67. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  68. startActivity(intent);
  69.  
  70. } else {
  71. // User is signed out
  72. Log.d("TAG", "onAuthStateChanged:signed_out");
  73. }
  74. }
  75. };
  76. registerButton.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View v) {
  79. startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
  80. }
  81. });
  82. }
  83. @Override
  84. public void onStart() {
  85. super.onStart();
  86. mAuth.addAuthStateListener(mAuthListener);
  87. }
  88.  
  89. @Override
  90. public void onStop() {
  91. super.onStop();
  92. if (mAuthListener != null) {
  93. mAuth.removeAuthStateListener(mAuthListener);
  94. }
  95. }
  96.  
  97. <application
  98. android:allowBackup="true"
  99. android:icon="@mipmap/ic_launcher"
  100. android:label="@string/app_name"
  101. android:supportsRtl="false"
  102. android:theme="@style/AppTheme">
  103. <activity android:name=".LoginActivity">
  104. <intent-filter>
  105. <action android:name="android.intent.action.MAIN" />
  106.  
  107. <category android:name="android.intent.category.LAUNCHER" />
  108. </intent-filter>
  109. </activity>
  110. <activity android:name=".MainActivity"></activity>
  111. <activity android:name=".RegisterActivity"></activity>
  112. </application>
Add Comment
Please, Sign In to add comment