Skip to content

Repository files navigation

Hasura Auth [WIP]

Inspired by the hasura-backend-plus

Setup

Create tables and initial state for your user management.

CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA public;
CREATETABLE "role" (
name textNOT NULLPRIMARY KEY,
created_at timestamp with time zoneNOT NULL DEFAULT now()
);
INSERT INTO"role" ("name") VALUES ('user');
CREATETABLE "organization" (
id uuid DEFAULT gen_random_uuid() NOT NULLCONSTRAINT organization_pkey PRIMARY KEY,
name textNOT NULL,
created_at timestamp WITH TIME ZONE DEFAULT now() NOT NULL,
updated_at timestamp WITH TIME ZONE DEFAULT now() NOT NULL
);
CREATETABLE "user" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id uuid NULLCONSTRAINT user_organization_id_fkey REFERENCES"organization"ONUPDATE CASCADE ON DELETE CASCADE,
email citext NOT NULL,
password textNOT NULL,
default_role textNOT NULL DEFAULT 'user',
is_active booleanNOT NULL DEFAULT false,
secret_token uuid NOT NULL,
created_at timestamp with time zoneNOT NULL DEFAULT now(),
updated_at timestamp with time zoneNOT NULL DEFAULT now(),
CONSTRAINT user_email_organization_id_key UNIQUE (email, organization_id),
FOREIGN KEY (default_role) REFERENCES role (name)
);
CREATETABLE "user_role" (
id uuid DEFAULT gen_random_uuid() NOT NULLCONSTRAINT user_role_pkey PRIMARY KEY,
user_id uuid NOT NULLCONSTRAINT user_role_user_id_fkey REFERENCES"user"ONUPDATE CASCADE ON DELETE CASCADE,
role textNOT NULL,
created_at timestamp WITH TIME ZONE DEFAULT now() NOT NULL,
updated_at timestamp WITH TIME ZONE DEFAULT now() not null,
CONSTRAINT user_role_user_id_role_key UNIQUE (user_id, role)
);
CREATETABLE "user_session" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_agent textNULL,
ip_address textNULL,
user_id uuid NOT NULL,
refresh_token textNOT NULL,
expires_at timestamp with time zoneNOT NULL,
created_at timestamp with time zoneNOT NULL DEFAULT now(),
updated_at timestamp with time zoneNOT NULL DEFAULT now(),
FOREIGN KEY (user_id) REFERENCES"user" (id)
);

Track your tables and relations in Hasura

Go to the Hasura console. Click the "Data" menu link and then click "Track all" under both "Untracked tables or views" and "Untracked foreign-key relations"

Enable auth hook

Set your hasura environment to use this configuration:

HASURA_GRAPHQL_AUTH_HOOK=https://<your-domain-with-hasura-auth>/hook

Read more...

Use Remote Schema

Go to the Hasura console. Clink the "Remote Schemas" menu link and then click "Add".

Set a name to "Remote Schema name" like you prefer eg. (hasura-auth) and set the "GraphQL server URL" to use tbe url https://<your-domain-with-hasura-auth>/graphql/ In "Headers for the remote GraphQL server" select the option "Forward all headers from client". After that clink an "Add Remote Schema" button

Environment

namedefaultdescription
PORT4000Express server port
HASURA_GRAPHQL_ENDPOINThttp://graphql-engine:8080/v1alpha1/graphqlEndpoit to hasura server
HASURA_GRAPHQL_ADMIN_SECRETNULLAdmin secrete key of hasura console
HASURA_GRAPHQL_CLAIMS_KEYhttps://hasura.io/jwt/claimsKey hequired by hasura in JWT
HASURA_GRAPHQL_HEADER_PREFIXx-hasura-Hasura header prefix
USER_REGISTRATION_AUTO_ACTIVEtrueAuto active the user account
REFRESH_TOKEN_EXPIRES_IN43200Life time in minutes of refresh token
JWT_ALGORITHMHS256JWT Algorithm
JWT_PRIVATE_KEYsecretkeyJWT Secret key used to generate token
JWT_TOKEN_EXPIRES15Life time in minutes of JWT
ALLOW_REGISTRATION_FOR*Allow registration by role
ALLOW_EMPTY_ORGANIZATIONtrueAllow use empty organization

Todo

  • Google Login
  • Facebook Login
  • Twitter Login

License

MIT

About

🔐 Hasura Auth 🔐

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages