How To Use Firebase Authentication With Xamarin Forms

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we are going to see how to authenticate users in xamarin forms application we will be using firebase as the backend system by the end of the video you will know how to sign up new users how to authenticate users using an email and a password and how to recover a password when you lose it if you are new to the channel consider subscribing the link to the source code will be available in the description let's get into it i have already created xamarin form solution to keep things simple the solution only targets ios all views have already been created we will focus on the business logic of the app before using firebase in the application we need to create a firebase project that will be connected to our application i'll go to the firebase console i create a new project let's call it exam forms firebase health i hit continue i disable google analytics because i don't need it i hit continue once my project is created the next step is to register my app in this project on the overview page i click on the ios icon that brings me to the registration page the first info we need to provide is the bundle id you can find it in the info please file in the xamarin solution i set the hap nickname to exam forms firebase hoth at this point a file has been generated i save it the registration is done the next step is to set up authentication by email and password on the overview page i click on the authentication card in the users tab i click on setup sign-in method in the sign-in method tab i click on email and password i toggle the enable switch to enable authentication let's save the configuration so we have registered our application and setup configuration let's move to visual studio now let's add the firebase ios config we saved earlier the file is named googleserviceinfo.plist it contains identifiers such as api key database url and so on to interact with firebase we will need two dependencies xamarin.firebase.ios.com and xamarin.firebase.ios.auth let's install them now that we have nuget packages installed we need to initialize firebase in the application in app delegate file i use the app class located in the firebase.com namespace and i call the configure method firebase is now configured the next step is to implement authentication logic in view models let's start by checking whether a user is signed in if it's not the case we will display the login page we'll put the logic in home view model in the constructor i had a method check whether the user is signed in firebase sdk will tell us if a user is sign in for code located in the share project to interact with code from the target project we need a shared interface in the common folder i create an interface called i authentication service i had a method called is sign in let's go back to the home view mode in the check whether the user is signed in method we get an instance of the authentication service by using the resolve method from the dependency service class we call the assigning method if not true we navigate to the login page let's add an implementation to the i authentication service interface in the ios project i'll create a class called firebase authentication this class implements the interface i authentication service let's implement the is sign in method i use the auth object which is a singleton and check the current user if it's not null it means the user is sign in in app delegate we register the interface as a dependency let's run the project there is no sign-in user i have been redirected to login page login view model as a sign up command bound to a button when the user clicks on this button the signup page should be displayed let's add code for that in the command implementation i use the method go to async on the current instance of the shell i pass new user page as an argument this is the sign up form let's run the project if i click on the sign up button the sign up form shows up let's add a method to create user in the authentication service let's edit the i authentication service i had a new method signature accolade create use this method takes three parameters a username an email and a password let's edit the implementation i use the create user essence method on the object in returns i get a not result object the create user async method takes the email and the password as argument once the user is created we can add additional information to his profile the auth result object as the property user i use the profile change request method on the user object to get a user profile change request object i set the display name to the username i commit the changes at this point a user and his profile has been created i return true in the catch block i display the error message in the console i return force to indicate that the creation failed you can't catch specific firebase exception but to keep things simple i'll catch general exceptionally let's go back to the new user view mode the signup command will wrap sign up logic i need an instance of the authentication service i call the create user method and i pass the username email and password as argument if the result is true the user is redirected to the home page otherwise we display a message to the console let's try to add a user patrick will be the username i'll put my email address i put a password if i click on the sign up button i'm able to log in let's check in firebase console you can see that the user has been created successfully in my early tests i had an issue with keychain sharing to resolve the issue make sure the bundle signing is set correct let's move to auto sign out the application has a menu item in the flyout the menu item command is bound to a sign out command let's create the command in the view mode in app shell view model i create a new command called sign out command in the action beyond the command i use the authentication service i call the sign out method after sign up we shall display the login page the sign out method doesn't exist yet let's generate it let's also update the implementation i call the sign out method on the auth object and that's it let's test out if i click on the sign out i'm out and the login page shows up the next step now is to sign in user the user will provide an email and a password by clicking on the sign in button if the authentication is successful the user will be redirected to the home page let's update the i authentication service interface i had a new signature method i call it sign in it takes an email and a password as parameters in the implementation i call sign in with password sync method i pass the email and password as argument i use the get id token ethnic method on the user object to obtain an authentication token let's go to the login view model i add a sign in command this command is bound to the sign-in button in the action i call the sign-in method from authentication service we are not going to do anything with the token here let's test the login i had my email and my password and boom it works in the next section we will implement password recovery in the login view there is a forgot password button when the user clicks on this button the forgot password page should be displayed in login view model i add a new command forgot password command in the command action i use the go to async method from the shell i pass the route to the forward password page and let's test it if i click on the button the forgot password page shows up let's implement the logic behind password recovery in the eye authentication service interface i add a new signature method accolade reset password it takes an email as a parameter and returns a task in the implementation a call send password reset async on the auth object i provide the email as an argument when the user enters his email address he can click on the send button to request password recovery the button is bound to a reset password command let's create it in forgot password view model i had reset password command in the action beyond the command i call the reset password method on the authentication service i pass the email as the argument after the request i display an alert message and redirect the user to the login page let's test i enter my email address i click on the send button after a few minutes i receive a link to recover my password i click on the link and follow the indications i was able to set a new password i am going to wrap this tutorial here you can find the source project and useful links in the description below if you get any values from this video hit the like button and subscribe thanks for watching and see you next time
Info
Channel: Tech With Pat
Views: 15,442
Rating: undefined out of 5
Keywords: xamarin forms firebase authentication, authentication firebase xamarin forms, xamarin firebase login, firebase authentication xamarin forms, xamarin form firebase authentication
Id: H-hZR123D9c
Channel Id: undefined
Length: 14min 11sec (851 seconds)
Published: Sun Nov 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.