npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

NPM

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");
        }

License

MIT