no-pass

1.0.0 • Public • Published

Lightweight token based authentication using email

Usage

First, install the module:

$ npm install no-pass --save

A typical implementation may look like this:

Setup a mail delivery

import nopass from 'no-pass'

nopass.setMailDelivery: ({ 
  'Mail Address', 
  'Mail Password', 
  'Smpt Server', 
  'Email Host' 
  })

Express hanlder for sending a token

router.post('/sendtoken', async (req, res) => {

    try {
        const tokenToSend = nopass.generateToken()
        const { uid } = req.body

        await nopass.storeToken(tokenToSend, uid, 1000 * 60 * 10)
        await nopass.mailDelivery(tokenToSend, uid)

        res.json({ msg: 'Sent to ' + uid })

    } catch (error) {
        log.error(error)
        res.json({ err: 'Malformed' })
    }
})

Express handler for verifying the token

router.post('/verifyToken', async (req, res) => {

    try {
        const { token, uid } = req.body
        if (!token || !uid) throw new Error("Malformed")

        await nopass.authenticateToken(token, uid)

        res.json({ msg: 'Authenticated ' + uid })

    } catch (error) {
        log.error(error)
        res.json({ error: error })
    }
})

Readme

Keywords

none

Package Sidebar

Install

npm i no-pass

Weekly Downloads

0

Version

1.0.0

License

ISC

Last publish

Collaborators

  • leonardeyer