Advertisement
Guest User

Untitled

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