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

x-auth-service

v1.0.2

Published

Simple Auth library for REST APIs using expressJS

Downloads

11

Readme

Introduction

This library was created in order to have a simple and reusable authentication service while building REST APIs.

Install & Setup

The project uses 17 different dependencies. It also uses the redis 3.1.2 ver which doesn't support async/await. The windows version can be downloaded from here.

To use the library add it to your project:

const xauth = require('x-auth-service')

import xauth from 'x-auth-service'

Auth Methods

login

The login method is used to check if the user's credentials are found inside our database. If the user is found, then we create JWT access and refresh Tokens that are valid for this user as a response.

   const result = await xauth.authService.login(req.body);

signup

The signup method is used to add the credentials of a new user into our backend system. The function would then return the JWT tokens as a response, and an email in order for the user to verify their account.

const result = await xauth.authService.signup(req.body,req.headers);

logout

The logout method makes sure to delete and blacklist the refresh from the server side which will in turn make sure that the user is logged out of the app. The tokens must also be deleted from the client side.

await xauth.authService.logout(req.body);

refreshToken

This method renews access to the user to the system by checking the validity of the token and sending back two new pairs of JWT.

const result = await xauth.authService.refreshToken(req.body.refreshToken);

forgotPassword

This function sends an email with a url that contains a JWT token that is valid only for 10min in order to reset their credentials.

 const result: any = await xauth.authService.forgotPassword(req.headers);

resetPassword

Checks the old password and updates the database with the new inserted one.

const result = await xauth.authService.resetPassword(req.params, req.body, req.headers.authorization);

verifyEmail

Verifies the that the token in the url matches with the userId.

 await xauth.authService.verifyEmail(req.headers.authorization);

hashPassword

Helper function that encrypts the user password using the bcryptjs library

 const hashedPassword = await hashPassword(req.body.password);

isValidPassword

A helper function that returns true if the decrypted hash and the plain text password match

const isMatch = await isValidPassword(req.body.password, hashedPassword);

JWT Methods

setAccessToken

Generates a temporary access Token that will be used to give access to users

const accessToken = xauth.setTokens.setAccessToken(userId);

setRefreshToken

Generates a refresh Token that will be used to send back 2 pairs of JWT tokens

const refreshToken = await xauth.setTokens.setRefreshToken(userId);

setResetPasswordToken

Generates a token valid for 10min that will be used to verify that the url belongs to the user

 const resetPasswordToken = await xauth.setTokens.setResetPasswordToken(userId);

verifyAccessToken

Verifies that the token is valid. Usually used as middleware for router protection.

 router.get('/', xauth.verifyTokens.verifyAccessToken , function)

verifyRefreshToken

Verifies that the token is valid

const isValid: any = await xauth.verifyTokens.verifyRefreshToken(token);

verifyResetPasswordToken

Used inside the resetPassword function ro return userId and make sure it is valid.

const isValid: any = await xauth.verifyTokens.verifyResetPasswordToken(token);