jwt-mongo-helper
v1.0.5
Published
DAO + BS + DTO modules written to be used as login & register logic using Mongo and Express + typescript
Downloads
10
Readme
JWT helper for REST made with typescript for MongoDB
Install
npm i --save jwt-mongo-helper
Usage
Note: This implementation example uses the jsonwebtoken library to generate the tokens. You could use an alternative if you want to. You should change mySecret key.
This project is made entirely with Typescript. In order to use the connector you should create/implement a register and login service. This example uses Express.
Login service
- Create a Mongo connection object (Db) and a UserDTO.
import {Db} from "mongodb"
import * as jsonwebtoken from "jsonwebtoken"
import {UserDTO, UserBS} from "jwt-mongo-helper"
let connection: Db ...
let userToLogin = new UserDTO();
Assign the values to the object fields from the POST body:
userToLogin._username = req.body.username;
userToLogin._email = req.body.email;
userToLogin._password = req.body.password;
Create and instance of UserBS. If the loginUser method returns a UserDTO object (result different from null) it means that the user exists on the Db (and its credentials had been correctly verified). We can now proceed to create the token using jsonwebtoken library and send it back inside the header field 'token'.
// "users" is the collection name used to store the users, connection is the Db Mongo object.
let userBS = new UserBS("users", connection);
let resultOfLoginUser : UserDTO = await userBS.loginUser(userToLogin);
if (resultOfLoginUser !== null) {
let token = jsonwebtoken.sign({"data": userToLogin._password}, 'mySecret', {
expiresIn: '30s'
}
);
res.header("token", token);
res.status(200).send();
} else {
res.status(401).send("Wrong credentials provided");
}
Register service
- Create a Mongo connection object (Db) and a UserDTO.
import {Db} from "mongodb"
import * as jsonwebtoken from "jsonwebtoken"
import {UserDTO, UserBS} from "jwt-mongo-helper"
let connection: Db ...
let userToRegister = new UserDTO();
Assign the values to the object fields from the POST body:
userToRegister._username = req.body.username;
userToRegister._email = req.body.email;
userToRegister._password = req.body.password;
Create and instance of UserBS. If the registerUser method returns a result different from null (void --> normal path), it means that the user doesn't exist in the collection and the new one will be inserted automatically in the system. We can now proceed to create the token using jsonwebtoken library and send it back inside the header field 'token'.
// "users" is the collection name used to store the users, connection is the Db Mongo object.
let userBS = new UserBS("users", connection);
let resultOfRegisterUser = await userBS.registerNewUser(userToRegister);
if (resultOfRegisterUser !== null) {
let token = jsonwebtoken.sign({"data": userToRegister._password}, 'mySecret', {
expiresIn: '30s' //1h, 2d ...
}
);
res.header(ServicesConstants.TOKEN_HEADER_NAME, token);
res.status(201).send();
} else {
res.status(409).send("User already exists");
}