Last active
May 30, 2018 01:13
-
-
Save sean3z/bcd43ae88385353f4309fed25f6aca49 to your computer and use it in GitHub Desktop.
Restify JWT Sign example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const config = require('./config'); | |
const rjwt = require('restify-jwt-community'); | |
const jwt = require('jsonwebtoken'); | |
// using restify-jwt to lock down everything except /auth | |
app.use(rjwt(config.jwt).unless({ | |
path: ['/auth'] | |
})); | |
// using the `req.user` object provided by restify-jwt | |
app.get('/user', (req, res, next) => { | |
res.send(req.user); | |
}); | |
app.post('/auth', (req, res, next) => { | |
let { username, password } = req.body; | |
user.authenticate(username, password).then(data => { | |
// creating jsonwebtoken using the secret from config.json | |
let token = jwt.sign(data, config.jwt.secret, { | |
expiresIn: '15m' // token expires in 15 minutes | |
}); | |
// retrieve issue and expiration times | |
let { iat, exp } = jwt.decode(token); | |
res.send({ iat, exp, token }); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment