In this lecture, we will validate the user's email by checking if their email is unique.
We left off with the validate function in the email taken file.
It's a required function by the async validator interface at the moment, we are getting an error for
its definition.
Upon further inspection, we will get a cluttered message about the error, in my opinion.
I'm not a big fan of this error message instead of trying to understand this message.
We should check out the documentation for the async validator interface in the resource section of this
lecture.
I provide a link to this Interfaces documentation page.
Scrolling to the Methods section, we will find more information on how to write the validate function.
The validate function will have one parameter called control.
It's a reference to the control the validator has been applied to.
The most important piece of information is the return value we have the option of returning and observable
or promise.
In either case, a validation errors object or no value must be returned.
Angular will automatically subscribe to an observable if we decide to return an observable.
If we return a promise, Angular will wait for the resolved value for this demonstration.
We are going to use a promise.
It'll be much simpler to write.
Let's go back to our editors.
Before writing the logic of the validate function, let's annotate the parameter and return value at
the top of the file.
We will update the import statement from the angular slash forms package to include the abstract control
and validation errors types.
Next, we will add the control parameter to the validate function.
It will be annotated with the abstract control class.
For the return value, we will explicitly return a promise.
The value resolved by the promise will be a validation errors object or null.
The return value will be similar to a synchronous validator if we return an object, angular will assume
the control did not pass validation.
It'll add the object to the errors property on the respective control.
We must return null if validation passes inside our function.
We are going to return the fetch signing methods for email function from the authentication service.
The fetch function is defined by Firebase in the resource section of this lecture, I provide a link
to this function.
There isn't an official function for checking if an email is unique in most cases, Firebase recommends
using this function as an alternative.
Users have two options for registering an account with their email.
They can provide a password or go password less.
Firebase supports both types of authentication systems.
This function will tell us which type of registration the user has opted into.
According to the documentation, this function returns a promise the promise will resolve to an array
of strings.
Honestly, we don't care which system they're using.
We're using this function for an alternative scenario.
If the email does not exist in our app, Firebase will return an empty array.
We can assume the email is unique.
On the other hand, if the array has values.
The email is taken.
We can use this function to check if an email exists within Firebase.
Let's go back to our editor.
First, we will need to provide an email.
We have to access the email through the control parameter.
Let's pass in the control value property.
Next, we are going to change the then function.
Even though the fetch function returns a promise, we should change the value resolved by the promise
by default, the fetch function returns an array of strings.
We are going to modify this value to return an error object or null in the event function.
We will pass in an arrow function with the response.
The return value from our arrow function will be a ternary operator.
We will check if the response array has a length.
If it does, we can assume the email is taken.
We will return an object with a property called email, taken with a value of true.
Otherwise we will return null.
Our validator is almost ready.
There are a couple of hiccups to fix in the next lecture.
We are going to finish our validator.