Skip to content

Small Nest.js microservice proof of concept to handle Authentification&Authorization via GraphQL interface and PostgreSQL db.

License

Notifications You must be signed in to change notification settings

NotNikita/NestJsAuthService

Repository files navigation

NestJsAuthService

Small Nest.js microservice proof of concept to handle Authentification&Authorization via GraphQL interface and PostgreSQL db.

Setup

Prerequisites:

  1. Deployed Vercel PosgreSQL db
  2. .env populated with variables from this db 2.1 .env has JWT_SECRET variable, that can be any string
  3. pnpm install

Prisma setup

  1. pnpm prisma generate
  2. pnpm prisma migrate dev --name init
  3. pnpm prisma migrate deploy

How to check that tables and schema were created?

psql -h your-host -U your-user -d your-database
\dn  # to check schemas
\dt your_schema.*  # to check tables in schema

Running the app

pnpm start go to http://localhost:3000/graphql.

Within the app:

Testing will be done inside of GraphQL playground There are 4 unauthorized functions: Example

First create user/admin:

mutation {
  register(email: "name@users.com", password: "password", role: ADMIN)
}

Now you will be able to do one of the following queries:

mutation {
  findUsersByRole(role: ADMIN)
}
mutation {
  findOneByEmail(email: "name@users.com")
}

To check Authorized function, implemented via @nestjs/passport AuthGuard

  1. First
mutation {
  login(email: "name@users.com", password: "password") {
    access_token
  }
}
  1. You will receive "access_token" which you need to copy Example

  2. Then open f12->Application->Cookies->Create new cookie with name of access_token and value that you copied Example

  3. Then in http://localhost:3000/graphql. click Settings in top-right corner and change following line like this: "request.credentials": "include"

  4. This is done, because GraphQL playground has broken Cookies setting and is not allowing to attach them easily

  5. Try to create new user via admin rights:

mutation {
  createUser(email: "created@users.com")
}

Example

  1. Thanks for attention

About

Small Nest.js microservice proof of concept to handle Authentification&Authorization via GraphQL interface and PostgreSQL db.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published