Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private FirebaseAuth mAuth;
- private FirebaseAuth.AuthStateListener mAuthListener;
- private String email;
- private String password;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- final TextView emailEditText = (EditText) findViewById(R.id.emailField);
- final TextView passwordEditText = (EditText) findViewById(R.id.passwordField);
- Button loginButton = (Button) findViewById(R.id.loginButton);
- Button registerButton = (Button) findViewById(R.id.registerButton);
- // Getting the firebase reference url
- final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
- mAuth = FirebaseAuth.getInstance();
- loginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Retrieves user inputs
- email = emailEditText.getText().toString();
- password = passwordEditText.getText().toString();
- // trims the input
- email = email.trim();
- password = password.trim();
- // When a user signs in to your app, pass the user's email address and password to signInWithEmailAndPassword
- mAuth.signInWithEmailAndPassword(email, password)
- .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- Log.d("TAG", "signInWithEmail:onComplete:" + task.isSuccessful());
- // If sign in fails, display a message to the user. If sign in succeeds
- // the auth state listener will be notified and logic to handle the
- // signed in user can be handled in the listener.
- if (!task.isSuccessful()) {
- Log.w("TAG", "signInWithEmail", task.getException());
- Toast.makeText(LoginActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- });
- // responds to changes in the user's sign-in state
- mAuthListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = firebaseAuth.getCurrentUser();
- if (user != null) {
- // User is signed in
- Log.d("TAG", "onAuthStateChanged:signed_in:" + user.getUid());
- // Authenticated successfully with authData
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- } else {
- // User is signed out
- Log.d("TAG", "onAuthStateChanged:signed_out");
- }
- }
- };
- registerButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
- }
- });
- }
- @Override
- public void onStart() {
- super.onStart();
- mAuth.addAuthStateListener(mAuthListener);
- }
- @Override
- public void onStop() {
- super.onStop();
- if (mAuthListener != null) {
- mAuth.removeAuthStateListener(mAuthListener);
- }
- }
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="false"
- android:theme="@style/AppTheme">
- <activity android:name=".LoginActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".MainActivity"></activity>
- <activity android:name=".RegisterActivity"></activity>
- </application>
Add Comment
Please, Sign In to add comment