Logo

dev-resources.site

for different kinds of informations.

Using Authorizer with Hasura

Published at
4/19/2022
Categories
hasura
oauth
opensource
graphql
Author
lakhansamani
Categories
4 categories in total
hasura
open
oauth
open
opensource
open
graphql
open
Author
12 person written this
lakhansamani
open
Using Authorizer with Hasura

Hasura gives you instant GraphQL / Rest API on top of SQL databases like Postgres and MySQL.

It takes away the pain of writing basic CRUD (Create, Read, Update, Delete) APIS. It also gives column and row level authorization layer using JWT tokens. This helps in having secured APIs without writing any backend code.

On the other hand Authorizer is database independent open source authentication and authorization solution built using GraphQL. You can bring in your database and have authentication layer ready for your users in seconds. Motive of Authorizer is to save your user data in your database without having the pain of developing highly secure auth system.

In technical terms you get an JWT token with authorizer.dev which you can use with Hasura to verify the permission for a given user.

You can check a video tutorial for same

Now you get following things:

  • GraphQL API out of the box with Hasura for your database
  • Authentication with Authorizer
  • Authorization (Role based access) with Hasura auth system
  • Secure session management with Authorizer
  • Multiple login recipes with Authorizer
  • And your data stays within in your database

Here is how the broader picture looks like

hasur-authorizer-architecture

Also authorizer-react / authorizer-js takes away the pain of

  • Securely managing JWT token
  • Creating login / signup page
  • Creating forgot password page

Isn’t that great! all in one place and under your control 🎉

No more need to pay 3rd party applications which can own your data and you might have to write some logic to stitch the data with your database.

gif

Here are 5 simple steps to achieve this

Step 1: Deploy Authorizer instance

Deploy production ready Authorizer instance using one click deployment options available below

Infra provider One-click link Additional information
Railway.app Deploy on Railway docs
Heroku Deploy to Heroku docs
Render render button docs

For more information check docs

Step 2: Setup Instance

  • Open authorizer instance endpoint in browser
  • Signup with a secure password
  • Configure social logins / smtp server and other environment variables based on your needs

For more information please check docs

Step 3: Setup Hasura Instance

Step 4: Configure Database with Hasura

  • Open the dashboard of hasura cloud ( go to https://cloud.hasura.io/)
  • Click on settings icon of your hasura project ( which is in top-right corner )
  • Go to Env vars section

Check the hasura docs for more information.

Note if you have used single click deployment option for authorizer you can get database URL from respective platform's env sections.

Step 5: Configure JWT token Authorization Script

In order for Hasura to authorize a user, JWT token needs to have specific keys, you can add those keys by modifying JWT token script in your Authorizer Dashboard.

Example:

function(user,tokenPayload) {
  var data = tokenPayload;
  data['https://hasura.io/jwt/claims'] = {
    'x-hasura-user-id': user.id,
    'x-hasura-default-role': tokenPayload.allowed_roles[0],
    'x-hasura-allowed-roles': user.roles
  }

  return data;
}
Enter fullscreen mode Exit fullscreen mode

sample

Once user login they get id_token which should be used with hasura queries as Authorization: Bearer ID_TOKEN. This will help in making Authorized requests.

You can configure access control for various roles that your application needs. You can also configure same roles in your authorizer dashboard.

For more information on access control check hasura docs

You can also stitch Authorizer Graphql Endpoint with Hasura Remote Schema, that way you can have single endpoint for all your GraphQL queries / mutations.

For more information check:

Site: https://authorizer.dev
Docs: https://docs.authorizer.dev
Youtube: https://youtube.com/playlist?list=PLSQGbUjHc6bpaAgCiQPzNxiUPr7SkDAFR
Github: https://github.com/authorizerdev/authorizer
React-SDK: https://github.com/authorizerdev/authorizer-react
JS-SDK: https://github.com/authorizerdev/authorizer-js
Join Discord: https://discord.gg/Zv2D5h6kkK

hasura Article's
30 articles in total
Favicon
Convert insert mutation to upsert
Favicon
refinedev - hasura (nested/multiple query_root)
Favicon
From Idea to Launch: My 30-Day MVP Journey
Favicon
How to Build a GraphQL API for MongoDB Using Hasura in Six Steps
Favicon
How to Build a Supergraph using Snowflake, Neon PostgreSQL, and Hasura in Five Steps
Favicon
Streamlining CI/CD Pipelines with Hasura GraphQL Engine
Favicon
Supabase over Hasura for 2024?
Favicon
Hasura and Keycloak integration with NestJS server
Favicon
Modern API Development (Part 1)
Favicon
Build a graphQL API with Hasura low-code platform
Favicon
Startup Starter Kit
Favicon
Hasura x MEWS
Favicon
Hasura vs Apollo: Comparing GraphQL Platforms
Favicon
Hasura and Nhost vs Supabase
Favicon
How to monitor URQL performance and link with database queries ?
Favicon
Build a Powerful GraphQL API with Postgres in Under 10 Minutes
Favicon
Guide to Side effects in Hasura
Favicon
Hasura: Building Scalable and Real-Time Applications - An Extensive Guide
Favicon
Hasura Cloud: Building Scalable and Secure GraphQL APIs Made Easy
Favicon
How to avoid messing up squash migration in Hasura
Favicon
Appwrite vs. Hasura vs. Apollo
Favicon
Auth0, Hasura, Social Media Login
Favicon
Deno & Hasura for app development
Favicon
SSR: clerk with hasura
Favicon
Hasura Custom Authentication Using JWT
Favicon
Unable to HASURA_GRAPHQL_JWT_SECRET in docker-compose file
Favicon
Hasura Storage in Go: 5x performance increase and 40% less RAM
Favicon
Using Authorizer with Hasura
Favicon
Hasura + Supertokens
Favicon
Creating a todo web app in < 112 lines of code with Hasura and Python

Featured ones: