All right my friends last thing I do inside of our sign and screen is make sure that we hook this thing
up to our off context.
So to get started at the very top of the file I'll find my import from react and I'm going to make sure
that I get use context as well.
Then I'll also add in an import or my context object from up one directory context off context and then
inside of sine in screen I'm going to get my state object and my sign in action function not from use
context and I'll pass in my context like so
OK.
Now we've got that state object we can take the error message property off of it and throw it into our
art form just as we did inside of our sign up screen right.
You're going to put in my red curly braces and I'll say state DOT error message likewise we can take
that action function of sign in and rather than passing in a ERO function in empty or a function for
on summit we can pass in sign in instead remember on submit is going to call that function automatically
with an object that has the email and password that the user entered so that should pretty much be it
for our sign and screen.
So let's save this and we can start to flip back over to our off context.
And the last thing we do is add in some additional code to sign in and go through this sign and process.
So we want to make sure that we essentially do the same exact stuff that we we did it inside of sign
up.
So we want to make sure that we put in a try catch in make our request to sign in store the token dispatch
and action to update our state navigate over to a track list and then handle an error as well.
So let's try to get through some of these steps right away because I think we've already got a pretty
good idea of what we have to do going to delete the comments inside of sign in and I'll replace it with
a try catch.
So first off let's handle the error case just in case something goes wrong with our request.
So inside if you're all dispatch and action if something goes wrong with our request or the entire sign
in process so give it a type of ad error and a payload of something went wrong with sign in
guess that's all we have do for our air handling case.
So now we can work on the actual sign in process before I do so I'm just going to refactor this aero
function right here.
I'm going to take out that return statement just as we discussed back up here with sign up we'll take
out the return statement I'm going to take out that opening curly brace right there and the closing
curly brace down there.
Just the one and then I'm going to unwind dent or dent all that stuff and then put that function declaration
on the same line.
So again what we have right here is 100 percent equivalent to what we had just a moment ago just some
slightly compacted syntax.
All right.
So inside of our try we can't tend to make our request with axes to sign our user in.
So I'll say con's response is a weight tracker API.
And then remember to sign a user in with our API we make a post request to slash sign in and we have
to provide the email and password that the user wants to sign in with.
As usual Notice I'm using the abbreviated syntax here for creating that object.
Now when we get the response we're gonna do the exact same response we had inside of sign up so we want
to make sure that we take the token we get back and story inside of async storage.
So if a user once again ever restarts their device we can sign the user back in automatically so once
we get that token back we'll call a wait async storage dot set item and we're going to set the same
key as we did up here inside of sign up.
So we're gonna set a key of token once again and then the actual value will be response dot data dot
token.
Now I'm using a wait right here.
So rather than running into the same error message we saw last time to make sure I get the async keyword
on that inner function right there right away.
Next up add in our dispatch so I'll put in a dispatch of type sign in with a payload of response dot
data dot token
and then right after that we'll make sure that we navigate after we successfully sign in and go through
all that work over to our track list.
Once again.
Now last thing we do is just make sure we add in a case to handle sign in so up inside of our dispatch
where our reduced right here that gets called by dispatch we can add in a case of sign in and I will
return.
Well guess what it actually ends up their work basically going to return the same state as what we did
up here.
So do we really need to separate cases one for sign up and sign in.
I think that realistically we can really just condense this down to one single case so rather than putting
in two essentially identical cases I'm just going to rename the existing one to sign in.
Because that's what you're really describing here.
There's not really anything to distinguish between signing up and signing in in both cases we get a
token and we store that token inside of our state object.
So I'm just going to call both them sign in because that's really what is happening.
I'm going to make sure that inside of my sign up action so your sign up rather than dispatching a action
of type sign up I mean do sign in instead.
Now let's make sure that I've got the exact same one down here inside of my sign in action as well.
Yep definitely do okay.
So I think this looks pretty good.
So let's do a quick save.
I can make sure I save everything inside of all these different files and we'll do a quick test so in
this case we want to make sure that we sign into an existing account.
If you've forgotten some of the different emails that you've used to create an account remember you
can always hop back over to that Mongo console back over here.
So here's my Mongo cluster.
We go to our cluster we select the cluster we then hit collections and then we'll see our list of users
if we tap on users right there.
So we can see all the different user accounts we've created so we can attempt to sign in with any of
these different emails.
So we're going to try to sign in as the user with email of test one at test dot com so I'll do on my
sign in screen.
I'm going to go over to sign in and I'll do test one at test dot com and my password a password.
I'll sign in and sure enough something went wrong so I'm sure I probably made another small typo.
Hopefully it wasn't as bad as my last one.
I'll check my terminal really quickly we'll do some live debugging this time around.
So looks like nothing quite happened here.
So a good way to catch this.
Remember we inside of our sign in right here we've got our try catch.
So right now if anything goes wrong inside of these statements right here we essentially get thrown
down into the catch block since we're catching that air we didn't see any air appear inside of our console
because that I'm very certain that something must be going wrong with that segment right there.
So I got to put in a quick console log right here and a console log out the air that occurred during
those lines and I'll try to sign in once more
sign in.
And now I should see a console describing exactly what occurred.
So I'll flip back over and I can see that I got a status code of 22 which means that I'm definitely
not sending across my email and password correctly.
So what's going on here.
Well I think unfortunately I'm going to do a little bit of debugging once again.
I can't believe this.
The same thing.
Both times testing out sign up and sign and ran into an error both times.
So once again I'm going to do a little bit of troubleshooting here figure out what's going on.
Let's come back and less.
Next video and figure out exactly what did not quite work.
A quick pause and I'll see you in just a minute.