How do we authenticate in stitch?
Well for authentication, we can dive into stitch again and then under users in the stitch console, under
providers you see different ways of authenticating users.
You can use third party providers like Facebook or Google, you can manage your own e-mails and passwords
and you can even allow anonymous authentication
and that is what I will turn on now by simply switching that.
So now if I save that, people can log in anonymously
and I'll show you how that works
and we'll switch to real users later too.
So I want to log in anonymously,
how does that work?
Well for that, I'll go back to the app.js file, in there where I initialize my app client, here
I also can log in. For that, I'll simply store the client which will be returned by the initialize default app
client method, I'll store the client in a constant
and now for anonymous authentication, I need to import anonymous credential,
that is something I need to import here,
anonymous credential from the mongodb stitch browser sdk. With that imported here, we can call
client.auth to access the auth features of the stitch sdk
and then here, we can call log in with credential.
Now to this credential, we can pass different credentials and the credential then depends on the
authentication methods you want to use and you unlocked in your console,
so in my case I only unlocked anonymous authentication for now,
so here I will pass the new anonymous credential. And for anonymous credential, we don't need to pass anything
else,
we can just pass it like this and this will log us in anonymously.
If we now save that and we let our app reload,
you see we don't get this error anymore but I still get another error,
I get a stitch request error saying that unexpected token in json add something occured while decoding
a response from stitch.
Now this error is stemming from the products.js file, line forty five
and if we have a look at that file, we can quickly see well it's coming from this console log,
so we get an error when we try to fetch products.
Now we are authenticated and still we get an error and that is related to the rules we need to set
up for accessing our database
because as I mentioned, by default nothing is accessible,
everything is locked down and you have to inform stitch which user or which authenticated entity because
right now we have that anonymous authentication but we still have an authenticated entity there,
what kind of access is allowed there?