Skip to content

Instantly share code, notes, and snippets.

@alexanmtz
Created January 13, 2019 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanmtz/0dadd18f55778a7d7c17455078d8d870 to your computer and use it in GitHub Desktop.
Save alexanmtz/0dadd18f55778a7d7c17455078d8d870 to your computer and use it in GitHub Desktop.
/**
* Authentication helpers to determine if a user is logged in or not
* before a route returns information to the response
*/
const userExist = require('../modules/users').userExists
const jwt = require('jsonwebtoken')
function isAuthOrRedirect (req, res, next) {
if (req.isAuthenticated()) return next()
res.redirect('/')
}
function isNotAuthOrRedirect (req, res, next) {
if (!req.isAuthenticated()) return next()
res.redirect('/')
}
function isAuth (req, res, next) {
// if (req.isAuthenticated()) return res.send({ 'authenticated': true });
const token = req.headers.authorization.split(' ')[1]
if (token) {
return jwt.verify(token, process.env.SECRET_PHRASE, (err, decoded) => {
// the 401 code is for unauthorized status
if (err) {
return res.status(401).end()
}
const userData = decoded
// check if a user exists
return userExist(userData).then(user => {
return res.send({ authenticated: true, user: user })
}).catch(e => {
// eslint-disable-next-line no-console
console.log('error to sign user')
return res.status(401).end()
})
})
}
return next()
}
function isNotAuth (req, res, next) {
if (!req.isAuthenticated()) return next()
res.send({ 'authenticated': true })
}
module.exports = {
isAuthOrRedirect: isAuthOrRedirect,
isNotAuthOrRedirect: isNotAuthOrRedirect,
isAuth: isAuth,
isNotAuth: isNotAuth
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment