Hasura Setup

Alright, we're about to begin our foray into authentication with Hasura.

First off, we need to do some set up.

Config Variables

Since we have deployed Hasura GraphQL Engine on Hasura Cloud, let's head to Hasura Cloud Dashboard to configure the JWT secret.

Open the Hasura Cloud dashboard and head to the "Env vars" page for your Hasura Cloud project:

Hasura ENV Config

Click on + New Env Var to add a new environment variable.

Add a new Config Var called HASURA_GRAPHQL_JWT_SECRET, and copy and paste the json below into the value box.

{
"type": "HS256",
"key": "JWTSecretMustBeAtLeast32CharactersLong!"
}

You should end up with something like the following:

Replace the key with any key you want but it must be at least 32 characters long.

You should end up with something like the following:

JWT Secret ENV

Permissions and Roles

Next up, go to your Hasura console. Refresh it if it was already open. You'd be asked to login to Hasura Cloud Dashboard.

Next up, go to the Data tab and click the users table.

We shall add another column called

password (String, nullable)

Password column

Go to Insert Row and add another mock user

Mock user

Next, go to the Permissions tab and create a new role called user

Set Insert, Update, Select, and Delete permissions

Users Insert Insert

Users Select Select

Users Delete Delete

Users Update Update

The Update permissions can also be set by using this line of code

{"id":{"_eq":"X-Hasura-User-Id"}}

Go to permissions in battles

Set Insert, Update, Select, and Delete permissions

Battles Insert Insert

Battles Select Select

Battles Delete Delete

Battles Update Update

The Update permissions can also be set by using this line of code

{"_or":[{"_or":[{"shooter_id":{"_is_null":true}},{"shooter_id":{"_eq":"X-Hasura-User-Id"}}]},{"_or":[{"defender_id":{"_is_null":true}},{"defender_id":{"_eq":"X-Hasura-User-Id"}}]}]}

Go to permissions in old_battles

Set Select and Delete permissions

Old Battles Select Select

Old Battles Delete Delete

Go to permissions in online_battles

Set Select permissions

Online Battles Select Select

And that's it. Those are the permissions set up for our Shooter Arena game. Next up, we set up our jwt service provider on glitch.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin