All about Firebase Authentication 🔥 | Login & Signup | Jetpack Compose

Easy Tuto
14 Jun 202424:47

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

00:00

🛠️ 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.

05:01

📱 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.

10:02

🔄 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.

15:04

🔒 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.

20:05

🏠 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

Firebase Authentication is a service provided by Google that allows developers to easily add user authentication functionality to their applications. In the context of the video, it is used to create a seamless login and signup process for the app, enabling users to create accounts and log in using their credentials. The script mentions integrating Firebase Authentication to manage user accounts and navigate between different screens based on the authentication status.

💡Android Studio

Android Studio is the official integrated development environment (IDE) for Android app development, based on IntelliJ IDEA. The video script refers to starting a new Android Studio project and configuring it to set up the development environment for the app that will integrate Firebase Authentication.

💡Jetpack Compose

Jetpack Compose is a modern toolkit for building native Android UI, enabling developers to create more intuitive and performant UIs with less code. The script mentions selecting 'Empty Activity that is Jetpack Compose Activity' when starting a new project, indicating the use of this toolkit for designing the app's user interface.

💡ViewModel

In Android development, a ViewModel is a lifecycle-aware component that stores UI-related data in a lifecycle-conscious way. The script discusses creating a ViewModel named 'AuthViewModel' to manage the authentication logic, including methods for checking the authentication status, logging in, signing up, and signing out.

💡Navigation

Navigation in Android refers to the process of moving between different screens or activities within an app. The script describes setting up navigation using a 'NavController' to manage transitions between the login, signup, and homepage screens based on the user's authentication state.

💡LiveData

LiveData is an observable data holder class in Android's architecture components. It is used for data observation within a UI. The script mentions using MutableLiveData to hold and update the authentication state, which the UI observes to react to changes in the authentication process.

💡Sealed Class

A sealed class in Kotlin is used for representing a restricted class hierarchy, where all subclasses are known at compile time. The script uses a sealed class 'AuthState' to define different states of the authentication process, such as authenticated, unauthenticated, loading, and error states.

💡Sign Up

Sign Up refers to the process of creating a new user account within an application. The video script details the creation of a signup page where users can input their email and password to create an account using Firebase Authentication.

💡Log In

Log In is the process of entering an application or system by providing credentials. The script describes implementing a login page where users can input their email and password to authenticate themselves using Firebase Authentication.

💡Sign Out

Sign Out is the action of logging out of an application or system, typically to protect user data or to switch accounts. The script explains creating a sign out method in the ViewModel that clears the user's authentication state and redirects them back to the login page.

💡Composable

In Jetpack Compose, a composable is a function that can be called to produce UI. The script frequently refers to creating composables for different pages like the homepage, login page, and signup page, which are the building blocks of the app's UI.

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

play00:00

what is going on guys in this video we

play00:02

are going to learn how to integrate

play00:03

Firebase authentication in our

play00:05

application we'll create login page

play00:06

we'll create signup page and then we'll

play00:08

integrate the Firebase authentication so

play00:10

that we can do a seamless authentication

play00:12

in our application we'll also learn

play00:13

about how we can navigate from one

play00:15

screen to another screen we'll learn lot

play00:17

of things from this single video so

play00:18

let's get started with new Android

play00:20

Studio

play00:22

project so click on new project select

play00:25

empty activity that is Jetpack compos

play00:27

Activity Click on next give the

play00:29

application let's give Firebase Au demo

play00:33

app minimum hdk Let It Be Android 8.0

play00:35

build configuration language Let It Be

play00:37

recommended one click on

play00:39

finish so till our project loads let's

play00:42

understand the architecture so here I

play00:44

will give the name Firebase author demo

play00:48

app and let's select canvas now for this

play00:50

application we will have three screen so

play00:52

first screen will be let's say sign up

play00:54

page so that user can create the account

play00:57

with the Firebase second screen will be

play01:00

homepage and third page will be login

play01:03

page so if user has already created the

play01:06

account they can use the login so in our

play01:07

application there will be these three

play01:09

pages so what we will do whenever our

play01:11

app launches let's say app launch from

play01:13

here it will first go to login page then

play01:16

we'll check for something either user

play01:17

has already logged in or not so how we

play01:19

can check we'll create a view model that

play01:21

we will call as o view model so that we

play01:24

can use it for all three pages so here

play01:26

we will put off view model now in this o

play01:29

View model we will create some methods

play01:31

let's say first method will be check Au

play01:34

status you can give any name so it will

play01:36

check either we are already logged in or

play01:38

not if we are already logged in then it

play01:40

will return something so first method

play01:42

will be check Au status so what login

play01:44

page will do it will first initialize

play01:46

this view model so that we can check the

play01:47

status if it is logged in if it is

play01:49

logged in then directly it will go to

play01:51

homepage so here we can say if we are

play01:53

authenticated that means we are already

play01:55

logged in then it will go to homepage if

play01:57

we are not logged in then what we will

play01:59

do we will call another method here that

play02:02

will be login so here we'll provide

play02:05

email and password so that we can login

play02:08

so from here we'll call login method and

play02:11

then from here we will get some response

play02:14

either it can be success or failure so

play02:16

we can write success or failure here if

play02:19

we get the success it will be

play02:20

authenticated and it will go to homepage

play02:22

if we get failure we can show some

play02:24

message so we can say failure with

play02:25

message so how we'll get this success

play02:27

and failure from off model we'll call

play02:29

the functions of Firebase We'll add

play02:30

library for the Firebase that will be

play02:32

Firebase authentication that will handle

play02:35

everything so here we will have the

play02:36

library code that will be Firebase so

play02:39

from here it will automatically call the

play02:42

Firebase

play02:43

functions so we don't have to worry

play02:46

about anything this will call the

play02:47

Firebase function and it will return the

play02:49

data from firas from view model we have

play02:51

to call the library code so here we will

play02:54

call let's say sign up or login and it

play02:57

will simply return success or F failure

play03:00

that's it so this is the task of view

play03:02

model so from login we do login it will

play03:04

go to fireb Au if it is Success then it

play03:07

will send success to login page we'll

play03:08

navigate to homepage if it is failure

play03:10

we'll show the message in the login page

play03:12

itself so this is how login flow works

play03:14

now in login page we'll create a button

play03:15

which will say if you have not created

play03:17

account yet you can sign up so we'll go

play03:19

to sign up page from the login page now

play03:21

from sign up page also same thing we

play03:23

will do we'll call the method of off viw

play03:25

model that will create over here sign up

play03:27

with email and password so it will call

play03:30

sign up and from here we'll get the

play03:32

response that can be success or failure

play03:35

with message same thing so whenever we

play03:38

are authenticated from sign up page

play03:39

we'll go to homepage again authenticated

play03:41

if it is failure it will show the

play03:43

message now we are completed with the

play03:44

login flow we go to login page check the

play03:46

authentication status if it is already

play03:48

logged in then we'll go to homepage

play03:50

directly if it is not logged in user can

play03:52

login or user can sign up so if it is

play03:54

authenticated from both side it will

play03:56

come to homepage now we have to add the

play03:58

log out button also so here what we will

play04:00

do in view model we'll create another

play04:02

method that will be sign

play04:04

out it will be simply sign out and from

play04:07

homepage we'll call that sign out method

play04:09

so here sign out so whenever we did the

play04:12

sign out that means we are

play04:14

unauthenticated then again it will go to

play04:16

login

play04:17

page let's move it here so if

play04:23

unauthenticated it will go again to

play04:25

login page so in this way our complete

play04:27

application will work we don't have to

play04:28

worry about these things these things

play04:30

will be handled by Library we just have

play04:32

to call from our Au view model that's it

play04:35

so now let's just start implementing it

play04:36

in the code let's get back to the

play04:38

Android Studio project our project is

play04:41

created let's run the application and

play04:42

see either it is created correctly or

play04:45

not so as you can see we have this hello

play04:48

Android that means it is created

play04:49

correctly so let's get back to the

play04:51

project and say hello to this hello

play04:52

world and let's delete this let's delete

play04:55

this greeting as well so here we will

play04:57

add our own composable so first thing

play04:59

what we will do we will create the

play05:00

navigation so what we will do let's go

play05:02

to our package here I'll create new

play05:06

package let's say pages and inside pages

play05:10

I will create three file that will be

play05:12

homepage and here I will create

play05:14

composable home page I create second

play05:17

page that will be login page

play05:20

file

play05:22

composable login page and I will create

play05:26

third page that will be sign up page now

play05:29

these will be our Pages now these will

play05:31

be our Pages outside this pages I will

play05:34

create navigation so I will call it as

play05:36

my app navigation so that we can

play05:38

correctly navigate let's call it

play05:41

as let's create composable my app

play05:45

navigation so here what we will do we

play05:47

will create a navigation controller and

play05:49

we'll call each Pages if you don't know

play05:51

how to create the navigation you can

play05:52

watch the complete video in the channel

play05:54

that is about navigation so here also we

play05:56

are doing the same thing first we'll

play05:57

create a nav controller remember nav nav

play05:59

controller so let's write remember nav

play06:02

controller so we need to add the

play06:04

dependency if you hover over here let's

play06:07

add this dependency and import it will

play06:09

take some time so our dependency is

play06:11

added now we have to create a nav host

play06:13

composable so nav host what it will take

play06:16

it will take nap controller that is our

play06:18

nap controller and instead of graph we

play06:20

will do start destination so it will

play06:22

take a string and then third parameter

play06:24

will be Builder will return composable

play06:26

here here is start destination if you go

play06:28

to our diagram here whenever app

play06:30

launches we should go to login page so

play06:32

start destination let's create as login

play06:35

and here we'll create composable route

play06:37

will will login if it is login then

play06:40

we'll return composable that is our

play06:42

login page we have created we'll pass

play06:44

the modifier from here that's it so in

play06:46

this way if it is login we'll go to

play06:48

login page now here we have to navigate

play06:50

from login page to homepage as well sign

play06:52

up page as well so we require nap

play06:54

controller so we can pass nap controller

play06:56

over here also we'll require view model

play06:58

so let's create view model first let's

play07:00

call it as o view

play07:02

model class and it will extend from view

play07:07

model for now we won't write anything so

play07:09

here we will take from Main activity

play07:12

that will be o view model and also pass

play07:15

off view model over here that means that

play07:18

means in the login page we'll require

play07:20

three parameter so second will be nap

play07:22

controller third will be off view model

play07:24

same way we will do for all three so

play07:26

homepage

play07:28

also and sign up page

play07:31

also now if you go to my app navigation

play07:33

error will be gone so let's copy this

play07:36

and create other composable that will be

play07:39

sign up it will go to sign up page and

play07:42

homepage it will be home that's it our

play07:45

navigation is complete at the launch of

play07:47

the app we'll go to login page and we

play07:49

can go to sign up and homepage from this

play07:51

NAB controller we have passed to every

play07:52

page now we can go to main activity and

play07:55

from here we can call that my app

play07:57

navigation and off view model will be we

play07:59

have to create a view model over here Au

play08:01

view model let's say which will be of

play08:03

type Au view model by view models now

play08:06

here we can pass Au view model Also

play08:08

let's pass this modifier C to modifier

play08:11

do padding will be inner padding that's

play08:13

it that's it from Main activity will go

play08:15

to my app navigation from my app

play08:17

navigation we'll go to login page so we

play08:19

need to design this login page if you

play08:21

run the app now there will be nothing

play08:23

because we have not written anything in

play08:24

login page now we'll start designing

play08:27

this so let's keep it side by side so

play08:29

here I will put it now first thing I

play08:31

will do I will create a column and give

play08:33

the modifier equals to modifier do

play08:35

filmax size vertical Arrangement will be

play08:38

arrangement. Center horizontal alignment

play08:40

will be alignment. Center horizontally

play08:43

and here I will create first a text

play08:45

let's say login page and font size will

play08:47

be 32p let's import this let's give a

play08:51

spacer modifier. height about 16 DP

play08:55

import this DP as well and below this I

play08:57

will create a text field so out outline

play08:59

text field value and on value change

play09:01

let's create this for email so I'll

play09:04

create variable email by remember

play09:07

mutable State off I'll initialize it

play09:09

with empty import import so here in

play09:12

value I'll pass email on value change

play09:15

email equals to it we can put it on

play09:18

separate

play09:19

line and third thing I will pass is

play09:21

label so that we can identify so here

play09:25

text composable that will be email

play09:28

that's it if you apply changes now you

play09:30

can see we have login page and we have

play09:32

the email text field so we can type here

play09:34

email also I'll give you space over here

play09:36

as well so after this outline text field

play09:39

about ad DP and create another outline

play09:41

text field can paste it let's call it as

play09:44

password let's create a variable for

play09:46

that as well here password now here

play09:49

value will be password and on value

play09:51

change password equals to it label will

play09:53

be password now again spacer let's keep

play09:55

about 16 DP and then below this we'll

play09:59

create a button button text will be

play10:02

login on click we'll do something right

play10:04

now let's put it empty and let's give

play10:06

spacer again ad DP if you rerun you can

play10:10

see we have email password and login now

play10:12

I'll create a text button so that we can

play10:14

go to sign up page so text button on

play10:16

click we won't do anything text will be

play10:18

don't have an account here we can write

play10:21

sign up and on click we'll do something

play10:23

if you run it you can see we have this

play10:25

button as well so this is our login page

play10:27

we can type the email we can type the

play10:29

password password we can do login and we

play10:31

can go to sign up page so first thing

play10:33

what we will do from here we have to

play10:34

move to sign up page so here in text

play10:36

button on click what I will do

play10:38

navcontroller do navigate we can pass

play10:41

the route that is sign up so whenever we

play10:43

click on this we'll go to signup page if

play10:47

I click I in sign up page it is empty

play10:49

because we have not written anything

play10:51

over there so what I will do I'll copy

play10:53

everything from this login page and

play10:55

paste it in sign up page as well it will

play10:57

be similar design let let's change

play10:59

everything from here email and password

play11:01

is required text will be sign up page

play11:04

email will be email password will be

play11:06

password and button will be let's say

play11:08

create account text button will be

play11:10

already have an account if so go to

play11:13

login page and navigate will be from

play11:15

sign up to login page if I run now if I

play11:18

go to sign up page it will be sign up

play11:21

page if I go to login page it will be

play11:22

login page so in this way it will work

play11:24

now what we have to do we have to

play11:26

implement the login and sign up so how

play11:28

we can do that if you see the diagram in

play11:30

AU view model we have to create four

play11:31

methods and then we have to call the

play11:33

Firebase so let's connect our

play11:35

application to the Firebase first so for

play11:37

that what we can do go to

play11:40

tools Firebase you can see a lot of

play11:43

option from authentication and you can

play11:45

select any authentication system let's

play11:47

say custom authentication and connect to

play11:51

Firebase so it will open in the browser

play11:54

you can click on ADD

play11:56

project so here you can see the name is

play11:58

automatically Ally detected you can

play12:00

click on continue you can enable or

play12:02

disable this analytics create

play12:05

project so our project is ready click on

play12:09

continue click on connect send so you

play12:13

can see your endro project is connected

play12:16

if you go back here you can see in green

play12:18

it is connected now we have to add the

play12:20

authentication Library click on it it

play12:23

will add the library for Google service

play12:24

and

play12:26

authentication so once it is done you

play12:29

can close this and you can go to the

play12:31

console console.

play12:33

Firebase and you can open your

play12:35

application you have to enable the

play12:37

authentication let's go to build

play12:40

authentication get started here you can

play12:43

see email and password click on this

play12:46

enable it and save it so our email and

play12:49

password is enabled in the Firebase and

play12:50

Firebase is connected now we can use the

play12:52

library code to create the account and

play12:54

do the login so what we will do we'll go

play12:56

to Au view model and create those

play12:58

methods so what will be the methods so

play13:01

to access the authentication we require

play13:02

Firebase o so let's create a variable o

play13:05

it will be of type Firebase o and we'll

play13:08

get it from Firebase o. get instance now

play13:10

we'll create the Au hisory so that we

play13:12

can get to know either we are

play13:13

authenticated either we are

play13:14

unauthenticated or we have got the

play13:16

failure with message so this thing we

play13:18

have to identify in the UI so for that I

play13:20

will create a seal class so sealed class

play13:23

let's say Au State this is our own

play13:25

implementation so here we'll create

play13:27

first object as authentic iated which

play13:29

will be of type Au state so that we can

play13:32

get to know in which state we are second

play13:35

will be unauthenticated Au State third

play13:38

we can say whenever we are doing login

play13:39

it will take some time to load from

play13:41

Firebase whenever we click on login or

play13:43

sign up it will take some time to do the

play13:44

login and sign up so we can call it as

play13:46

loading state it will be of type same Au

play13:49

State and the last thing will be failure

play13:52

with message so for that we have to pass

play13:54

message as well so I will create a data

play13:56

class instead of object which we call as

play13:58

error and we will have message in it as

play14:00

string so it will be of type same Au

play14:02

State now we have four type of State

play14:04

either we are authenticated then we'll

play14:06

be in homepage if we are unauthenticated

play14:08

we will be in login page and if we have

play14:10

error we'll show the message that's it

play14:12

so here I will create the variable for

play14:14

that private well Au state which will be

play14:17

of type mutable live data Au State

play14:19

generic now we have to expose it to UI

play14:22

so this will be private We'll add

play14:24

underscore on this and exposing variable

play14:26

will be Au State without underscore

play14:28

which will of type live data of au State

play14:31

and it will return this Au State now we

play14:34

have created the variable this we can

play14:35

use in the UI so first thing what we

play14:37

will do we'll create this method check

play14:39

Au status so check Au status we'll check

play14:41

either we are logged in or not so

play14:43

function check Au status here firas

play14:46

Library we can use au. current user if

play14:49

there is current user that means it is

play14:51

authenticated if it is not there that

play14:53

means it is unauthenticated so if equals

play14:55

to equal to null we can say underscore

play14:58

auate do value equals to auth State DOT

play15:01

we can say this unauthenticated if it is

play15:04

not null then it is

play15:07

authenticated so here we'll call this on

play15:09

init of the view model that is check

play15:11

auth status every time when we launch

play15:13

the activity it will check for this

play15:16

that's it one method is done now second

play15:18

method is login so we'll create another

play15:20

method here let's minimize this function

play15:24

login here we will create email string

play15:28

password

play15:29

string as I have said from view model

play15:31

we'll call the methods of Firebase Au so

play15:33

Au provide up some method so Au do sign

play15:36

in with email and password you can see

play15:38

over here you can pass email and

play15:41

password and it will take some time to

play15:43

complete so what we will do we will add

play15:45

listener that will be add on complete

play15:46

listener so whenever it is completed

play15:49

fire will provide us some task so the

play15:52

with this task we can check either it is

play15:53

success or not so we can check if task

play15:55

do is successful that means we have

play15:58

logged in else we have some error

play15:59

message so before doing this login what

play16:01

we will do first we'll set it to loading

play16:04

so here Au state. value equals to auate

play16:08

do loading so whenever we do login it

play16:10

will be first loading then whenever it

play16:12

will be success we'll say auate do value

play16:15

equals to Au state. authenticated and if

play16:18

it is failed we'll set Au state. Val

play16:21

equals to we'll set this error auate do

play16:24

error and we have to provide the message

play16:27

the message we'll get from this task

play16:28

itself so task if we have exception do

play16:31

exception and from that we can get

play16:33

message so it can be null so message if

play16:36

it is null will provide a generic

play16:38

message that will be something went

play16:40

wrong in this way whenever we click on

play16:42

login button we'll call this method it

play16:44

will set it to loading and then it will

play16:46

try to sign in if it is successful it

play16:48

will say authenticated if it is failed

play16:50

it will show the message before calling

play16:52

this method what we can do we can check

play16:54

if email is empty or if password do is

play16:57

empty if it is we can we should not go

play17:00

to this so what we can do here we can

play17:02

copy this error message paste it over

play17:05

here delete this and write here email or

play17:08

password can't be empty so that we'll

play17:11

not get the exception and we'll directly

play17:13

return from here if these values are

play17:14

empty we don't have to do login so in

play17:16

this way our login function is also

play17:18

completed so here we can do it is

play17:20

completed now we'll create sign up

play17:22

method same thing we have to do let's

play17:24

copy this everything paste it below

play17:26

let's call it as sign up and if it is a

play17:29

we will show the empty loading will be

play17:31

loading and off method this sign in with

play17:34

email and password what we will do we

play17:35

have another method create user with

play17:37

email and password you can see over here

play17:39

create user with email and password in

play17:42

sign up we have to call this method if

play17:44

it is successful it will show

play17:45

authenticated if it is error it will

play17:47

show the message that's it in this way

play17:49

we have created this sign up method as

play17:51

well now we have to create the sign out

play17:53

so here what we'll do function sign out

play17:56

we don't have to pass anything we have

play17:57

to do Au do sign out Firebase provide

play18:00

the method and we will set auate do

play18:03

value as auate do unauthenticated again

play18:06

so that we can go to login page so

play18:08

whenever we are logged out it will be

play18:09

unauthenticated so these four state

play18:11

we'll use in the UI so that whenever it

play18:13

is changed in the view model the UI will

play18:16

be changed in this way it will work now

play18:18

let's go to sign up page and call the

play18:20

signup method because before login we

play18:21

have to create the account so what I

play18:23

will do I will close everything from

play18:25

here go to sign up page here we have the

play18:27

access of view model let's open AUB

play18:29

model as well we have to get this Au

play18:32

state so here we have to get the Au

play18:34

state so for that I'll create a value Au

play18:36

State equals to Au viw model. state.

play18:40

observe as state so that it will be

play18:42

reflected immediately if you H over here

play18:45

you can see we have to add dependency

play18:46

for this live data let's import it now

play18:49

we can observe this Au state if it is

play18:51

changed it will reflect in the UI now

play18:53

what we can do on click of this create

play18:55

account button or if you go over here on

play18:57

click of this create account button we

play18:58

can pass email and password to the view

play19:00

model so here what I will do Au view

play19:02

model do signup and I can pass email and

play19:05

password that's it now it should work

play19:07

now we have called the sign up let's run

play19:09

the application and see either it is

play19:11

working or not so if I go to don't have

play19:14

an account and create account let's say

play19:16

test [email protected] and password will be 1

play19:20

2 3 4 5

play19:22

6 let's create account fire console and

play19:26

click on users over here in

play19:28

Authentication we can see the account is

play19:30

created that means create account is

play19:31

working now what we have to do whenever

play19:33

we have completed the create account we

play19:35

have to go to homepage so for that what

play19:37

we can do we can use this auate and here

play19:39

we'll use composable that is launch

play19:41

defect with the value auate do value so

play19:45

when Au state. value we'll switch when

play19:49

it is au state do authenticated if we

play19:52

are authenticated from this nav

play19:54

controller we'll go to homepage so nav

play19:56

controller. Navigate we'll go to

play19:58

homepage second condition can be if it

play20:01

is au state. error will show the message

play20:05

so toast. make text we'll need context

play20:08

so here value context equals to local

play20:10

context do current context we'll show

play20:14

the message from auth estate. val.

play20:16

message and toast. length will be sort

play20:20

and so so here if you hover it will ask

play20:22

to cast it to error if it is error you

play20:24

can do it and in else condition we can

play20:27

just return nothing that is unit so in

play20:29

this way if we are authenticated we'll

play20:31

go to homepage if you run it now if you

play20:33

go to sign up page see we are in the

play20:35

homepage that means we are already

play20:37

authenticated we are already logged in

play20:39

and we are in the homepage so let's

play20:40

design the homepage so that we can see

play20:42

here sign out button as well so let's go

play20:44

to homepage now so in the homepage we

play20:46

have to do same thing so first we'll

play20:48

require Au State equals to Au viw model

play20:51

do Au state. observe asate now we'll

play20:54

send launch def here Au state. value

play20:57

here when when auate do value if it is

play21:01

auate do unauthenticated we'll navigate

play21:04

to login page as we have done over here

play21:06

if we are in homepage and we are

play21:08

unauthenticated we'll go to login page

play21:10

else we don't have to do anything so

play21:12

unit now here we'll create composable

play21:15

let's say column same thing modifier

play21:17

equals to modifier. filmax size vertical

play21:20

Arrangement arrangement. Center

play21:21

horizontal alignment. center now here I

play21:24

will create simple text that will be

play21:26

home page font size will be 32 SP and

play21:30

simply I will create a text button that

play21:32

will be text sign out and whenever we

play21:35

click on this we'll call the method of

play21:37

view model off view model do sign out on

play21:39

sign out will be unauthenticated then

play21:41

we'll go to login page if I run

play21:44

now in login page we have not

play21:46

implemented anything that's why we are

play21:47

here if you click on sign up page we'll

play21:50

go to homepage because we are

play21:51

authenticated now now let's click on

play21:53

sign out if I click on sign out then we

play21:56

are in login page that means we are now

play21:58

on auth indicated if I run again it is

play22:00

always go to login page now let's

play22:02

Implement same thing in login page let's

play22:04

go to login page here what we will do

play22:07

first we'll create the launch def like

play22:09

we have created in signup page I'll copy

play22:11

this paste it over here create the

play22:13

variable for auth state and context now

play22:16

if it is authenticated it will go to

play22:18

homepage if it has error it will show

play22:20

the error that's it it is correct now on

play22:22

click of the login button Au viw model.

play22:25

login we'll pass email and password now

play22:28

now it will be the complete flow if I

play22:30

run now you can see we are in login page

play22:33

let's login with the ID we have created

play22:35

just now password is 6 Time 1 now if I

play22:38

click on login you can see we are in the

play22:40

homepage that means we are authenticated

play22:42

and we are in the homepage if I run

play22:44

again it is always go to homepage if I

play22:47

click on sign out we are in the login

play22:49

page we can create the account if you

play22:51

pass the wrong login let's say test

play22:53

[email protected] and anything password if you

play22:57

click on login the message will be

play22:58

displayed that it is incorrect that

play23:00

means it is working so one more thing

play23:02

what we can do we are setting the Au

play23:03

state to loading so whenever we click on

play23:05

login button let's say it should be

play23:07

disabled when it is loading that means

play23:09

what we can do in this button we have

play23:11

this enabled over here after this

play23:13

onclick what we can do we can set

play23:16

enabled we can pass here Boolean so what

play23:19

we can do we can check Au state do value

play23:23

equals to equals to Au state do loading

play23:27

we should do is not not equal to if it

play23:29

is not equal to loading then it is

play23:31

enabled if it is loading then it will be

play23:33

disabled if I run now you can see if I

play23:35

do

play23:37

test you can see it is disabled for some

play23:39

time and it is enabled again same thing

play23:41

we can do it in sign up page as

play23:44

well here

play23:47

here if you go to sign up let's create

play23:49

another account test [email protected]

play23:54

password create account it will disabled

play23:57

and once we have created the account

play23:58

then we are in homepage if you go and

play24:00

reload over here you can see we have two

play24:03

accounts click on sign out let's try to

play24:06

create account with the same user that

play24:08

we have created earlier if you create

play24:10

account you can see the message this is

play24:12

already in use so in this way our

play24:13

complete flow will work and you can

play24:15

integrate firway authentication in your

play24:17

application so if you have learned

play24:18

something from this video hit the like

play24:19

button if you have any doubts and

play24:21

queries please comment below I will

play24:22

reply for you don't forget to hit the

play24:23

Subscribe button before going see you in

play24:25

the next video bye-bye

play24:43

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
Firebase AuthAndroid StudioUser SignupUser LoginAuthenticationApp DevelopmentTutorialMobile AppsCodingJetpack Compose