Skip to content

๐Ÿ” Complete API for user authentication with email confirm & JWT & roles.

Notifications You must be signed in to change notification settings

cheatsnake/auth-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

21 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ” Auth API

Complete API for user authentication with email confirm & JWT & roles.

โฌ†๏ธ Stack

  • TypeScript
  • Express.js
  • PostgreSQL (pg)
  • Nodemailer
  • JsonWebToken
  • BcryptJS

๐Ÿš€ Setup local server

  1. Clone this repo and install dependencies:
npm install
  1. Create a new database and use SQL scripts from sql folder to create required tables.

  2. Create .env file with required credentials:

# Base URL of your server
API_URL=http://localhost:5000

# Connection to PostgreSQL
POSTGRES_USER=postgres
POSTGRES_PASSWORD=root
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=postgres

# Random secret keys to encrypt data by JWT
JWT_ACCESS_SECRET=SomeSecretKey
JWT_REFRESH_SECRET=AndAnotherSecretKey

# Data to access an email account to send activation messages
SMTP_USER=example@mail.com
SMTP_PASSWORD=EmailPassword
SMTP_SERVICE=EmailService
  1. Create production build & run server:
npm run build
npm start

Or launch server in develepment mode with nodemon:

npm run dev

Base server url: http://localhost:5000

๐Ÿณ Run with docker

To start the server and database in docker containers you only need to execute 2 simple commands inside the project directory:

docker-compose build
docker-compose up

๐Ÿ“Œ End-points

  • POST /auth/register - Register a new user
{
    "username": "user",
    "firstName": "User",
    "lastName": "User",
    "email": "user@mail.com",
    "password": "123456"
}
  • POST /auth/login - Enter to the account by username & password
{
    "username": "user",
    "password": "123456"
}
  • POST /auth/logout - Logout from the account & clear cookies
  • GET /auth/refresh - Update life time of access token by refresh token that stored in cookies
  • GET /public - Route that are available to everyone
  • GET /guest - Route that are available to all registered users (Need Authorization header with access token)
  • GET /protected - Route that are available to all users with verified email (Need Authorization header with access token)
  • GET /admin - Route that are available only for admins (Need Authorization header with access token)