1 00:00:02,230 --> 00:00:03,470 So now that we added 2 00:00:03,510 --> 00:00:04,290 sign up, 3 00:00:04,290 --> 00:00:09,440 let's also add log in. For this, in the auth handler in the app.js file, 4 00:00:09,450 --> 00:00:15,330 we need to find an alternative to signing a user up as we're currently doing it and previously, I had an if check 5 00:00:15,330 --> 00:00:16,050 here. 6 00:00:16,050 --> 00:00:18,920 So let me reintroduce that if check here 7 00:00:21,290 --> 00:00:26,170 and I'll again work with that request variable which I'll also re-add here 8 00:00:26,540 --> 00:00:34,090 and now request either holds my email pass client register with e-mail logic, 9 00:00:34,090 --> 00:00:38,300 that happens here when I want to sign up, 10 00:00:38,330 --> 00:00:45,480 so then request is equal to the result of that and I can then use request down there 11 00:00:46,630 --> 00:00:48,820 or I do log in 12 00:00:48,820 --> 00:00:53,840 and now how do I log in with stitch? For that I need to add an import 13 00:00:53,840 --> 00:00:57,680 first of all, I need to import the user password credential, 14 00:00:57,740 --> 00:01:00,480 you remember? Previously we used the anonymous credential, 15 00:01:00,500 --> 00:01:03,180 now we simply use the user password credential. 16 00:01:03,320 --> 00:01:09,060 So I am importing that and I'll now create a new credential here, 17 00:01:10,010 --> 00:01:10,690 credential, 18 00:01:14,710 --> 00:01:19,480 a new user password credential with the two pieces of information I have, 19 00:01:19,480 --> 00:01:27,680 so with the e-mail and the password. So I'll again pass auth data e-mail here and auth data password as a 20 00:01:27,670 --> 00:01:29,050 a second argument. 21 00:01:29,050 --> 00:01:35,700 So now with that credential, we can log in just as we did it before with the anonymous credential. 22 00:01:35,720 --> 00:01:37,800 So here to login, 23 00:01:38,060 --> 00:01:41,700 I'll actually use my general stitch client with this client, 24 00:01:41,900 --> 00:01:50,030 then auth and there, I can call log in with credential and pass my credential which I created here as 25 00:01:50,030 --> 00:01:52,120 an argument. 26 00:01:52,120 --> 00:01:57,820 Now this will give me a promise and hence I can safely store this in request and then here let's see 27 00:01:57,820 --> 00:02:00,160 what we have as a result now, 28 00:02:00,550 --> 00:02:07,480 so here, so in here let me console log result. 29 00:02:07,550 --> 00:02:10,260 So now we should have a log inflow too, 30 00:02:10,580 --> 00:02:12,200 so let's go back to our application, 31 00:02:12,230 --> 00:02:18,200 make sure you're in log in mode and then try the credentials you used for signing up with your e-mail 32 00:02:18,290 --> 00:02:22,670 and of course, the correct password. 33 00:02:22,780 --> 00:02:25,850 Now I get a reply here and that reply looks positive, 34 00:02:25,860 --> 00:02:27,100 it's not an error 35 00:02:27,390 --> 00:02:33,360 and this simply confirms to me that I am logged in and the whole session of the user to the tokens to 36 00:02:33,360 --> 00:02:36,390 be precise are managed by stitch behind the scenes. 37 00:02:36,390 --> 00:02:41,640 You can by the way see them if you go to the application tab in your chrome browser dev tools and you 38 00:02:41,640 --> 00:02:43,620 then check out your local storage, 39 00:02:43,770 --> 00:02:50,550 you should see that stitch client thing here which in the end stores information about your user, 40 00:02:50,760 --> 00:02:53,520 where you see for example the token that is managed for you. 41 00:02:53,670 --> 00:02:56,950 So that is what stitch does for you, you don't need to worry about that, 42 00:02:57,060 --> 00:02:59,720 you now have a way of logging in. 43 00:02:59,730 --> 00:03:06,540 So what you know here by this point is that if result is set because it was undefined for signing up 44 00:03:06,540 --> 00:03:07,270 if you remember, 45 00:03:07,380 --> 00:03:11,460 so if we have a value for result, then we are logged in 46 00:03:11,490 --> 00:03:17,890 and in this case, I want to also change my authentication status, 47 00:03:17,930 --> 00:03:24,250 so then I want to call this set state is auth true so that I also reflect this in my user interface. 48 00:03:24,310 --> 00:03:28,780 So now again if I do enter my valid credentials here, 49 00:03:32,070 --> 00:03:33,150 I am logged in 50 00:03:33,150 --> 00:03:34,910 and now I load my products. 51 00:03:34,920 --> 00:03:40,500 Now let's also adjust our rules such that only logged in users can interact with the products.