Created
July 1, 2018 16:46
-
-
Save JacquesInnocent/0ad0e586a9261bea184ce3764e92666f to your computer and use it in GitHub Desktop.
This file contains 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.example.roguex.nyumbaapp; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.google.android.gms.tasks.OnCompleteListener; | |
import com.google.android.gms.tasks.Task; | |
import com.google.firebase.auth.AuthResult; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.auth.FirebaseUser; | |
public class loginActivity extends AppCompatActivity { | |
private EditText mEmailField; | |
private EditText mPasswordField; | |
private Button mLoginBtn; | |
private FirebaseAuth mAuth; | |
private FirebaseAuth.AuthStateListener mAuthListener; | |
private String TAG; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
mAuth = FirebaseAuth.getInstance(); | |
mEmailField = findViewById(R.id.emailField); | |
mPasswordField = findViewById(R.id.passwordField); | |
mLoginBtn = findViewById(R.id.loginBtn); | |
mAuthListener = new FirebaseAuth.AuthStateListener() { | |
@Override | |
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | |
if (firebaseAuth.getCurrentUser() != null){ | |
startActivity(new Intent(loginActivity.this, MainActivity.class)); | |
} | |
} | |
}; | |
mLoginBtn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
startSignIn(); | |
} | |
}); | |
} | |
/** | |
@Override | |
private void onStart(){ | |
super.onStart(); | |
mAuth.addAuthStateListener(mAuthListener); | |
} | |
**/ | |
private void startSignIn() { | |
String email = mEmailField.getText().toString(); | |
String password = mPasswordField.getText().toString(); | |
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) { | |
Toast.makeText(loginActivity.this, "Input fields are empty", Toast.LENGTH_SHORT).show(); | |
} else { | |
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(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, "createUserWithEmail:success"); | |
FirebaseUser user = mAuth.getCurrentUser(); | |
updateUI(user); | |
Toast.makeText(loginActivity.this, "Authentication Failure", Toast.LENGTH_SHORT).show(); | |
}else { | |
// If sign in fails, display a message to the user. | |
Log.d(TAG, "createUserWithEmail:failure", task.getException()); | |
Toast.makeText(loginActivity.this, "Authentication Failed.", Toast.LENGTH_SHORT).show(); | |
updateUI(null); | |
} | |
} | |
}); | |
} | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
// Check if user is signed in (non-null) and update UI accordingly. | |
FirebaseUser currentUser = mAuth.getCurrentUser(); | |
updateUI(currentUser); | |
} | |
private void updateUI(FirebaseUser currentUser) { | |
} | |
} | |
//Put these in the build.gradle (Module:app) | |
//implementation 'com.google.firebase:firebase-auth:11.6.0' | |
//implementation 'com.google.firebase:firebase-core:11.6.0' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment