Skip to content

Express TypeScript API Authentication with MongoDB, JWT & Unit Testing with Mocha / Chai

Notifications You must be signed in to change notification settings

torredefarol24/node-mg-rd-ch-jwt-ts

Repository files navigation

Node Authentication in TypeScript

Using

  • JWT (For Stateless Auth)
  • Chai + Mocha (For Testing)
  • Redis (For In-Memory DB)
  • MongoDB + Mongoose (For Persistent DB)
  • Express in TypeScript

To test this,

Run Docker Container

  • Pull Docker Image
$ cd nodets-mongojwt-chai
$ docker pull burningraven06/nodets-jwt-mongo-chai-rds
  • Start Mongo Container
$ docker pull mongo
$ docker run --name <MONGO_INSTANCE> -d mongo
  • Start Redis Container
$ docker pull redis
$ docker run --name <REDIS_INSTANCE> -d redis

# You may need to update your docker redis.conf
  • Start App & Link Containers
$ docker run --name <APP_INSTANCE> --link <MONGO_INSTANCE_NAME>:mongo --link <REDIS_INSTANCE> -p 5000:4000 -d burningraven06/nodets-jwt-mongo-chai-rds

# Visit localhost:5000 

Run App Locally

Create Necessary Files

  • Create DBKeys
$ cd nodets-mongojwt-chai
$ touch src/keys/dbKeys.ts
  • Update DBKEys
const dbKeys = {
  mongoLocalUrl : <YOUR_MONGODB_URI>,
  mongoDockerURL : ""
}

export default dbKeys
  • Create JWTSecrets
$ cd nodets-mongojwt-chai
$ touch src/keys/jwtSecret.ts
  • Update JWTSecrets
const jwtKeys = {
  tokenSecret : <YOUR_TOKEN_SECRET>,
  resfreshTokenSecret : <YOUR_REFRESHTOKEN_SECRET>
}

export default jwtKeys
  • Update src/app.ts
$ cd nodets-mongojwt-chai/src/   #Open app.ts

#Change Line 25
mongoDBConfig.dockerSetup()

# to
mongoDBConfig.localSetup()
  • Update src/controllers/user/authenticate.ts
$ cd nodets-mongojwt-chai/src/controllers/user/   #Open authenticate.ts

#Change Line 9
var redisDB = redis.createClient(6379, "redis")

# to 
var redisDB = redis.createClient(6379, "localhost")
  • Update src/controllers/user/refreshToken.ts
$ cd nodets-mongojwt-chai/src/controllers/user/   #Open refreshToken.ts

#Change Line 7
var redisDB = redis.createClient(6379, "redis")

# to 
var redisDB = redis.createClient(6379, "localhost")

INSTALL PACKAGE DEPENDENCIES

$ cd nodets-mongojwt-chai
$ yarn install
# or with npm

$ npm install

RUN TESTS

$ cd nodets-mongojwt-chai
$ yarn run watch-test-ts

RUN APP in DEV Mode

$ cd nodets-mongojwt-chai
$ yarn run watch-ts
$ yarn run watch-js

RUN IN PROD Mode

$ cd nodets-mongojwt-chai
$ yarn start

About

Express TypeScript API Authentication with MongoDB, JWT & Unit Testing with Mocha / Chai

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published