Created
January 11, 2017 08:23
-
-
Save matt212/a9fc715abfc81f125d59f5226c98053b to your computer and use it in GitHub Desktop.
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
function isAuth(req, res, next) { | |
var token = req.body.token || req.param('token') || req.headers['x-access-token']; | |
//check whether request has token ! | |
if (token != undefined) { | |
// decode token | |
if (token) { | |
// verifies secret and checks exp | |
jwt.verify(token, app.get('superSecret'), function(err, decoded) { | |
if (err) { | |
return res.json({ | |
success: false, | |
message: 'Failed to authenticate token.' | |
}); | |
} else { | |
// if everything is good, save to request for use in other routes | |
req.decoded = decoded; | |
next(); | |
} | |
}); | |
} else { | |
// if there is no token | |
// return an error | |
return res.status(403).send({ | |
success: false, | |
message: 'No token provided.' | |
}) | |
} | |
} else { | |
//check whether request is api or normal request ! | |
var string = req.url, | |
substring = "api"; | |
if (string.indexOf(substring) !== -1) { | |
res.status(403).send({ | |
success: false, | |
message: 'No token provided.' | |
}) | |
} else { | |
//check whether request is authenticated or not ! | |
if (req.isAuthenticated()) | |
return next(); | |
/* res.status(401).json({authenticated: false});*/ | |
res.redirect('/login'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage of my above codebase !