Email SignIn in Android App with help of FireBase



project Link - CLICKME
Email SignIn with help of FireBase




          what is firebase Auth?
Almost every app collects data of the user to give them access to use their app.there
are several reasons to collect data and creating user account one of the main reason to differentiate them and to give them access to what data they use they can save it.
so almost every have had different types of login. Some common examples of login are
login with facebook,loginwithG+, email login, signup with email, twitter login. So there are many auth providers in Android.
Firebase Auth is an Auth provider API by which you can set authentication in Android App by various Auth provides like facebook, twitter, G+, Gmail, email, GitHub.

email auth-
email auth in firebase by which you can set user login with the help of their email as
user write their email in firebase and click create an account then it sends a mail on your email to verify account and if you click on verify then account successfully verified.
Email Authentication is fully secured and all security feature provided by firebase.you can go on firebase doc  to view more about these methods in detail and you can customize it UI by your own way


            1. create 4 empty activity of name given below-

              1.MainActivity   (already made when creating a new project)
        2.LoginActivity
        3.RegisterActivity
        4.ForgetAndChangePasswordActivity  
 
      now their xml file also made

    
  * Then connect app with firebase and go to firebase console 
   and on email Signin
 
 
         2 now copy paste all xml and java code

                MainActivity.java 

package com.example.anuj.maddeveloper;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
Button btnSignOut;
FirebaseAuth auth;
FirebaseUser user;
ProgressDialog PD;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
user = auth.getCurrentUser();
PD = new ProgressDialog(this);
PD.setMessage("Loading...");
PD.setCancelable(true);
PD.setCanceledOnTouchOutside(false);
btnSignOut = (Button) findViewById(R.id.sign_out_button);
btnSignOut.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
auth.signOut();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}
}
};
}
});
findViewById(R.id.change_password_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), ForgetAndChangePasswordActivity.class).putExtra("Mode", 1));
}
});
findViewById(R.id.change_email_button).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), ForgetAndChangePasswordActivity.class).putExtra("Mode", 2));
}
});
findViewById(R.id.delete_user_button).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), ForgetAndChangePasswordActivity.class).putExtra("Mode", 3));
}
});
}
@Override protected void onResume() {
if (auth.getCurrentUser() == null) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}
super.onResume();
}
}


                   LoginActivity.java
package com.example.anuj.maddeveloper;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
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;
public class LoginActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private Button btnSignUp, btnLogin;
private ProgressDialog PD;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
PD = new ProgressDialog(this);
PD.setMessage("Loading...");
PD.setCancelable(true);
PD.setCanceledOnTouchOutside(false);
auth = FirebaseAuth.getInstance();
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnSignUp = (Button) findViewById(R.id.sign_up_button);
btnLogin = (Button) findViewById(R.id.sign_in_button);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
final String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
try {
if (password.length() > 0 && email.length() > 0) {
PD.show();
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(
LoginActivity.this,
"Authentication Failed",
Toast.LENGTH_LONG).show();
Log.v("error", task.getResult().toString());
} else {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
PD.dismiss();
}
});
} else {
Toast.makeText(
LoginActivity.this,
"Fill All Fields",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
findViewById(R.id.forget_password_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), ForgetAndChangePasswordActivity.class).putExtra("Mode", 0));
}
});
}
@Override protected void onResume() {
if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
super.onResume();
}
}


                            RegisterActivity.java
package com.example.anuj.maddeveloper;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
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;
public class RegisterActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private Button btnSignUp, btnLogin;
private ProgressDialog PD;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
PD = new ProgressDialog(this);
PD.setMessage("Loading...");
PD.setCancelable(true);
PD.setCanceledOnTouchOutside(false);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(RegisterActivity.this, MainActivity.class));
finish();
}
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnSignUp = (Button) findViewById(R.id.sign_up_button);
btnLogin = (Button) findViewById(R.id.sign_in_button);
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
final String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
try {
if (password.length() > 0 && email.length() > 0) {
PD.show();
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(
RegisterActivity.this,
"Authentication Failed",
Toast.LENGTH_LONG).show();
Log.v("error", task.getResult().toString());
} else {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
PD.dismiss();
}
});
} else {
Toast.makeText(
RegisterActivity.this,
"Fill All Fields",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
finish();
}
});
}
}


 
 
          forgetandchangepasswordactivity.java
      
package com.example.anuj.maddeveloper;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class ForgetAndChangePasswordActivity extends AppCompatActivity {
private EditText edtMode;
private TextView txtMode;
private Button submit;
private FirebaseAuth auth;
private ProgressDialog PD;
private TextInputLayout labelMode;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forget_and_change_password);
PD = new ProgressDialog(this);
PD.setMessage("Loading...");
PD.setCancelable(true);
PD.setCanceledOnTouchOutside(false);
auth = FirebaseAuth.getInstance();
edtMode = (EditText) findViewById(R.id.mode);
txtMode = (TextView) findViewById(R.id.title);
submit = (Button) findViewById(R.id.submit_button);
labelMode = (TextInputLayout) findViewById(R.id.label);
final int mode = getIntent().getIntExtra("Mode", 0);
if (mode == 0) {
txtMode.setText("Forget Password");
edtMode.setHint("Enter Registered Email");
labelMode.setHint("Enter Registered Email");
} else if (mode == 1) {
txtMode.setText("Change Password");
edtMode.setHint("Enter New Password");
labelMode.setHint("Enter New Password");
} else if (mode == 2) {
txtMode.setText("Change Email");
edtMode.setHint("Enter New Email");
labelMode.setHint("Enter New Email");
} else {
txtMode.setText("Delete User");
edtMode.setVisibility(View.GONE);
}
submit.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
callFunction(mode);
}
});
}
private void callFunction(int mode) {
FirebaseUser user = auth.getCurrentUser();
final String modeStr = edtMode.getText().toString();
if (mode == 0) {
if (TextUtils.isEmpty(modeStr)) {
edtMode.setError("Value Required");
} else {
PD.show();
auth.sendPasswordResetEmail(modeStr).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(ForgetAndChangePasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
}
PD.dismiss();
}
});
}
} else if (mode == 1) {
if (TextUtils.isEmpty(modeStr)) {
edtMode.setError("Value Required");
} else {
PD.show();
user.updatePassword(modeStr)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(ForgetAndChangePasswordActivity.this, "Password is updated!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to update password!", Toast.LENGTH_SHORT).show();
}
PD.dismiss();
}
});
}
} else if (mode == 2) {
if (TextUtils.isEmpty(modeStr)) {
edtMode.setError("Value Required");
} else {
PD.show();
user.updateEmail(modeStr)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(ForgetAndChangePasswordActivity.this, "Email address is updated.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to update email!", Toast.LENGTH_LONG).show();
}
PD.dismiss();
}
});
}
} else {
if (user != null) {
PD.show();
user.delete()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(ForgetAndChangePasswordActivity.this, "Your profile is deleted:( Create a account now!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to delete your account!", Toast.LENGTH_SHORT).show();
}
PD.dismiss();
}
});
}
}
}
}

                activity_forget_and_change_password.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.example.anuj.maddeveloper.ForgetAndChangePasswordActivity">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FireBase Change Password"
android:gravity="center"
android:textAllCaps="true"
android:textSize="16sp"
android:padding="10dp"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/label">
<EditText
android:id="@+id/mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mode"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/colorPrimary" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/submit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary"
android:text="Submit"
android:textColor="@color/white" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.example.anuj.maddeveloper.ForgetAndChangePasswordActivity">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FireBase Change Password"
android:gravity="center"
android:textAllCaps="true"
android:textSize="16sp"
android:padding="10dp"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/label">
<EditText
android:id="@+id/mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mode"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/colorPrimary" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/submit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary"
android:text="Submit"
android:textColor="@color/white" />
</LinearLayout>


             activity_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FireBase Auth Login"
android:gravity="center"
android:textAllCaps="true"
android:textSize="16sp"
android:padding="10dp"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/colorPrimary" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="Password"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/colorPrimary" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary"
android:text="Sign In"
android:textColor="@color/white" />
<!-- Link to Login Screen -->
<Button
android:id="@+id/sign_up_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimary"
android:text="Sign Up"
android:textColor="@color/white"
android:textStyle="bold" />
<!-- Link to Forget Password -->
<TextView
android:id="@+id/forget_password_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:padding="10dp"
android:text="Forget Password"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
</LinearLayout>






                           activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Firebase Auth Tutorial"
android:gravity="center"
android:textAllCaps="true"
android:textSize="16sp"
android:padding="10dp"/>
<Button
android:id="@+id/change_password_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary"
android:text="Change Password"
android:textColor="@color/white" />
<Button
android:id="@+id/change_email_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary"
android:text="Change Email"
android:textColor="@color/white" />
<Button
android:id="@+id/delete_user_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary"
android:text="Delete User"
android:textColor="@color/white" />
<Button
android:id="@+id/sign_out_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary"
android:text="Sign Out"
android:textColor="@color/white" />
</LinearLayout>





            activity_register.xmls
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FireBase Auth Register"
android:gravity="center"
android:textAllCaps="true"
android:textSize="16sp"
android:padding="10dp"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/colorPrimary" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="Password"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/colorPrimary" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/sign_up_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimary"
android:text="Sign Up"
android:textColor="@color/white"
android:textStyle="bold" />
<!-- Link to Login Screen -->
<Button
android:id="@+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorPrimary"
android:text="Sign In"
android:textColor="@color/white" />
</LinearLayout>


  
         AT LAST
   add values>colour.xml
    <color name="white">#ffffff</color>
 
 
and
build.grade(module app)
in dependency add-
       compile 'com.android.support:design:24.0.0' 

 

Post a Comment

10 Comments

  1. thanks alot for this info
    would like to know how to create a user profile

    ReplyDelete
  2. An ever increasing number of individuals are pulled in to these catching diversions every day. neko atsume tips

    ReplyDelete
  3. This is genuinely an awesome read for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome! Nights At Freddy'S 2 Mod Apk

    ReplyDelete
  4. We are the highly skillful team of Digital Advertising experts who promise to improve the reputation of your brand within a committed time. www.digiorbite.com

    ReplyDelete
  5. Thanks for sharing the useful blog about Email sign-in in Android App with the help of Firebase through your Examples and Source Code.

    Android App Development Company in Coimbatore

    ReplyDelete
  6. Thanks for sharing great post. I really like this post.

    Web Design Company in Anand

    ReplyDelete
  7. WOW wonderful this helped me alot in my project

    ReplyDelete
  8. please tel me about textlayout and where lable file created

    ReplyDelete
  9. Great tutorials, i follow this one, but its crashes all the times.
    I can't registrate, or forgottpassword.
    Please help.

    ReplyDelete