/nodeJs-JWT-API

NodeJs API for JWT authentication and Authorization. No Static assets are served. Only the API s are exposed via the URL.

Primary LanguageJavaScript

NodeJs JWT Authentication API

Environment vars to be updated

Update these env variables in .env file or platform specific environment

 * - required env vars(if not set, will fallback to predefined defaults) 
DB_CONN - remote db connection uri
JWT_SECRET* - secret for jwt
JWT_REFRESH_SECRET* - secret for refresh token
JWT_EXP - expiration time for jwt(s)
JWT_SECRET_EXP - expiration time for refresh token(s)
REDIS_HOST - redis db host uri
REDIS_PORT - redis db port
REDIS_PASS - redis db password
REDIS_EXP - Expiration for a Redis key
PORT - server port/default 3000

Redis & MongoDB should be installed either locally if remote connections are not provided & running in default configs.

Fallback defaults
MongoDB => db name - 'local', port - 27017
Redis => host - localhost, port - 6379


Available endpoints

  • Register - Validates & create new user

POST
/user/register
{
    "name":"",
    "email":"",
    "password":""
}

response body - { user_id: created user's ID in DB }
  • Login - returns a JWT

POST
/user/login
{
    "email":"",
    "password":""
}

response body - { token: JWT, refreshtoken: refresh JWT } 
  • Private routes - Can be accessed only via presenting valid token inside 'auth-token' custom header

test private Route:
/get

request header: 'auth-token: a valid JWT'