Skip to content

Instantly share code, notes, and snippets.

@sean3z
Last active May 30, 2018 01:13
Show Gist options
  • Save sean3z/bcd43ae88385353f4309fed25f6aca49 to your computer and use it in GitHub Desktop.
Save sean3z/bcd43ae88385353f4309fed25f6aca49 to your computer and use it in GitHub Desktop.
Restify JWT Sign example
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