-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(auth): use requires and exports style
- Loading branch information
Showing
3 changed files
with
52 additions
and
53 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
'use strict'; | ||
|
||
var config = require('../config/environment'); | ||
var jwt = require('jsonwebtoken'); | ||
var expressJwt = require('express-jwt'); | ||
var compose = require('composable-middleware'); | ||
|
||
var config = require('../config/environment'); | ||
var User = require('../api/user/user.model'); | ||
var validateJwt = expressJwt({ secret: config.secrets.session }); | ||
|
||
module.exports = { | ||
|
||
/** | ||
* Attach the user object to the request if authenticated | ||
* Otherwise returns 403 | ||
*/ | ||
isAuthenticated: function () { | ||
return compose() | ||
.use(function (req, res, next) { | ||
validateJwt(req, res, next); | ||
}) | ||
.use(function (req, res, next) { | ||
User.findById(req.user._id, function (err, user) { | ||
if (err) { return next(err); } | ||
if (!user) { return res.send(401); } | ||
req.user = user; | ||
next(); | ||
}); | ||
/** | ||
* Attach the user object to the request if authenticated | ||
* Otherwise returns 403 | ||
*/ | ||
exports.isAuthenticated = function () { | ||
|
||
return compose() | ||
.use(function (req, res, next) { | ||
validateJwt(req, res, next); | ||
}) | ||
.use(function (req, res, next) { | ||
User.findById(req.user._id, function (err, user) { | ||
if (err) { return next(err); } | ||
if (!user) { return res.send(401); } | ||
req.user = user; | ||
next(); | ||
}); | ||
}, | ||
|
||
/** | ||
* Returns a jwt token, signed by the app secret | ||
*/ | ||
signToken: function (id) { | ||
return jwt.sign( | ||
{ _id: id }, | ||
config.secrets.session, | ||
{ expiresInMinutes: 60 * 5 } | ||
); | ||
} | ||
}); | ||
|
||
}; | ||
|
||
/** | ||
* Returns a jwt token, signed by the app secret | ||
*/ | ||
exports.signToken = function (id) { | ||
|
||
return jwt.sign( | ||
{ _id: id }, | ||
config.secrets.session, | ||
{ expiresInMinutes: 60 * 5 } | ||
); | ||
|
||
}; |
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
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