1.0.0 • Published 7 years ago
no-pass v1.0.0
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 })
}
})
1.0.0
7 years ago