Reputation: 1856
Given the following Sails.js controller, how can I call its processLogin
method from its processRegistration
method?
Currently this results in a ReferenceError: AuthController is not defined
on line 56 (AuthController.processLogin(req, res);
).
If I change it to this.processLogin(req, res);
I get a TypeError: Object #<Object> has no method 'processLogin'
.
Similarly if I change it to just processLogin(req, res);
I get ReferenceError: processLogin is not defined
.
/**
* AuthController.js
*
* @description ::
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
var passport = require('passport');
module.exports = {
login: function (req, res) {
res.view('auth/login');
},
processLogin: function (req, res) {
passport.authenticate('local', function (err, user, info) {
if ((err) || (!user)) {
res.redirect('/login');
return;
}
req.logIn(user, function (err) {
if (err) res.redirect('/login');
console.log(user);
console.log('user ' + user.username + ' logged in');
return res.redirect('/user');
});
})(req, res);
},
logout: function (req, res) {
var username = req.user.username;
req.logout();
console.log('user ' + username + ' logged out');
res.redirect('/login');
},
register: function(req,res) {
res.view('auth/register');
},
processRegistration: function(req, res) {
var userObj = {
username: req.param('username'),
password: req.param('password')
};
User.create(userObj, function(err, user) {
if (err) {
console.log(err);
return res.redirect('/register');
}
console.log('user ' + user.username + ' registered');
// Log user in
AuthController.processLogin(req, res); // ReferenceError here
});
},
_config: {}
};
I am using Sails.js 0.10.0-rc5
.
Upvotes: 3
Views: 7862
Reputation: 8182
I know this is old, but sails.controllers.
exposes the controllers
Upvotes: 1
Reputation: 1856
Refactoring slightly fixed it:
/**
* AuthController.js
*
* @description ::
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
var passport = require('passport');
var AuthController = {
login: function(req, res) {
res.view('auth/login');
},
processLogin: function(req, res) {
passport.authenticate('local', function(err, user, info) {
if ((err) || (!user)) {
res.redirect('/login');
return;
}
req.logIn(user, function(err) {
if (err) res.redirect('/login');
console.log('user ' + user.username + ' logged in');
return res.redirect('/user');
});
})(req, res);
},
logout: function(req, res) {
var username = req.user.username;
req.logout();
console.log('user ' + username + ' logged out');
res.redirect('/login');
},
register: function(req, res) {
res.view('auth/register');
},
processRegistration: function(req, res) {
var userObj = {
username: req.param('username'),
password: req.param('password')
};
User.create(userObj, function(err, user) {
if (err) {
console.log(err);
return res.redirect('/register');
}
console.log('user ' + user.username + ' registered');
// Log user in
AuthController.processLogin(req, res);
});
}
};
module.exports = AuthController;
I had tried this previously but just didn't get the module.exports
line right (did something like module.exports = { AuthController: AuthController };
, which didn't work either).
Upvotes: 2