1 00:00:02,440 --> 00:00:04,960 How do we authenticate in stitch? 2 00:00:05,260 --> 00:00:11,670 Well for authentication, we can dive into stitch again and then under users in the stitch console, under 3 00:00:11,670 --> 00:00:16,270 providers you see different ways of authenticating users. 4 00:00:16,390 --> 00:00:21,580 You can use third party providers like Facebook or Google, you can manage your own e-mails and passwords 5 00:00:21,610 --> 00:00:24,910 and you can even allow anonymous authentication 6 00:00:25,000 --> 00:00:27,870 and that is what I will turn on now by simply switching that. 7 00:00:28,030 --> 00:00:32,120 So now if I save that, people can log in anonymously 8 00:00:32,140 --> 00:00:33,460 and I'll show you how that works 9 00:00:33,490 --> 00:00:36,180 and we'll switch to real users later too. 10 00:00:36,440 --> 00:00:38,620 So I want to log in anonymously, 11 00:00:38,620 --> 00:00:39,840 how does that work? 12 00:00:40,390 --> 00:00:47,420 Well for that, I'll go back to the app.js file, in there where I initialize my app client, here 13 00:00:47,470 --> 00:00:54,060 I also can log in. For that, I'll simply store the client which will be returned by the initialize default app 14 00:00:54,060 --> 00:00:57,490 client method, I'll store the client in a constant 15 00:00:57,900 --> 00:01:06,310 and now for anonymous authentication, I need to import anonymous credential, 16 00:01:06,420 --> 00:01:07,910 that is something I need to import here, 17 00:01:07,980 --> 00:01:13,680 anonymous credential from the mongodb stitch browser sdk. With that imported here, we can call 18 00:01:13,680 --> 00:01:18,280 client.auth to access the auth features of the stitch sdk 19 00:01:18,630 --> 00:01:24,460 and then here, we can call log in with credential. 20 00:01:24,850 --> 00:01:30,160 Now to this credential, we can pass different credentials and the credential then depends on the 21 00:01:30,160 --> 00:01:33,730 authentication methods you want to use and you unlocked in your console, 22 00:01:33,730 --> 00:01:37,520 so in my case I only unlocked anonymous authentication for now, 23 00:01:37,600 --> 00:01:45,490 so here I will pass the new anonymous credential. And for anonymous credential, we don't need to pass anything 24 00:01:45,490 --> 00:01:45,670 else, 25 00:01:45,670 --> 00:01:49,740 we can just pass it like this and this will log us in anonymously. 26 00:01:49,810 --> 00:01:55,370 If we now save that and we let our app reload, 27 00:01:55,410 --> 00:02:00,040 you see we don't get this error anymore but I still get another error, 28 00:02:00,420 --> 00:02:06,930 I get a stitch request error saying that unexpected token in json add something occured while decoding 29 00:02:06,930 --> 00:02:08,780 a response from stitch. 30 00:02:08,790 --> 00:02:13,270 Now this error is stemming from the products.js file, line forty five 31 00:02:13,470 --> 00:02:18,260 and if we have a look at that file, we can quickly see well it's coming from this console log, 32 00:02:18,270 --> 00:02:21,020 so we get an error when we try to fetch products. 33 00:02:21,270 --> 00:02:26,370 Now we are authenticated and still we get an error and that is related to the rules we need to set 34 00:02:26,370 --> 00:02:28,470 up for accessing our database 35 00:02:28,470 --> 00:02:32,200 because as I mentioned, by default nothing is accessible, 36 00:02:32,220 --> 00:02:38,550 everything is locked down and you have to inform stitch which user or which authenticated entity because 37 00:02:38,550 --> 00:02:43,110 right now we have that anonymous authentication but we still have an authenticated entity there, 38 00:02:43,380 --> 00:02:45,870 what kind of access is allowed there?