In this tutorials, we can learn how to login android application using firebase cloud platform. After develop the application, first we have to know how to configure android application in firebase, which is the basic thing of any android application start using fire. please read the above link.
To add Firebase to your app you'll need a Firebase project and a Firebase configuration file for your app.
- Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
- Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
- When prompted, enter your app's package name. It's important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
- At the end, you'll download a google-services.json file. You can download this file again at any time.
- If you haven't done so already, copy this into your project's module folder, typically app/.
Then, in your module Gradle file (usually the app/build.gradle), add the authentication library in dependencies section.
com.google.firebase:firebase-auth:9.8.0
Now, In firebase, we know, there are many way to gives the user authentication like email/password, google, facebook, twitter, github and anonymous.
Lets start how to sign in using email/password.
After setting all the dependencies, you have to go project and left side there is Authentication section and go to sign-in method and enable the email/password section. Default it is disable, if you don't set the enable it won't work.
Now go to LoginActivity.java file,
Declare the FirebaseAuth and AuthStateListener objects.
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
In the onCreate() method, initialize the FirebaseAuth instance and the AuthStateListener method so you can track whenever the user signs in or out.
mAuth = FirebaseAuth.getInstance();
firebaseAuthListener= new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser=firebaseAuth.getCurrentUser();
if(firebaseUser!=null){
Log.e(TAG," onAuthStateChange: singed_in"+firebaseUser.getUid());
}else{
Log.e(TAG," onAuthStateChange: singed_out");
}
updateUI(firebaseUser);
}
};
Attach the listener to your FirebaseAuth instance in the onStart() method and remove it on onStop().
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(firebaseAuthListener);
}
@Override
protected void onStop() {
super.onStop();
if(mAuth!=null){
mAuth.addAuthStateListener(firebaseAuthListener);
}
}
For creating new user, we have to get the email and password from edittext and validates them and create new user with the createUserWithEmailAndPassword method.
// create new account
public void createNewAccount(String email, String password){
if(!validateForm()){
return;
}
showProgressDialog();
mAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(LoginActivity.this,"User Create Sucessuflly", Toast.LENGTH_SHORT).show();
}
hideProgressDialog();
}
});
}
For sign in existing user, get the email and password from edittext field and validates them and signs a user in with the signInWithEmailAndPassword method.
// sign in exising user
public void singIn(String email, String password){
if(!validateForm()){
return;
}
showProgressDialog();
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(LoginActivity.this,"User Login Failed", Toast.LENGTH_SHORT).show();
tvStatus.setText("Authentication Failed, Create New Account or Enter correct Credentials");
}
hideProgressDialog();
}
});
}
Yes, there is featues in firebase we can access the information about the signedIn user and there is a method getCurrentUser.
//geting user information
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// Name, email address.
String name = user.getDisplayName();
String email = user.getEmail();
}
Hope you will understand how to login using email/password.
Please if you want more information about go to firebase doc.
Happy Coding !!!