Firebase SDK: Authentication using email/password FIREBASE
- Firebase Tutorial
- Features of Firebase
- Android Studio
- Firebase - Environment Setup (with android studio)
- Firebase Assistance - Android Studio
- Firebase Authentication
- Firebase: Google Sign-In Authentication
- Firebase: Facebook Sign-In Authentication
- Firebase: Sign-In Authentication Using Phone Number
- Firebase UI: Authentication using email/password
- Firebase UI: Authentication using email link
- Firebase SDK: Authentication using email/password
- Firebase SDK: Authentication using the email link
- Firebase: Realtime Database
- Firebase: Real-time database setup and configuration
- Firebase: Data Organization in Real-time Database
- Firebase: Realtime Database Reading and Writing
- Firebase: Realtime Database Update and Delete
Firebase SDK: Authentication using email/password
Firebase SDK: Authentication using email/password
Firebase SDK is slightly the same as Firebase UI. Let's see the steps for implementing email/password authentication using Firebase SDK.
Step1:
In the first step, you have to perform the preliminary steps, which are as follows:
- Creating an Android project.
- Creating a Firebase project.
- Adding Firebase to the Android project or application, either manually or Firebase Assistance.
- Adding the required libraries and JSON files.
- Enable email/password sign-in method in Firebase console.
Step 2:
After performing the preliminary steps, let's create a register, login, and sign-out activities.
Step 3:
Now code for register activity. The code is as follows:
Register
- public class MainActivity extends AppCompatActivity {
- //Initialization section
- EditText email,pass,username;
- ProgressBar progressBar;
- String userid,Email;
- FirebaseAuth auth; //FirebaseAuth Instance
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- email=(EditText)findViewById(R.id.email);
- pass=(EditText)findViewById(R.id.password);
- username=(EditText)findViewById(R.id.username);
- progressBar=(ProgressBar)findViewById(R.id.progressBar);
- auth=FirebaseAuth.getInstance(); //Getting instance of FirebaseAuth
- }
- // Checking the current all state
- @Override
- public void onStart() {
- super.onStart();
- // if user logged in, go to sign-in screen
- if (auth.getCurrentUser() != null) {
- startActivity(new Intent(this, Sign_out.class));
- finish();
- }
- }
- @Override
- protected void onResume(){
- super.onResume();
- progressBar.setVisibility(View.GONE);
- }
- // Register button click
- public void onRegisterClicked(View view) {
- //Fetching data
- String emailInput = email.getText().toString().trim();
- String password = pass.getText().toString().trim();
- final String user = username.getText().toString().trim();
- //Validation check
- if (TextUtils.isEmpty(user)) {
- Toast.makeText(getApplicationContext(), "Enter username!", Toast.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(emailInput)) {
- Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(password)) {
- Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
- return;
- }
- if (password.length() < 6) {
- Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
- return;
- }
- progressBar.setVisibility(View.VISIBLE);
- //create user with email/password by adding complete listener
- auth.createUserWithEmailAndPassword(emailInput, password)
- .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
- progressBar.setVisibility(View.GONE);
- // 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()) {
- Toast.makeText(MainActivity.this, "Authentication failed." + task.getException(),
- Toast.LENGTH_LONG).show();
- Log.e("MyTag", task.getException().toString());
- } else {
- startActivity(new Intent(MainActivity.this, Sign_out.class));
- finish();
- }
- }
- });
- }
- //Login button click
- public void onLoginClicked(View view) {
- startActivity(new Intent(this, login.class));
- }
- }
Step 4:
After that, You need to code for login and sign-out activity, respectively. The codes are as follows
Login
- public class login extends AppCompatActivity {
- //Declaration section
- EditText email, password;
- ProgressBar progressBar;
- FirebaseAuth auth;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- email = (EditText) findViewById(R.id.email);
- password = (EditText) findViewById(R.id.password);
- progressBar = (ProgressBar) findViewById(R.id.progressBar);
- auth = FirebaseAuth.getInstance(); //Getting instances of FirebaseAuth
- }
- //Checking current user is logging or not
- @Override
- public void onStart() {
- super.onStart();
- // If user logged in, go to sign-in screen
- if (auth.getCurrentUser() != null) {
- startActivity(new Intent(this, Sign_out.class));
- finish();
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- progressBar.setVisibility(View.GONE);
- }
- //Login button click
- public void loginButtonClicked(View view) {
- String Email = email.getText().toString();
- final String pass = password.getText().toString();
- //Validation section
- if (TextUtils.isEmpty(Email)) {
- Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(pass)) {
- Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
- return;
- }
- progressBar.setVisibility(View.VISIBLE);
- if (password.length() < 6) {
- password.setError("Should be greater than 6");
- }
- //authenticate user with email/password by adding complete listener
- auth.signInWithEmailAndPassword(Email, pass)
- .addOnCompleteListener(login.this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (!task.isSuccessful()) {
- // there was an error
- Toast.makeText(login.this, "Authentication failed." + task.getException(),
- Toast.LENGTH_LONG).show();
- Log.e("MyTag", task.getException().toString());
- } else {
- Intent intent = new Intent(login.this, Sign_out.class);
- startActivity(intent);
- finish();
- }
- }
- });
- }
- }
Sign-out
- public class Sign_out extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_sign_out);
- }
- public void onLogout(View view){
- FirebaseAuth.getInstance().signOut();
- startActivity(new Intent(this,MainActivity.class));
- }
- }
Output: