Skip to content
This repository has been archived by the owner on May 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #10 from RodolfoSilva/multiple_roles
Browse files Browse the repository at this point in the history
Add multiple role support
  • Loading branch information
RodolfoSilva authored Sep 12, 2019
2 parents de24d3f + dfda44b commit fb9d014
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,33 @@ Create tables and initial state for your user management.

```sql
CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA public;

CREATE TABLE "role" (
name text NOT NULL PRIMARY KEY,
created_at timestamp with time zone NOT NULL DEFAULT now(),
);

INSERT INTO "roles" ("name") VALUES ('user');

CREATE TABLE "user" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
email citext NOT NULL UNIQUE,
password text NOT NULL,
role text NOT NULL DEFAULT 'user',
default_role text NOT NULL DEFAULT 'user',
is_active boolean NOT NULL DEFAULT false,
secret_token uuid NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now()
updated_at timestamp with time zone NOT NULL DEFAULT now(),
FOREIGN KEY (default_role) REFERENCES role (name)
);

CREATE TABLE "user_role" (
id uuid DEFAULT gen_random_uuid() NOT NULL CONSTRAINT user_role_pkey PRIMARY KEY,
user_id uuid NOT NULL CONSTRAINT user_role_user_id_fkey REFERENCES "user" ON UPDATE CASCADE ON DELETE CASCADE,
role text NOT 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)
);

CREATE TABLE "user_session" (
Expand Down
6 changes: 3 additions & 3 deletions src/auth-tools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jwt from 'jsonwebtoken';
import uniq from 'lodash/uniq';
import { User } from './hasura';
import * as vars from './vars';

Expand All @@ -23,9 +24,8 @@ export const generateJwtRefreshToken = (payload: any) => {
export const generateClaimsJwtToken = (user: User, sessionId: string) => {
const payload = {
[vars.hasuraGraphqlClaimsKey]: {
[`${vars.hasuraHeaderPrefix}allowed-roles`]: user.role,
[`${vars.hasuraHeaderPrefix}default-role`]: user.role,
[`${vars.hasuraHeaderPrefix}role`]: user.role,
[`${vars.hasuraHeaderPrefix}allowed-roles`]: uniq([user.default_role, ...user.user_roles.map(({ role }) => role)]).filter(role => !!role),
[`${vars.hasuraHeaderPrefix}default-role`]: user.default_role,
[`${vars.hasuraHeaderPrefix}user-id`]: user.id.toString(),
[`${vars.hasuraHeaderPrefix}session-id`]: sessionId,
},
Expand Down
6 changes: 5 additions & 1 deletion src/hasura/user-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export const USER_FRAGMENT = gql`
password
is_active
secret_token
role
default_role
user_roles {
id
role
}
created_at
updated_at
}
Expand Down
8 changes: 7 additions & 1 deletion src/hasura/user-type.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
interface Role {
id: string;
role: string;
}

export interface User {
id: string;
email: string;
is_active: boolean;
role: string;
default_role: string;
user_roles: [Role];
password: string;
created_at: Date;
updated_at: Date;
Expand Down
2 changes: 1 addition & 1 deletion src/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const typeDefs = gql`
email: String!
is_active: String!
default_role: String!
roles: [Role!]!
user_roles: [Role!]!
created_at: String!
}
Expand Down

0 comments on commit fb9d014

Please sign in to comment.