Skip to content
Browse files

Allow findUserById to get access to the request if needed

  • Loading branch information...
1 parent 6b404d0 commit a8c4afc40c89169c1cef04de18e3260e6e82a59e @evdb evdb committed
Showing with 22 additions and 3 deletions.
  1. +11 −0 README.md
  2. +10 −2 index.js
  3. +1 −1 lib/modules/everymodule.js
View
11 README.md
@@ -158,6 +158,17 @@ everyauth.everymodule.findUserById( function (userId, callback) {
});
```
+If you need access to the request object the function can have three arguments:
+
+```javascript
+everyauth.everymodule.findUserById( function (req, userId, callback) {
+
+ // use the request in some way ...
+
+ // callback has the signature, function (err, user) {...}
+});
+```
+
Once you have configured this method, you now have access to the user object
that was fetched anywhere in your server app code as `req.user`. For instance:
View
12 index.js
@@ -80,7 +80,8 @@ everyauth.middleware = function () {
if (!auth || !auth.userId) return next();
var everymodule = everyauth.everymodule;
var pause = __pause(req);
- everymodule.findUserById()(auth.userId, function (err, user) {
+
+ var findUserById_callback = function (err, user) {
if (err) {
pause.resume();
return next(err);
@@ -89,7 +90,14 @@ everyauth.middleware = function () {
else delete sess.auth;
next();
pause.resume();
- });
+ };
+
+ var findUserById_function = everymodule.findUserById();
+
+ findUserById_function.length === 3
+ ? findUserById_function( req, auth.userId, findUserById_callback )
+ : findUserById_function( auth.userId, findUserById_callback );
+
}
, connect.router(function (app) {
var modules = everyauth.enabled
View
2 lib/modules/everymodule.js
@@ -319,7 +319,7 @@ everyModule
, moduleErrback: 'THE error callback that is invoked any time an error occurs in the module; ' +
'defaults to `throw` wrapper'
, logoutRedirectPath: 'Where to redirect the app upon logging out'
- , findUserById: 'function for fetching a user by his/her id -- used to assign to `req.user` - function (userId, callback) where function callback (err, user)'
+ , findUserById: 'function for fetching a user by his/her id -- used to assign to `req.user` - function ( [req], userId, callback) where function callback (err, user)'
, performRedirect: 'function for redirecting responses'
, userPkey: 'identifying property of the user; defaults to "id"'
})

0 comments on commit a8c4afc

Please sign in to comment.
Something went wrong with that request. Please try again.