login and registration using firebase in android studio

 

User authentication using Firebase in Android:



firebase login with email and password:

1-Login Activity

public class loginActivity extends AppCompatActivity {
private EditText emailTV, passwordTV;
private Button loginBtn,regBtn;
private ProgressBar progressBar;

private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();

initializeUI();
if(LoginUtils.isLogin(loginActivity.this)){
Intent intent=new Intent(loginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else {
/* Intent intent=new Intent(loginActivity.this,RagistrationActivity.class);
startActivity(intent);*/
}

loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginUserAccount();
}
});
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// registerNewUser();
Intent intent=new Intent(loginActivity.this,RagistrationActivity.class);
startActivity(intent);
}
});
}

private void loginUserAccount() {
progressBar.setVisibility(View.VISIBLE);

String email, password;
email = emailTV.getText().toString();
password = passwordTV.getText().toString();

if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Please enter email...", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Please enter password!", Toast.LENGTH_LONG).show();
return;
}
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Login successful!", Toast.LENGTH_LONG).show();
SharedPreferenceUtils.getInstance(loginActivity.this).putBoolean(AppConstants.IS_USER_LOGIN, true);

progressBar.setVisibility(View.GONE);
emailTV.setText("");
passwordTV.setText("");
Intent intent = new Intent(loginActivity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Invalid credentials", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}

private void initializeUI() {
emailTV = findViewById(R.id.email);
passwordTV = findViewById(R.id.password);
regBtn = findViewById(R.id.register);
loginBtn = findViewById(R.id.login);
progressBar = findViewById(R.id.progressBar);
}
}
2-Login Activity XML code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="16dp">

<ImageView
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:src="@drawable/youtube" />
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email-ID"
app:boxStrokeColor="@color/colorPrimary"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="@color/colorPrimary"
app:hintTextColor="@color/black"
app:startIconDrawable="@drawable/email">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">

</com.google.android.material.textfield.TextInputEditText>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_marginTop="8dp"
app:boxStrokeColor="@color/colorPrimary"
app:boxStrokeWidthFocused="2dp"
app:endIconTint="@color/colorPrimary"
app:hintTextColor="@color/black"
app:passwordToggleEnabled="true"
app:startIconDrawable="@drawable/lock">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:transitionName="password_tran">

</com.google.android.material.textfield.TextInputEditText>

</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/rounded_corner8"
android:text="Log In"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/rounded_corner8"
android:text="Register"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
registration using firebase :
registration Activity :
public class registrationActivity extends AppCompatActivity {
private EditText emailTextView, passwordTextView;
private Button Btn23,Btn;
private ProgressBar progressbar;
private FirebaseAuth mAuth;
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login23);
mAuth = FirebaseAuth.getInstance();
Btn = findViewById(R.id.btnregister44);
// initialising all views through id defined above
emailTextView = findViewById(R.id.email23);
passwordTextView = findViewById(R.id.password23);
Btn23 = findViewById(R.id.login23);
progressbar = findViewById(R.id.progressBar23);
if(LoginUtils.isLogin(RagistrationActivity.this)){
    Intent intent=new Intent(RagistrationActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else {
/* Intent intent=new Intent(loginActivity.this,RagistrationActivity.class);
startActivity(intent);*/
}

Btn23.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
registerNewUser()
;
}
})
;
}
private void registerNewUser() {
// show the visibility of progress bar to show loading
progressbar.setVisibility(View.GONE);


// Take the value of two edit texts in Strings
String email, password;
email = emailTextView.getText().toString();
password = passwordTextView.getText().toString();

// Validations for input email and password
if (TextUtils.isEmpty(email)) {
Toast.
makeText(getApplicationContext(),
"Please enter email!!",
Toast.LENGTH_LONG)
.show()
;
return;
}
if (TextUtils.isEmpty(password)) {
Toast.
makeText(getApplicationContext(),
"Please enter password!!",
Toast.LENGTH_LONG)
.show()
;
return;
}
// create new user or register new user
mAuth
.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(
new OnCompleteListener<AuthResult>() {

@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if (task.isSuccessful()) {
Toast.
makeText(getApplicationContext(),
"Registration successful!",
Toast.LENGTH_LONG)
.show()
;



// hide the progress bar
progressbar.setVisibility(View.GONE);

SharedPreferenceUtils.getInstance(SignInActivity.this).putBoolean(AppConstants.IS_USER_LOGIN, true);
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
finish();

// if the user created intent to login activity

}
else {

// Registration failed
Toast.makeText(
getApplicationContext()
,
"Registration failed!!"
+ " Please try again later",
Toast.LENGTH_LONG)
.show()
;

// hide the progress bar
progressbar.setVisibility(View.GONE);
}
}
})
;

}
}
registration Activity Xml Code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Authenticate.SignInActivity">
<TextView
android:layout_width="match_parent"
android:textStyle="bold"
android:textSize="20sp"
android:layout_margin="15dp"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Register"/>

<!-- Edit text for email -->
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxStrokeColor="@color/teal_200"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:startIconDrawable="@drawable/email_24"
app:endIconTint="@color/teal_200"
app:hintTextColor="@color/black"
android:hint="Enter your Email"

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
>

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:id="@+id/email23"
android:layout_margin="10dp"
android:layout_height="wrap_content"
android:transitionName="username_tran"
android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>

<!-- Edit text for password -->
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColorHint="#B9B8B8"
app:passwordToggleEnabled="true">

<EditText
android:id="@+id/password23"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_margin="10dp"
android:inputType="textPassword"
android:padding="16dp"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/login23"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/shape"
android:text="Register" />

<Button
android:id="@+id/btnregister44"
android:layout_width="match_parent"
android:background="@drawable/shape"
android:layout_margin="10dp"
android:layout_height="wrap_content"
android:text="LogIn" />

<!-- ProgressBar for Loading Time -->
<ProgressBar
android:id="@+id/progressBar23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progress="25"
android:visibility="gone" />


</LinearLayout>
we store boolean  valeu on shared preferences ,in shared preference put Boolean method present,
we put true when user is successfully  complete registration;
3-AppConstants interface for store constemt valeu
public interface AppConstants {

/* Dashboard*/
public static final int APP_EXIT_TIME = 2000;

/*Font*/
public static final String FONT_ROBOTO_SEMIBOLD_TTF = "font/Roboto-Medium.ttf";

/*User*/
/* public static final String USER_ID = "Id";
public static final String USER_NAME = "Name";
public static final String USER_EMAIL = "Email";
public static final String VENDORID = "VendorID";
public static final String ENDUSERID = "EndUserID";
public static final String USERCITY = "City";
public static final String USER_CART_COUNT = "TotCartItems";
public static final String TOKEN = "token";
public static final String AndroidId = "androidid";
public static final String FILTERNAME = "filtername";/*

/*login*/login constent valeu store here
public static final String IS_USER_LOGIN = "is_user_login";
public static final String IS_USER_REGG = "is_user_Complete_reg";

public static final int WRITE_EXTERNAL_PERMISSION_REQUEST = 0x03;

}
if we want logout frome any Avtivity then we write this code on that particuler Activity
SharedPreferenceUtils.getInstance(MainActivity.this).clearALl();
Intent intent = new Intent(MainActivity.this, loginActivity.class);
startActivity(intent);
finish();
login utils class :
public class LoginUtils {
public static boolean isLogin(Context context) {
return SharedPreferenceUtils.getInstance(context).getSharedPreferences().getBoolean(AppConstants.IS_USER_LOGIN, false);
}

}

Rajeshbhatt12

My name is Rajesh Bhatt. I am working as a senior android developer . I have created this blog for kotlin ,java and Android Development interview questions etc..

Previous Post Next Post