Skip to main content

auth-be

This plugin provides authentication functionality for Rest & GraphQL APIs.

Tags

Backend, Authentication

Features Overview

  • User registration via email: Allows users to create new accounts using their email addresses.
  • User login via email: Enables users to authenticate using their email and password.
  • Forgot & password reset: Helps users with a password reset process via email.
  • Renew access token: Enable users to renew access tokens.

Dependencies:

  • NestJS: A framework for building efficient and scalable server-side applications.
  • GraphQL: A query language for APIs that enables clients to request and retrieve specific data.
  • Prisma: An open-source ORM for Node.js and TypeScript that simplifies database interactions.
  • Postgres: Postgres is an open-source relational database management system that supports SQL (relational) and JSON (non-relational) querying.

Project Structure

auth-be/
|--src/
| |-- index.ts -> (plugin installation scripts)
|--template -> (nestjs project)
| |--src/
| | ├── app.module.ts
| | ├── auth
| | ├── authorization
| | ├── common
| | │   ├── dto
| | │   ├── enums
| | │   ├── filters
| | │   ├── interceptor
| | │   └── logger
| | ├── config
| | │   ├── env-config.module.ts
| | │   └── env.config.ts
| | ├── db
| | │   ├── @generated
| | │   │   └── index.ts
| | │   ├── auth
| | │   │   ├── auth-db.repository.ts
| | │   │   └── auth-db.service.ts
| | │   ├── db.module.ts
| | │   ├── db.service.ts
| | │   ├── dto
| | │   ├── helpers
| | │   └── prisma
| | └── main.ts

Installation

To install the auth-be plugin, you can use the following command:

npx create-gs-project@latest add auth-be

Running the app

# Setup the Prisma client
$ npm run prisma:setup

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Post Installation steps

  1. yarn install or npm install
  2. Create a .env.development file with all the variables present in the .env.example file or run cp .env.example .env.
  3. Start the Docker containers: docker-compose up -d
  4. Setup prisma models : npm run prisma:setup
  5. Seed the database: npm run prisma:seed
  6. Start the development server: npm run start:dev
  7. Access GraphQL at: http://localhost:3000/graphql
  8. Access Swagger at: http://localhost:3000/api

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Methods

authenticateUser

Validates user credentials (email and password) and returns an authentication response with tokens upon success.

registerUser

Registers a new user using the provided email and password.

renewAccessToken

Refreshes the access token for the current user.

initiatePasswordReset

Initiates the password reset process by sending a reset link to the user's email.

completePasswordReset

Resets the user's password to the provided new password.