All about Firebase Authentication 🔥 | Login & Signup | Jetpack Compose
Summary
TLDRThis video tutorial guides viewers on integrating Firebase authentication into an Android application. It covers creating a login and signup page, setting up seamless navigation between screens, and using a shared ViewModel to manage authentication status. The tutorial also demonstrates how to connect the app to Firebase, handle user registration, login, and logout processes, ensuring a smooth user experience with real-time UI updates.
Takeaways
- 🚀 The video tutorial covers integrating Firebase Authentication into an Android application, guiding through the creation of a login and signup page.
- 📱 It introduces the process of setting up a new Android Studio project with an empty activity and configuring it for Firebase integration.
- 🔒 The tutorial explains the concept of a ViewModel in Android, which is used to manage authentication status across different screens.
- 📝 It details the creation of methods within the ViewModel to handle authentication checks, login, signup, and logout functionalities.
- 🔄 The script discusses navigating between screens in the application, using a navigation controller to manage the flow.
- 📚 The importance of adding Firebase Authentication library to handle user authentication is emphasized, abstracting the complexity from the application code.
- 🔗 The process of connecting the Android application to Firebase and enabling email and password authentication in the Firebase console is outlined.
- 🛠️ The tutorial provides step-by-step instructions for designing the UI for login and signup pages, including input fields for email and password, and buttons for user actions.
- 🔑 The script explains how to implement the login and signup functionality by calling Firebase methods and handling the response to update the UI accordingly.
- ❗️ It highlights the importance of error handling during the authentication process, showing messages to the user when authentication fails.
- 🔄 The tutorial concludes with the implementation of the sign-out functionality, demonstrating how to return the user to the login page upon successful logout.
Q & A
What is the main topic of the video?
-The main topic of the video is integrating Firebase authentication into an Android application, including creating login and signup pages.
What are the three screens mentioned for the application architecture?
-The three screens mentioned are the signup page, homepage, and login page.
What is the purpose of the 'AuthViewModel' in the application?
-The 'AuthViewModel' is used to manage authentication status, provide methods for checking login status, logging in, signing up, and signing out, and to handle navigation between different screens based on the authentication state.
How does the application check if a user is already logged in?
-The application checks if a user is already logged in by using the 'checkAuthStatus' method in the 'AuthViewModel', which checks the current user state from Firebase authentication.
What is the role of the 'NavController' in the navigation flow?
-The 'NavController' is responsible for navigating between different screens of the application based on the user's authentication state and interactions, such as signing in or signing up.
How does the video script guide the implementation of the login page?
-The script guides the implementation of the login page by detailing the creation of UI elements like email and password input fields, a login button, and a text button to navigate to the signup page.
What is the significance of the 'AuState' sealed class in the ViewModel?
-The 'AuState' sealed class is used to represent different authentication states such as authenticated, unauthenticated, loading, and error, which helps in updating the UI accordingly.
How is the sign-up process implemented in the application?
-The sign-up process is implemented by calling the 'signup' method in the 'AuthViewModel' with the user's email and password, which then interacts with Firebase authentication to create a new user account.
What is the purpose of the 'signOut' method in the ViewModel?
-The 'signOut' method is used to sign out the current user by calling Firebase's sign-out function, which then updates the authentication state to unauthenticated, prompting navigation back to the login page.
How does the script handle user input validation for the login and signup processes?
-The script checks if the email and password fields are empty before attempting to log in or sign up. If they are empty, it prevents the login or signup process and can display an error message to the user.
What additional feature is implemented to enhance the user experience during the login and signup processes?
-The script implements a feature that disables the login and signup buttons while the authentication process is in the 'loading' state, preventing multiple submissions and providing a better user experience.
Outlines
🛠️ Setting Up Firebase Authentication in Android Studio
The video begins with an introduction to integrating Firebase authentication into an Android application. It covers creating a new project in Android Studio with Jetpack Compose Activity, naming the application 'Firebase Auth demo app', and setting the minimum SDK to Android 8.0. The architecture involves three screens: a sign-up page, a home page, and a login page. The video explains the navigation flow and the use of a shared ViewModel to manage authentication status across screens. It also outlines the process of checking user login status and navigating to the home page if the user is already authenticated, or prompting them to log in or sign up otherwise.
📱 Creating Navigation and UI Components
This paragraph delves into the creation of navigation within the app. It describes setting up a new package for pages and creating composable files for the home, login, and sign-up pages. The navigation is established using a NavController and a NavHost composable, with the start destination set to the login page. The video also covers passing the NavController and ViewModel to each page for navigation purposes. The process of designing the login page UI with email and password fields, a login button, and a text button to navigate to the sign-up page is detailed, including the use of modifiers for layout and spacing.
🔄 Implementing Sign-Up and Login Flows
The script explains the process of moving to the sign-up page from the login page, and the need to implement similar UI components in the sign-up page as in the login page. It discusses the implementation of the login and sign-up functionality by connecting the application to Firebase, enabling email and password authentication, and adding the necessary Firebase authentication library. The creation of methods within the AuthViewModel for checking authentication status, logging in, and signing up is outlined, including the handling of different states such as authenticated, unauthenticated, loading, and error with messages.
🔒 Handling Authentication States and UI Updates
The paragraph focuses on handling different authentication states in the UI. It describes the creation of a sealed class 'AuthState' to represent the authentication status and the use of 'MutableLiveData' to expose the current auth state to the UI. The video script outlines the methods for checking auth status, logging in, and signing up, including setting the auth state to loading before performing operations and updating the state based on the operation's success or failure. It also covers the implementation of the sign-out functionality and how it affects the auth state and UI navigation.
🏠 Designing the Home Page and Implementing Sign-Out
The final paragraph discusses the design of the home page, which includes a simple text indicating the home page and a sign-out button. It explains how to observe the auth state in the home page composable and navigate to the login page if the user is unauthenticated. The paragraph also details the process of signing out by calling the sign-out method from the ViewModel, which updates the auth state to unauthenticated, triggering a navigation back to the login page. Additionally, it covers the implementation of UI feedback for the loading state during sign-up and login operations, such as disabling buttons during these processes.
Mindmap
Keywords
💡Firebase Authentication
💡Android Studio
💡Jetpack Compose
💡ViewModel
💡Navigation
💡LiveData
💡Sealed Class
💡Sign Up
💡Log In
💡Sign Out
💡Composable
Highlights
Introduction to integrating Firebase authentication in an Android application.
Creating a login and signup page for seamless user authentication.
Setting up a new Android Studio project with Firebase Auth demo app.
Understanding the app architecture with three main screens: signup, homepage, and login.
Creating a shared ViewModel for authentication status across all pages.
Implementing a method to check the authentication status using ViewModel.
Direct navigation to the homepage if the user is already authenticated.
Handling login with email and password through Firebase authentication.
Managing login success and failure responses in the application.
Navigating from the login page to the signup page and vice versa.
Creating a sign up method in ViewModel to handle user registration.
Adding a logout functionality to return users to the login page.
Building the UI for the login page with email, password fields, and buttons.
Implementing navigation between different pages using NavController.
Connecting the Android application to Firebase for authentication.
Enabling email and password authentication in the Firebase console.
Creating methods in ViewModel for login, signup, and logout functionalities.
Using LiveData to observe authentication state changes in the UI.
Designing the homepage with a sign-out button to end user sessions.
Handling UI updates based on the authentication state from ViewModel.
Testing the complete flow from signup, login, homepage navigation, to logout.
Ensuring the login button is disabled during the authentication process.
Conclusion and invitation for feedback on integrating Firebase authentication.
Transcripts
what is going on guys in this video we
are going to learn how to integrate
Firebase authentication in our
application we'll create login page
we'll create signup page and then we'll
integrate the Firebase authentication so
that we can do a seamless authentication
in our application we'll also learn
about how we can navigate from one
screen to another screen we'll learn lot
of things from this single video so
let's get started with new Android
Studio
project so click on new project select
empty activity that is Jetpack compos
Activity Click on next give the
application let's give Firebase Au demo
app minimum hdk Let It Be Android 8.0
build configuration language Let It Be
recommended one click on
finish so till our project loads let's
understand the architecture so here I
will give the name Firebase author demo
app and let's select canvas now for this
application we will have three screen so
first screen will be let's say sign up
page so that user can create the account
with the Firebase second screen will be
homepage and third page will be login
page so if user has already created the
account they can use the login so in our
application there will be these three
pages so what we will do whenever our
app launches let's say app launch from
here it will first go to login page then
we'll check for something either user
has already logged in or not so how we
can check we'll create a view model that
we will call as o view model so that we
can use it for all three pages so here
we will put off view model now in this o
View model we will create some methods
let's say first method will be check Au
status you can give any name so it will
check either we are already logged in or
not if we are already logged in then it
will return something so first method
will be check Au status so what login
page will do it will first initialize
this view model so that we can check the
status if it is logged in if it is
logged in then directly it will go to
homepage so here we can say if we are
authenticated that means we are already
logged in then it will go to homepage if
we are not logged in then what we will
do we will call another method here that
will be login so here we'll provide
email and password so that we can login
so from here we'll call login method and
then from here we will get some response
either it can be success or failure so
we can write success or failure here if
we get the success it will be
authenticated and it will go to homepage
if we get failure we can show some
message so we can say failure with
message so how we'll get this success
and failure from off model we'll call
the functions of Firebase We'll add
library for the Firebase that will be
Firebase authentication that will handle
everything so here we will have the
library code that will be Firebase so
from here it will automatically call the
Firebase
functions so we don't have to worry
about anything this will call the
Firebase function and it will return the
data from firas from view model we have
to call the library code so here we will
call let's say sign up or login and it
will simply return success or F failure
that's it so this is the task of view
model so from login we do login it will
go to fireb Au if it is Success then it
will send success to login page we'll
navigate to homepage if it is failure
we'll show the message in the login page
itself so this is how login flow works
now in login page we'll create a button
which will say if you have not created
account yet you can sign up so we'll go
to sign up page from the login page now
from sign up page also same thing we
will do we'll call the method of off viw
model that will create over here sign up
with email and password so it will call
sign up and from here we'll get the
response that can be success or failure
with message same thing so whenever we
are authenticated from sign up page
we'll go to homepage again authenticated
if it is failure it will show the
message now we are completed with the
login flow we go to login page check the
authentication status if it is already
logged in then we'll go to homepage
directly if it is not logged in user can
login or user can sign up so if it is
authenticated from both side it will
come to homepage now we have to add the
log out button also so here what we will
do in view model we'll create another
method that will be sign
out it will be simply sign out and from
homepage we'll call that sign out method
so here sign out so whenever we did the
sign out that means we are
unauthenticated then again it will go to
login
page let's move it here so if
unauthenticated it will go again to
login page so in this way our complete
application will work we don't have to
worry about these things these things
will be handled by Library we just have
to call from our Au view model that's it
so now let's just start implementing it
in the code let's get back to the
Android Studio project our project is
created let's run the application and
see either it is created correctly or
not so as you can see we have this hello
Android that means it is created
correctly so let's get back to the
project and say hello to this hello
world and let's delete this let's delete
this greeting as well so here we will
add our own composable so first thing
what we will do we will create the
navigation so what we will do let's go
to our package here I'll create new
package let's say pages and inside pages
I will create three file that will be
homepage and here I will create
composable home page I create second
page that will be login page
file
composable login page and I will create
third page that will be sign up page now
these will be our Pages now these will
be our Pages outside this pages I will
create navigation so I will call it as
my app navigation so that we can
correctly navigate let's call it
as let's create composable my app
navigation so here what we will do we
will create a navigation controller and
we'll call each Pages if you don't know
how to create the navigation you can
watch the complete video in the channel
that is about navigation so here also we
are doing the same thing first we'll
create a nav controller remember nav nav
controller so let's write remember nav
controller so we need to add the
dependency if you hover over here let's
add this dependency and import it will
take some time so our dependency is
added now we have to create a nav host
composable so nav host what it will take
it will take nap controller that is our
nap controller and instead of graph we
will do start destination so it will
take a string and then third parameter
will be Builder will return composable
here here is start destination if you go
to our diagram here whenever app
launches we should go to login page so
start destination let's create as login
and here we'll create composable route
will will login if it is login then
we'll return composable that is our
login page we have created we'll pass
the modifier from here that's it so in
this way if it is login we'll go to
login page now here we have to navigate
from login page to homepage as well sign
up page as well so we require nap
controller so we can pass nap controller
over here also we'll require view model
so let's create view model first let's
call it as o view
model class and it will extend from view
model for now we won't write anything so
here we will take from Main activity
that will be o view model and also pass
off view model over here that means that
means in the login page we'll require
three parameter so second will be nap
controller third will be off view model
same way we will do for all three so
homepage
also and sign up page
also now if you go to my app navigation
error will be gone so let's copy this
and create other composable that will be
sign up it will go to sign up page and
homepage it will be home that's it our
navigation is complete at the launch of
the app we'll go to login page and we
can go to sign up and homepage from this
NAB controller we have passed to every
page now we can go to main activity and
from here we can call that my app
navigation and off view model will be we
have to create a view model over here Au
view model let's say which will be of
type Au view model by view models now
here we can pass Au view model Also
let's pass this modifier C to modifier
do padding will be inner padding that's
it that's it from Main activity will go
to my app navigation from my app
navigation we'll go to login page so we
need to design this login page if you
run the app now there will be nothing
because we have not written anything in
login page now we'll start designing
this so let's keep it side by side so
here I will put it now first thing I
will do I will create a column and give
the modifier equals to modifier do
filmax size vertical Arrangement will be
arrangement. Center horizontal alignment
will be alignment. Center horizontally
and here I will create first a text
let's say login page and font size will
be 32p let's import this let's give a
spacer modifier. height about 16 DP
import this DP as well and below this I
will create a text field so out outline
text field value and on value change
let's create this for email so I'll
create variable email by remember
mutable State off I'll initialize it
with empty import import so here in
value I'll pass email on value change
email equals to it we can put it on
separate
line and third thing I will pass is
label so that we can identify so here
text composable that will be email
that's it if you apply changes now you
can see we have login page and we have
the email text field so we can type here
email also I'll give you space over here
as well so after this outline text field
about ad DP and create another outline
text field can paste it let's call it as
password let's create a variable for
that as well here password now here
value will be password and on value
change password equals to it label will
be password now again spacer let's keep
about 16 DP and then below this we'll
create a button button text will be
login on click we'll do something right
now let's put it empty and let's give
spacer again ad DP if you rerun you can
see we have email password and login now
I'll create a text button so that we can
go to sign up page so text button on
click we won't do anything text will be
don't have an account here we can write
sign up and on click we'll do something
if you run it you can see we have this
button as well so this is our login page
we can type the email we can type the
password password we can do login and we
can go to sign up page so first thing
what we will do from here we have to
move to sign up page so here in text
button on click what I will do
navcontroller do navigate we can pass
the route that is sign up so whenever we
click on this we'll go to signup page if
I click I in sign up page it is empty
because we have not written anything
over there so what I will do I'll copy
everything from this login page and
paste it in sign up page as well it will
be similar design let let's change
everything from here email and password
is required text will be sign up page
email will be email password will be
password and button will be let's say
create account text button will be
already have an account if so go to
login page and navigate will be from
sign up to login page if I run now if I
go to sign up page it will be sign up
page if I go to login page it will be
login page so in this way it will work
now what we have to do we have to
implement the login and sign up so how
we can do that if you see the diagram in
AU view model we have to create four
methods and then we have to call the
Firebase so let's connect our
application to the Firebase first so for
that what we can do go to
tools Firebase you can see a lot of
option from authentication and you can
select any authentication system let's
say custom authentication and connect to
Firebase so it will open in the browser
you can click on ADD
project so here you can see the name is
automatically Ally detected you can
click on continue you can enable or
disable this analytics create
project so our project is ready click on
continue click on connect send so you
can see your endro project is connected
if you go back here you can see in green
it is connected now we have to add the
authentication Library click on it it
will add the library for Google service
and
authentication so once it is done you
can close this and you can go to the
console console.
Firebase and you can open your
application you have to enable the
authentication let's go to build
authentication get started here you can
see email and password click on this
enable it and save it so our email and
password is enabled in the Firebase and
Firebase is connected now we can use the
library code to create the account and
do the login so what we will do we'll go
to Au view model and create those
methods so what will be the methods so
to access the authentication we require
Firebase o so let's create a variable o
it will be of type Firebase o and we'll
get it from Firebase o. get instance now
we'll create the Au hisory so that we
can get to know either we are
authenticated either we are
unauthenticated or we have got the
failure with message so this thing we
have to identify in the UI so for that I
will create a seal class so sealed class
let's say Au State this is our own
implementation so here we'll create
first object as authentic iated which
will be of type Au state so that we can
get to know in which state we are second
will be unauthenticated Au State third
we can say whenever we are doing login
it will take some time to load from
Firebase whenever we click on login or
sign up it will take some time to do the
login and sign up so we can call it as
loading state it will be of type same Au
State and the last thing will be failure
with message so for that we have to pass
message as well so I will create a data
class instead of object which we call as
error and we will have message in it as
string so it will be of type same Au
State now we have four type of State
either we are authenticated then we'll
be in homepage if we are unauthenticated
we will be in login page and if we have
error we'll show the message that's it
so here I will create the variable for
that private well Au state which will be
of type mutable live data Au State
generic now we have to expose it to UI
so this will be private We'll add
underscore on this and exposing variable
will be Au State without underscore
which will of type live data of au State
and it will return this Au State now we
have created the variable this we can
use in the UI so first thing what we
will do we'll create this method check
Au status so check Au status we'll check
either we are logged in or not so
function check Au status here firas
Library we can use au. current user if
there is current user that means it is
authenticated if it is not there that
means it is unauthenticated so if equals
to equal to null we can say underscore
auate do value equals to auth State DOT
we can say this unauthenticated if it is
not null then it is
authenticated so here we'll call this on
init of the view model that is check
auth status every time when we launch
the activity it will check for this
that's it one method is done now second
method is login so we'll create another
method here let's minimize this function
login here we will create email string
password
string as I have said from view model
we'll call the methods of Firebase Au so
Au provide up some method so Au do sign
in with email and password you can see
over here you can pass email and
password and it will take some time to
complete so what we will do we will add
listener that will be add on complete
listener so whenever it is completed
fire will provide us some task so the
with this task we can check either it is
success or not so we can check if task
do is successful that means we have
logged in else we have some error
message so before doing this login what
we will do first we'll set it to loading
so here Au state. value equals to auate
do loading so whenever we do login it
will be first loading then whenever it
will be success we'll say auate do value
equals to Au state. authenticated and if
it is failed we'll set Au state. Val
equals to we'll set this error auate do
error and we have to provide the message
the message we'll get from this task
itself so task if we have exception do
exception and from that we can get
message so it can be null so message if
it is null will provide a generic
message that will be something went
wrong in this way whenever we click on
login button we'll call this method it
will set it to loading and then it will
try to sign in if it is successful it
will say authenticated if it is failed
it will show the message before calling
this method what we can do we can check
if email is empty or if password do is
empty if it is we can we should not go
to this so what we can do here we can
copy this error message paste it over
here delete this and write here email or
password can't be empty so that we'll
not get the exception and we'll directly
return from here if these values are
empty we don't have to do login so in
this way our login function is also
completed so here we can do it is
completed now we'll create sign up
method same thing we have to do let's
copy this everything paste it below
let's call it as sign up and if it is a
we will show the empty loading will be
loading and off method this sign in with
email and password what we will do we
have another method create user with
email and password you can see over here
create user with email and password in
sign up we have to call this method if
it is successful it will show
authenticated if it is error it will
show the message that's it in this way
we have created this sign up method as
well now we have to create the sign out
so here what we'll do function sign out
we don't have to pass anything we have
to do Au do sign out Firebase provide
the method and we will set auate do
value as auate do unauthenticated again
so that we can go to login page so
whenever we are logged out it will be
unauthenticated so these four state
we'll use in the UI so that whenever it
is changed in the view model the UI will
be changed in this way it will work now
let's go to sign up page and call the
signup method because before login we
have to create the account so what I
will do I will close everything from
here go to sign up page here we have the
access of view model let's open AUB
model as well we have to get this Au
state so here we have to get the Au
state so for that I'll create a value Au
State equals to Au viw model. state.
observe as state so that it will be
reflected immediately if you H over here
you can see we have to add dependency
for this live data let's import it now
we can observe this Au state if it is
changed it will reflect in the UI now
what we can do on click of this create
account button or if you go over here on
click of this create account button we
can pass email and password to the view
model so here what I will do Au view
model do signup and I can pass email and
password that's it now it should work
now we have called the sign up let's run
the application and see either it is
working or not so if I go to don't have
an account and create account let's say
test [email protected] and password will be 1
2 3 4 5
6 let's create account fire console and
click on users over here in
Authentication we can see the account is
created that means create account is
working now what we have to do whenever
we have completed the create account we
have to go to homepage so for that what
we can do we can use this auate and here
we'll use composable that is launch
defect with the value auate do value so
when Au state. value we'll switch when
it is au state do authenticated if we
are authenticated from this nav
controller we'll go to homepage so nav
controller. Navigate we'll go to
homepage second condition can be if it
is au state. error will show the message
so toast. make text we'll need context
so here value context equals to local
context do current context we'll show
the message from auth estate. val.
message and toast. length will be sort
and so so here if you hover it will ask
to cast it to error if it is error you
can do it and in else condition we can
just return nothing that is unit so in
this way if we are authenticated we'll
go to homepage if you run it now if you
go to sign up page see we are in the
homepage that means we are already
authenticated we are already logged in
and we are in the homepage so let's
design the homepage so that we can see
here sign out button as well so let's go
to homepage now so in the homepage we
have to do same thing so first we'll
require Au State equals to Au viw model
do Au state. observe asate now we'll
send launch def here Au state. value
here when when auate do value if it is
auate do unauthenticated we'll navigate
to login page as we have done over here
if we are in homepage and we are
unauthenticated we'll go to login page
else we don't have to do anything so
unit now here we'll create composable
let's say column same thing modifier
equals to modifier. filmax size vertical
Arrangement arrangement. Center
horizontal alignment. center now here I
will create simple text that will be
home page font size will be 32 SP and
simply I will create a text button that
will be text sign out and whenever we
click on this we'll call the method of
view model off view model do sign out on
sign out will be unauthenticated then
we'll go to login page if I run
now in login page we have not
implemented anything that's why we are
here if you click on sign up page we'll
go to homepage because we are
authenticated now now let's click on
sign out if I click on sign out then we
are in login page that means we are now
on auth indicated if I run again it is
always go to login page now let's
Implement same thing in login page let's
go to login page here what we will do
first we'll create the launch def like
we have created in signup page I'll copy
this paste it over here create the
variable for auth state and context now
if it is authenticated it will go to
homepage if it has error it will show
the error that's it it is correct now on
click of the login button Au viw model.
login we'll pass email and password now
now it will be the complete flow if I
run now you can see we are in login page
let's login with the ID we have created
just now password is 6 Time 1 now if I
click on login you can see we are in the
homepage that means we are authenticated
and we are in the homepage if I run
again it is always go to homepage if I
click on sign out we are in the login
page we can create the account if you
pass the wrong login let's say test
[email protected] and anything password if you
click on login the message will be
displayed that it is incorrect that
means it is working so one more thing
what we can do we are setting the Au
state to loading so whenever we click on
login button let's say it should be
disabled when it is loading that means
what we can do in this button we have
this enabled over here after this
onclick what we can do we can set
enabled we can pass here Boolean so what
we can do we can check Au state do value
equals to equals to Au state do loading
we should do is not not equal to if it
is not equal to loading then it is
enabled if it is loading then it will be
disabled if I run now you can see if I
do
test you can see it is disabled for some
time and it is enabled again same thing
we can do it in sign up page as
well here
here if you go to sign up let's create
another account test [email protected]
password create account it will disabled
and once we have created the account
then we are in homepage if you go and
reload over here you can see we have two
accounts click on sign out let's try to
create account with the same user that
we have created earlier if you create
account you can see the message this is
already in use so in this way our
complete flow will work and you can
integrate firway authentication in your
application so if you have learned
something from this video hit the like
button if you have any doubts and
queries please comment below I will
reply for you don't forget to hit the
Subscribe button before going see you in
the next video bye-bye
[Music]
Weitere ähnliche Videos ansehen
Flutter Tutorial For Beginners #9 - Adding Google Authentication with Firebase
Auth Page Logic | To Do App | FlutterFlow for Beginners
JWT Authentication with Node.js, React, MySQL | Node JS Authentication With JSON Web Token
NestJs REST API with MongoDB #4 - Authentication, Login/Sign Up, assign JWT and more
NextJS + Firebase Tutorial 🔥 // Hooks, Firestore, Authentication & Functions!
[Legacy] Use Firebase for Auth in Wized
5.0 / 5 (0 votes)