Once we understand how strapping handles authentication well then we can start working on our front
end and I guess we're gonna start by registering the user.
So again we have two options we can either log into user or registered user and sends for the logging
user I guess we would need to set up first our user in a strategy.
We might as well register first and we have our function.
So I'm importing right now a empty string and let me just double check.
So we have registered user like I said we have nothing here and it's not even empty string.
In fact it is a comment.
And then within a log and Page Well I would want to run it and I would want to run it.
If the member is not true meaning if the user hasn't registered so I'm going to sign my variable response
to a wait.
Again it's gonna be a synchronous function and registry user.
So let me comment this.
And within this function we're gonna pass in the object with three values with all three state values
email password as well as user name.
Can you parse the arguments one by one without passing it as an object.
Of course you can.
The reason why I like passing in as an object because that way I don't need to worry about the order
of my arguments.
So we're gonna pass here the email then we're gonna pass the password like so and then we're also going
to have a user name.
And then if the response is gonna be successful legit console log something.
Now there's also gonna be a console log of course with our error if there's going to be an error but
for success we're gonna go console log and let's just write success like so.
And also you know what we can right away maybe console log our actual response so let's go with log
and response and you're gonna see our object our user object with our token.
Now we have everything in place in a log and user we're ready to start registering the user but of course
we need to set up the function and then within the register user function that is located within the
strappy we're gonna import first year things we're going to import axioms and so we're going to use
axioms to set up our ATP requests so we import CEOs from and then let's look for Axis library and then
also I'd want to import your URL again why we need the URL because once we deploy our application our
domain is gonna change.
So again the path is going to be still forward slash all local and register but the actual domain is
gonna change.
So I'm going to go with the URL it is coming from the hotels.
So let's find new tools and then let's look for the URL.
So those are our imports.
Now let's set up our function and this is gonna be a synchronous function.
So let's write async then we're gonna have function.
Then let's do register user register user and then since we passed thirty three arguments I know I need
to set up three parameters.
I'm going to be looking for email password as well as a user name and then within a function body for
the time being.
Let's just leave it blank and before we write anything else largest export as well so let's write export
default and then we're going to write a register user register user so that way of course within the
logon page now we can access it.
And the way it's going to work is we're gonna have another response variable so cost response and that
is gonna be equal to a weight.
So again we're gonna wait till our promises settle because again act use is going to return a promise.
So we're gonna go with ax heroes then in order to set up a post request we need to type dot post.
So that is the method and then we're looking for the URL.
Again your URL is gonna be domain and then whatever we have all local register.
So I'm going to set up a template string first I'm going to access the You are all variable which gives
me my local host.
Then we're gonna have all then we have forwards lush local and then we're gonna have forwards the cash
register.
Once we set this up.
The second thing is we need to pass in our data.
So essentially we need to pass in the fields.
Remember when we checked strappy the user had fields it had a user name email and password and those
data they were passing in.
So we're gonna go with user name because that is our parameter and we're going to have email as well
as the password.
And since our quest returns a promise we can right away a chain that catch.
So we're gonna go catch on we're gonna say error and in our case we're just gonna console log there
let's console log the error and then keep in mind this is a synchronous function so it's going to happen
by default it is going to return a that's not the reason why within the logging we set our handle submit
also as asynchronous.
But what we would like to return from register function is in fact our response.
So I'm gonna say Here return response whatever the response is either it's gonna be undefined or we're
gonna get our object but again since it is a single function it is going to return a promise.
Now let's save it and let's take it out for a test drive.
So when I click on toggling as I would like to of course register now currently it says please fill
out all the form fields.
That is correct because of course our is empty is true.
And now I'm just going to set up some emails.
I'm gonna go John G mail dot com.
As far as the password.
I'm just gonna go it's something really simple.
And then we have the user name.
So let me type in John as you notice once I type in the value now I'm able to submit and again I'm going
to have two options either my response is gonna be success essentially I'm just gonna get my user object
or it's gonna be undefined.
Let's try it out.
Let's try to submit.
Now we're just gonna wait a little bit and I can see that I have success.
So if I head over to a log page I a console log the success and that is going to be my response.
So essentially I'm just gonna get back my data and what we're going to be looking for is this J WTI.
So Jason Webb token and if we're gonna head over right now to our strappy application you're going to
notice that if we refresh There you go we have a new user.
Of course I'm showing you still the input but there is there is our user.
And like I said since your ad I mean you can do whatever you want you want to delete John.
You can go ahead and delete John.
Now I also want to show you how we're gonna have a error because think about it we already registered
user.
However of course we haven't done anything in our front and we still have the values and everything.
So what do you think is going to happen if I'm going to try to register one more time with the same
user.
Think about it.
Well we already have the user ID so we should have the user.
Now you can clear the console you can leave the object.
It's up to you.
I think in my case since we're gonna do it quite a few times essentially looking at the user object
I'm just in the clear for now and we should get an error and we do we have a bad request.
So essentially we cannot of course register with the same values.
And what's interesting is that with strappy sense when we're signing in we can use either a log in or
user name Marcus or a email when we're signing up.
Even if my email is gonna be different but my user name is gonna be exactly the same.
I'm not going to be able to register.
So let's test it out let's say I'm going gonna try to register with email of Peter but still gonna keep
the same user name.
If I'm going to try to do that again I'm gonna get an error.
I'm getting the error because user name is exactly the same.
That's the reason when you're trying to sign in.
You can use either the email or the user name.
And the reason why I didn't do that because I didn't want to overcomplicate things but in general please
keep in mind that is always your option.
So that's the reason when we were registering also the user name needs to be unique.
So let's say if I'm trying to register Peter in this case I'm gonna wipe out and my console if I'm working
and go with Peter username and if we're going to try to submit it then of course we're gonna have again
our data our user data.
So that was successful and not only that if we're going to head over again to our shop application refresh.
Now I have two users I have John and Peter.
And just to show you that we rule the world just let's delete.
Peter there's no point for him being there.
Let's confirm Yep.
Now Peter has been deleted.