Giter Site home page Giter Site logo

cheatsnake / auth-api Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 326 KB

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

TypeScript 98.95% JavaScript 0.56% Dockerfile 0.49%
jwt jwt-authentication email-verification roles api rest-api express nodejs typescript postgresql pg sql authentication registration

auth-api's Introduction

๐Ÿ” 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=[email protected]
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": "[email protected]",
    "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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.