Firebase Authentication

Google FireBase Auth

Hey Guys this is my First Post And I am Going to Cover The Most Trending Topic Google Firebase And I am gonna tell You Everything You need to Know About Firebase 

Lets Start with FireBase Authentication
In This We will Cover Login Register And LogOut By using FireBase Authentication 

Soo Firstly Add this Firebase to Your Project:- 

sign in with your google account and get started for free
click on create new project give it a awesome name and select the region
now click on  Add Firebase to your Android app

Enter Your Package name and leave the other fields sha key not necessary  for learning purpose 

now copy the config file to your app directory  

add this library to your project level gradle
classpath 'com.google.gms:google-services:3.0.0'

and this one to your app level gradle at end of your page

apply plugin: 'com.google.gms.google-services'
now go to your app level gradle and add this dependency for Firebase Auth
compile ‘com.google.firebase:firebase-auth:9.2.1’ 
Add Internet Permissions in Android MainFest

 <uses-permission android:name="android.permission.INTERNET"/>  

now in The firebase console go to authentication  and  enable email password authentication in Sign in methods.

Lets Code  now…


Now we will create a Layouts

Register Ui :-

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
tools:context="com.cursedcompany.androidclassroomauth.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email"
android:hint="email..."/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:hint="password..."
android:layout_below="@+id/email"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:layout_below="@+id/password"
android:id="@+id/Register_btn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/goto_login"
android:layout_centerHorizontal="true"
style="@style/Base.TextAppearance.AppCompat.Headline"
android:text="Goto Login Page "
android:layout_alignParentBottom="true"/>
</RelativeLayout>

Now Lets Create Login Ui

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context="com.cursedcompany.androidclassroomauth.LoginActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/log_email"
android:hint="email..."/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/log_password"
android:hint="password..."
android:layout_below="@+id/log_email"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@+id/log_password"
android:id="@+id/login_btn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/goto_register"
android:layout_centerHorizontal="true"
style="@style/Base.TextAppearance.AppCompat.Headline"
android:text="Goto Register Page "
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Now Lets Create Create Acount Page Layout
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_acount_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cursedcompany.androidclassroomauth.AcountPage">
<Button
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/logout"
android:text="Logout"/>
</RelativeLayout>
our Layout is Complete So lets first code MainActivity To RegisterUser
MainActivity:
 public class MainActivity extends AppCompatActivity {  
private EditText mEmailEdit, mPasswordEdit;
private Button mRegister;
private TextView gotoLogin;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEmailEdit = (EditText) findViewById(R.id.email);
mPasswordEdit = (EditText) findViewById(R.id.password);
mRegister = (Button) findViewById(R.id.Register_btn);
gotoLogin = (TextView) findViewById(R.id.goto_login);
mAuth = FirebaseAuth.getInstance();
mRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmailEdit.getText().toString().trim();
String password = mPasswordEdit.getText().toString().trim();
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(MainActivity.this, AcountPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG).show();
}
}
});
}
}
});
}

So To Register A User We need To Create A FirebaseAuth Instance on Oncreate 
Than to register the User We call A FireBaseAuth Method createUserWithEmailAndPasssword(String email , String password )
it Require two Strings email and password 
hurray!! We have Succesfully Registered..
Now Time To Use FireBase Login feature With Existing User
Now Lets Code Login Activity :
LoginActivity

 public class LoginActivity extends AppCompatActivity {  
private EditText mEmailEdit, mPasswordEdit;
private Button mLogin;
private TextView gotoReg;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEmailEdit = (EditText) findViewById(R.id.log_email);
mPasswordEdit = (EditText) findViewById(R.id.log_password);
mLogin = (Button) findViewById(R.id.login_btn);
gotoReg = (TextView) findViewById(R.id.goto_register);
mAuth = FirebaseAuth.getInstance();
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmailEdit.getText().toString().trim();
String password = mPasswordEdit.getText().toString().trim();
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(LoginActivity.this, AcountPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG).show();
}
}
});
}
}
});
}
}

So Here We Created FirebaseAuth Instance and used method *SignInWithEmailAndPassword(String email , String Password ) it requires two Strings email and password To Log In 
Now Lets Finish It By Adding Our Last Feature LOG OUT 
let Code Account Activity
AcountPage:

 public class AcountPage extends AppCompatActivity {  
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acount_page);
mAuth = FirebaseAuth.getInstance();
Button SignOut = (Button) findViewById(R.id.logout);
SignOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mAuth.getCurrentUser() != null) {
mAuth.signOut();
}
}
});
}
}

  
Soo Here We Created FirebaseAuth Instance And then Checked if The user Exist or not and if user Exist then we Signed Him Out by Using Auth.SignOut() Method…
Soo this Part Finishes here 
Thanks Guys For Reading the Blog If you have Any Issue comment it below i will Surely give You Solution
See you in next Tutorial 

Leave a Reply

Your email address will not be published. Required fields are marked *