Complete API for user authentication with email confirm & JWT & roles.
- TypeScript
- Express.js
- PostgreSQL (pg)
- Nodemailer
- JsonWebToken
- BcryptJS
- Clone this repo and install dependencies:
npm install
-
Create a new database and use SQL scripts from
sql
folder to create required tables. -
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
- 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
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
- 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)