Flutter Firebase & DDD Course [6] - Firebase Auth Setup & Facade

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
although we fully implemented the sign-in form block in the previous part we still cannot actually sign in using fire baits until now we have successfully hidden firebase of details behind a nice interface called I of facade through the power of abstraction now it's time to provide a concrete implementation for it while using Google sign-in and firebase off Oh welcome to resew code where you are getting prepared for real app developments of this you will get freelance clients or a job and be confident about the apps you built so subscribe and hit the bell to join us in our quest for become an in-demand flutter' developers before we go any further I just wanna let you know that these kinds of tutorial series on this channel are no longer gonna be supported with written tutorials there is still going to be code published as always on github so you can go there and really study the code at your own pace but there will be no written articles so the first thing we should do here is to create a firebase project because actually up until now we did not have any firebase configuration inside our app so we're going to open up a browser and go to cancel that far based on google.com login with your Google account and we're going to hit here at project we're going to call this note app and then just click continue then we're going to just enable Google Analytics we don't really care about that continue Google Analytics account will be default account for firebase and now let's hit create project after some time the project is ready and we can go ahead and continue and now we just want to set up our android client of course you can also set up the iOS client and that's actually a bit simpler to set up budgets for the purposes of this tutorial we're focus on Android to keep this short and simple so let's click on this Android icon and I actually don't know what is the package name of the app so let's go ahead and go to the Xcode open up the Android folder we're gonna hop into app build our Grail and here we can find that it's called what oh here it is come reso coder and blah blah blah so we're going to copy this and please copy your own package code because otherwise it's not going to work we're gonna paste it here and now we need to provide debug sign and certificate because we are gonna be using the app in debug mode and we will want to sign in with Google and sign in with Google requires Google to know the sign-in certificate not sign in as in sign in but signing right and since we are in debug mode the app is signed with a debug key and pardon me I just made the font a bit bigger but you didn't miss anything we just filled in this Android package name so we want to go to this page and here it writes how we can obtain the sign-in certificate among windows so I'm gonna just copy this command and hop into vs code open up the terminal I'm using PowerShell paste this in here and it sort of didn't work that's unfortunate because as you can see this command is provided as two parts with this slash which signifies newline I'm not sure why they provided as base at least on Windows what you need to do is to paste it for example to the address bar in the browser remove the hash or remove the fourth slash backward slash excuse me and now we're going to copy the command without backslash and now we should be able to use it just fine in the terminal once we do that we see that we have an error popping up on us which is unfortunate so maybe you just cannot use PowerShell maybe we can use a regular command line these things are very funny sometimes so now we're gonna use the regular command line let's try that and as you can see we can use regular command line so Windows users you probably cannot use PowerShell with this command anyway we're now going to enter the keystore password and since this is a debug keystore the password is just Android and here we go we have the sha-1 fingerprint here we can just copy it and once we have that by the way this is completely fine if you see this because it's just a debug keystore anyway now back inside the browser we're gonna paste this sha-1 debug sign-in certificate into this field and register the app once that will be done we'll be able to download the config file awesome so we can download Google services dot JSON and we are going to put it into the project folder which we are which we have opened in BS code go to Android app and save it there so when we go to vs Co we can see that we have the Google services JSON file right here now what's quite important to do is to add this file to get ignore for Android so just go here and we're going to say Google services JSON save this get ignore which is present inside Android and this way this will not get added to the git repository which is pretty important because you do not want to keep your sensitive details inside a git repository especially if you have some sort of an open-source project such as this one because I'm publishing it github for everybody to see all right with this get ignore and also the Google services JSON file here we are now going to be able to use firebase but in addition to all this we also need to add some native dependencies to the build downgrade of files for Android if you are on iOS you actually don't need to mess with any native things at all it's all gonna get done for you but an Android what we need to do is continue in the setup so hit next and now we want to add firebase SDK so we want to copy this line we're going to hop into the project a level build that Gradle so now the one we have currently opened under app but only the one which is in the Android folder itself we want to go to build a cradle there and now go to dependencies I guess and paste this line here so we are depending on Congo gold services and now let's scroll down in the console and we also need to apply the plugin Google services in the app build a Gradle file so let's scroll up and we're going to apply it the plugin right here and then also we need to and firebase analytics if you are using analytics we are actually not gonna be using them but anyway I'll just add it over there to dependencies just add it like this and now with all of this setup we can now hit next we are now going to verify installation we don't care about that and now we are inside our app in firebase console so now what we need to do is just to go to the authentication tab go to sign-in method and we want to make sure that we have enabled email and password sign in so let's just hit enable and then also Google will be enabled okay project support email and save with all of this Donna we are now gonna be able to use firebase and also firebase authentication from our flutter app although it's gonna take some time until we get to the point of having user interface to use it all but at least we know that we have everything set up so we don't need to care about this anymore now what we also need to do is to end the packages which are flutter specific because we have set up the native Android part but also there are regular pub packages while we are at that let's also update some of the already out of the packages which we have added previously for example freezed now has a newer version so let's update that and yeah that's about it for now the packages which we want to add our firebase core firebase off and also Google sign-in so let's just add them here and as always you can get the code not from the written tutorial for tutorial series but from the github repository and because we have updated the frist package also let's just make sure that everything is still working properly by running the build command so we're just gonna say for our pub run build run or watch and with the leaf contracting outputs just to be safe that the new version works and by the way always make sure to check on the github repository for new versions if you are watching this in the future because there are probably gonna be some breaking changes down the line and I'm gonna make sure to go through the project and github and update the existing code periodically alright so some of the code was regenerated but we don't have any errors and that's cool we can now close out this code generation and let's focus on the facade first of all we only have the i/o facade which is the apps tray class or in other words interface but we don't have any class nor file which should hold the implementation and where should the implementation of an I office I'll be helped well it's a part of the infrastructure layer of the auth feature so under infrastructure we are gonna create a folder and inside of it we are going to create firebase off facade it's inside infrastructure because we are gonna be dealing with third-party dependencies and the reason why the interface IO facade is in the domain layer is because the interface is completely independent of any third-party dependencies it just defines a contract and that contract should be fulfilled no matter if we use firebase or some custom authentication solution this facade contract needs to be fulfilled that's why it's in domain but as soon as we start even with the specificities of firebase we need to move over to the infrastructure later all right so let's create a class firebase or facade it's going to implement I or facade and we are going to actually deal with dependency injection later in a specific part just for that where we are gonna setup injectable and get it and all of that stuff but for now let's just populate this class with a few fields final firebase auth is a dependency for firebase or facade obviously we need to have firebase off if we want to sign in with firebase so far based off aced dependency field name and then also final Google sign in Google sign-in is its name now let's create constructor for final fields by hitting ctrl + dot and let's also import the library for firebase off and also Google sign-in and lastly we are going to create three missing overrides and those missing overrides are of course register with email and password signing with email and password and sign in with Google and for some reason password changed into PA Rd which is interesting anyway now we have fixed the issue we're good to go that just form a disco debate by providing commas where they should be located additionally let's also make these parameters required and let's import required from Foundation and I know you can also import just meta but I don't care I just wanna import something quickly so let's jump into register with email and password first right what we should do here is to call firebase off that create user with email and password the problem though is that the method on firebase auth does not have the concept of our value object email address and password it expects a string and indeed email address is just a string which is validated and the same goes for password which is a value object holding a string so what we need to do first before calling this create user with email and password is to extract the value held inside that value object and we can do that by doing something like final email address string and we are gonna say email address that value dot get or else and if it's not a valid string so this value holds a value failure we can say failure oh no and this way this email address string will either hold the proper email address or if the email address entered is invalid this email address string will hold the failure in all caps the problem is this kind of behavior does not make sense because think about it in the previous part we have implemented the sign-in form block and we are just not gonna be able to proceed from it if there is some sort of a failure happening in the email address or password for example if the email address is invalid we are physically not gonna be able to press the sign-in button so this means that if we get to the firebase all facade register with email and password method and the past in email address is in the valid something is wrong in our app and we should fix it immediately because this state should not ever be possible we should simply not be able to get to register with email and password if the entered email or password is invalid and what do you do when you get into a state which is completely invalid and doesn't make sense well you throw an error not an exception but rather an error errors in dart at least as the convention goes are reserved for things which should just crash the app because we have arrived in an unrecoverable state so let's create such an error shall we we are going to create it inside domain and core because errors are really not specific to any sort of a layer there are just some core thing so we're gonna create new file errors the dart these are not failures these are simply things which are thrown as usual and they should specifically be used to crash the app we won't be apt to crash if we write in an unrecoverable point so we're gonna create a class an expected value error it will extend error and inside of there we are going to store a value failure and value failure of course comes from failures that Dart it's the freest Union so we want to store the value of failure which caused this unexpected error so we're gonna populate it in a constructor and then the thing which will be printed out to the screen or to the lock or to the debug console is going to be of course we it over I to string is that we have encountered a value failure at an unrecoverable point and I'm gonna spare you of me writing that out I'm just gonna paste it here and you can get this string and all of the gold from the project on github link is in the description but actually it would be quite nice to see also what the error was so we're going to say also failure was and say value failure here which is gonna be automatically converted to string but this string is quite long so what we're gonna do is extract this part to its own constant string explanation like that now we're going to interpolate the explanation also the terminating should be part of the explanation all right and now this is good enough but errors should not have any weird characters inside of them and that's why it's best to use error that safe to string method and pass in this string this is gonna ensure that any sort of an environment some command line which is like from the 80s is still gonna be able to display the error I guess that's what this safety string is for once we have this done we can hop into firebase office R and what should we do well instead of get or else and returning failure if the value is left value failure we are instead going to throw an exception when an unexpected thing happens the way we can do that is by saying fold and if the value is on the left side we are going to throw an expected value error and pass in the failure let's actually rename l2f to make it clear that we are dealing with the failure and then if the value is correct we are going to just return our but of course we don't want to do this everywhere we have some sort of an invalid and recoverable state so that's why we are going to move this monstrosity in to the value object superclass itself so let's just copy all of this or cut it out actually we're going to go to email address value objects superclass and once we are inside the value object superclass we're going to create a helper method on there which will be get or crash and it will return P and T is of course the value or the type of the value which is held inside the value object and we're just going to say return value fault an expected value error let's just import everything here and we are good to go and also so that you will not go away without gaining any functional programming knowledge goodness this R arrow R is fairly self-explanatory we are simply returning the value of the right side of either this one unchanged this is good enough and I think we can remain at that but just so that you learn something new this is called identity because after all we are returning an identical thing we are not changing anything we are now returning anything else and there is a function which we can call from darts and it's called ID if we go there you can see that it receives something and it returns that something unchanged so that's it you can also use this identity function I'm also going to comment this in just so you know when you check out the source code on github and also it's not bad to document what this get or crash method does because it may not be self explanatory so that's why we are going to provide a documentation so three slashes and now we're gonna say throws and expected value error containing the value failure so now what we can do is hop into firebase all facade and just call email address get or crash if we check I would clear the crash does we can see that it throws an expected value error blah blah blah so now once we get to register with email and birth and for some reason which is completely unknown there is an invalid thing held inside the email address we're going to crash the app and that's precisely what we should do because again this situation should not even happen in the first place it's completely in our power to prevent it by writing good code the same thing which we have done for the email address is also going to happen for the password so password string and with that done we have the email address string and password string and we can now pass them over to the create user with email and password function so email address string goes in their password string goes also there this returns a future containing auth result which we should probably await right so let's create or let's make this function async and I am sure you remember from the previous parts that the infrastructure layer which is also this far based off facade is responsible for converting exceptions into failures which are then held inside either so we should handle any exceptions which this create user with email and password function can throw and convert them to our nice or failures which we have created a few parts back so for example we have failure email already in use or invalid email and password combination and so on if we take a look at the documentation for the create user with email and password method we can see that it can throw three types of errors weak password invalid email and email already in use if we take a close look at this weak password and invalid email are really not something we need to worry about here because we are already pre been these invalid emails and weak passwords from even entering into this function in the first place because we have good validation in place inside our value objects email address and password and of course even if you are now going to the extremes and by the way I don't think I would go to these kinds of extremes with value objects like ever because yeah it really takes so much time to build this up you most don't need this but we are just demonstrating the domain driven design principles here we are going by the book and abiding all the rules but I don't think that you really need these kinds of objects in a mobile app but anyway even if you do not have them you are still going to validate the form and you are still probably not gonna get to this method with invalid values for the strings of email address and password but what does interest us is this email already in user so we would definitely want to catch that so try-catch let's move this up top catch will be also here but of course we do not just wanna catch anything we just want to catch platform exceptions because that's what is actually thrown from these firebase methods so on a platform exception catch e and now how can we differentiate between these sorts of errors because we are really interested only in email already in use error and the way we can differentiate between these different types of errors is to check if e that code is equal to and now copy the email or the error code which we are interested in if the code of the platform exception is this we are going to return left and comes off failure not canceled by user but instead email already in use and then also we're going to otherwise return left Const or failure server error why because if the error returned is either weak password or invalid email it should not have happened really but in case it happens we are just going to show a generic server error happened message to the user the problem is that if you take a look at these squiggly lines they say that this function has a return type of this but doesn't end with the return statement why is that we are returning left from the catch statement but we are now returning anything from the try block so if there is no exception happening we are not gonna return anything and we need to return something so what can we do well we can return right unit because unit is used inside either to signify that nothing wrong happened it's sort of an void for either which I have already explained in the previous parts now that we already have all of this done it's gonna be extremely simple to also implement the sign-in with email and password actually let's just copy the content of this register method paste it inside sign in with email and password method make it a sync and the changes here are pretty substantial so that we really cannot refactor this code later on we need to have these separate methods almost copy each other that is how it needs to be because we need to call sign-in with email and password and also handle different errors because signing with email and password can throw invalid email wrong password user a fan blah blah blah too many requests we are not going to care about every of these errors instead we're just going to care about error wrong password and error user not found because if you think about it error prone password means that the password is wrong of course and error user not found means that there is not proper email address or actually that the email address which is entered does not belong to any user and in either case so error wrong password and error user not found so or that code is equal to our user not found we want to return the very same of failure and its name is invalid email and password combination why is that why do we not have separate failure for a wrong password and a separate one for user not found why do we join them together well the reason for that is that if we said wrong password to the user in the UI that user if he tried to be malicious could know now that the email address is actually correct and only the password is wrong and what could happen then is that the user could do a brute-force tag or just simply try to hack the account because that user which is malicious would know that oh that email address is already present on the back end I just need to find out about a password but with this sort of an error message invalid email and password combination that potentially malicious user can cannot have any clue if it's actually the email that's wrong or if it's the password that's wrong right so we are making it a bit harder for the possible person to hack into the data of an unsuspecting user and again otherwise so the fallback case for all of the other errors we are just going to return a generic server error because yeah that's about it what the user needs to see and of course you can always log this error to your error handling reporting system like crashlytics so lock and you would be able to log the error here and so on of course we're not gonna do that that's outside the scope of this tutorial series lastly let's implement sign-in with Google right this is gonna be a bit different because we do not get anything passed into the sign-in with Google method we actually need to call something on the Google sign-in instance to initiate the pop-up where the user actually provides their information for the Google account so what should we do well we should call Google sign-in dot sign-in of course if we check out the documentation we can see that it returned in the future so that automatically tells us something that we should make this method asynchronous return future resolves to an instance of Google signing account for successful sign-in or null in case sign-in process was aborted and what do we have as an auth failure we have canceled by user so what that tells us is that if the return value by this method is null so final Google user is equal to that and if the Google user is no we wanna return a failure so left auth failure dot canceled by user alright cool I'm not sure why does on a return anything interesting stuff of course because it's not a sink silly me anyway now we need to await the signing call and we're done here not quite but at least for this part of the code but what should we actually do with the Google sign account which I have called Google user well this Google sign accounts can provide us with authentication object which holds or actually this is a future but this value holds an access token and an ID token an ID token is an open IV connect compliant token which is used if you want to authenticate with Google it's a JSON web token but we actually do not need to care about its implementation because we are gonna let firebase handle everything for us we just need to pass it over to firebase and it's gonna be all done and set so what we want to do here is to actually grab the authentication from the Google user and stored inside a separate variable find on Google authentication is equal to this and of course we need to await it because it's a future this is just Google specific authentication but we are now dealing with Google services themselves we actually want to use this inside firebase and firebase does not understand the Google sign-in authentication object firebase understands only something called a credential and every major signing provider for firebase does have and auth provider implemented let me show you for example there is the Twitter auth provider this comes from firebase off there is also github of provider this also comes from firebase off and similarly there is the Google of provider and what we want to do here is to get credential and we are just going to pass in the Google authentication that ID token and then also Google authentication access token format everything so that we can see it on the screen and this will return an auth credential author credential is nothing complex it's just a simple object I'll just actually see what it is of credential is a simple object which just wraps the ID token an access token if that simple it's nothing fancy it's just a simple wrapper class and by the way you may be thinking that okay cool but for signing in with email and password we did not need to obtain any credentials right we just passed those email addresses and passwords right into some method on firebase off we did not need to get credential or nothing well of course that's true but what's happening behind the scenes is that firebase calls email off provider get credential and passes in the email and password to that method to obtain authorization it's just hidden from your site because yeah firebase doesn't want to add any unnecessary boilerplate to your code but even in the case of email and password you should know that you are still actually dealing with credentials only they are hidden we're gonna store this credential inside final or credential and with this we can now use it to sign it with credential on fire based off that sign in with credential and passing the author credential with this all set and done for some reason we have an error oh of course we are not returning anything that's why that's because we should return either of failure and unit and not an auth result since I have already written the return keyword before calling this method what we can do instead of just splitting it up and awaiting the call and then simply returning right unit what we can do instead is to use the error syntax for handling futures in dart which is the chaining with then and when we obtain value which is the auth result right so our for result we are going to return unit right unit alright and we actually don't need to use the await keyword anywhere because we are just returning the future itself we are not awaiting anything and of course even sign in with credential can return an error but we are actually not interested in showing the user any kind of these errors because they are really not something that the user should worry about again we can log them to crashlytics and do all sorts of stuff with them but the user should not be aware of these sorts of low-level errors and for that reason we are just going to ignore them so let's just wrap actually this whole function body inside try catch copy this code or I'll just move it up try on platform exception catch and we don't actually need to worry about what we taught so we'll just provide an underscore there and we're going to always return left server failure whoa so both failure server error like that and let's also make this constant which tells me that I did not enable proper a linking because we should be getting the messages that we should use Const whenever possible and so in the next part we are going to enable dependency injection and do all sorts of things for maintenance like for example the just now mentioned a linting so in this part you learned how to use firebase and also Google sign-in to provide an implementation for our firebase auth facade you have also learned how to handle unrecoverable and actually quite nonsensical states because something like an invalid email address days deep into our layer structure should never happen that's because we should catch something like an invalid email address right in the presentation layer or actually in the application layer if you know what I mean inside this sign form block this is where we should stop anything from happening if there is some sort of an invalid value input it but in case something like that happens we are going to crash the app because that's the proper thing to do in the next part we are going to do some sort of a light maintenance work like setting up injectable for dependency injection and also enabling linking and just making sure that our code will remain healthy and maintainable throughout its lifetime I hope that this more of a of the cuff recording of this video is something that you enjoy because I don't have anything scripted I don't have any Oracle to follow but as always you can get the code from the link in the description which in this case is now gonna take you to reso coral common fortunately but it's going to take you to github and if you are serious while we come in a great flora developer who can build real apps for clients or at the job go to flutter dot education link is also in the video description to get the top curated foreign news and resources and that improving your app development career over there you can also subscribe to my mailing list to get the best photo resources delivered weekly right into your inbox and if you don't want to miss more tutorials like this be sure to subscribe to this channel and also join you on vacation squad by hitting the bell button to make sure you grow your flower skill because here in reso colder I am determined to provide you with the best tutorials and resources so that you will become an in-demand flutter developer if this video helps you again give it a like and also share it with our developers who are purely going to find beneficial to leave a comment and see you in the next video [Music]
Info
Channel: Reso Coder
Views: 16,029
Rating: undefined out of 5
Keywords: resocoder, tutorial, programming, code, programming tutorial, flutter, flutter tutorial, flutter firebase, flutter firestore, flutter firebase auth, flutter firestore tutorial, flutter firebase tutorial, flutter architecture, flutter architecture patterns, flutter domain driven design, flutter ddd, flutter database, flutter todo app, flutter todo app tutorial, flutter todo app firebase, flutter app tutorial, flutter app example, flutter clean architecture, flutter bloc
Id: plz5NRNWiVU
Channel Id: undefined
Length: 44min 52sec (2692 seconds)
Published: Tue Apr 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.