So now that we added
sign up,
let's also add log in. For this, in the auth handler in the app.js file,
we need to find an alternative to signing a user up as we're currently doing it and previously, I had an if check
here.
So let me reintroduce that if check here
and I'll again work with that request variable which I'll also re-add here
and now request either holds my email pass client register with e-mail logic,
that happens here when I want to sign up,
so then request is equal to the result of that and I can then use request down there
or I do log in
and now how do I log in with stitch? For that I need to add an import
first of all, I need to import the user password credential,
you remember? Previously we used the anonymous credential,
now we simply use the user password credential.
So I am importing that and I'll now create a new credential here,
credential,
a new user password credential with the two pieces of information I have,
so with the e-mail and the password. So I'll again pass auth data e-mail here and auth data password as a
a second argument.
So now with that credential, we can log in just as we did it before with the anonymous credential.
So here to login,
I'll actually use my general stitch client with this client,
then auth and there, I can call log in with credential and pass my credential which I created here as
an argument.
Now this will give me a promise and hence I can safely store this in request and then here let's see
what we have as a result now,
so here, so in here let me console log result.
So now we should have a log inflow too,
so let's go back to our application,
make sure you're in log in mode and then try the credentials you used for signing up with your e-mail
and of course, the correct password.
Now I get a reply here and that reply looks positive,
it's not an error
and this simply confirms to me that I am logged in and the whole session of the user to the tokens to
be precise are managed by stitch behind the scenes.
You can by the way see them if you go to the application tab in your chrome browser dev tools and you
then check out your local storage,
you should see that stitch client thing here which in the end stores information about your user,
where you see for example the token that is managed for you.
So that is what stitch does for you, you don't need to worry about that,
you now have a way of logging in.
So what you know here by this point is that if result is set because it was undefined for signing up
if you remember,
so if we have a value for result, then we are logged in
and in this case, I want to also change my authentication status,
so then I want to call this set state is auth true so that I also reflect this in my user interface.
So now again if I do enter my valid credentials here,
I am logged in
and now I load my products.
Now let's also adjust our rules such that only logged in users can interact with the products.