Comments for GeekTantra https://www.geektantra.com/ yet another geek's blog Mon, 18 Nov 2019 08:09:31 +0000 hourly 1 https://wordpress.org/?v=6.5.3 Comment on [How To] Implement Passport.js Authentication with Sails.js by Artem Pylypchuk https://www.geektantra.com/2013/08/implement-passport-js-authentication-with-sails-js/#comment-100507 Mon, 22 Dec 2014 13:03:49 +0000 http://www.geektantra.com/?p=366#comment-100507 In reply to Craig Roy.

The same applies to line #6, without this change session won’t work (silently).

]]>
Comment on [How To] Implement Passport.js Authentication with Sails.js by Artem Pylypchuk https://www.geektantra.com/2013/08/implement-passport-js-authentication-with-sails-js/#comment-100506 Mon, 22 Dec 2014 13:01:49 +0000 http://www.geektantra.com/?p=366#comment-100506 Maybe this was a good example some time ago, but now it isn’t. It is extremely error-prone.
I used the working one from https://github.com/kasperisager/sails-generate-auth (thanks @Edygar), but made it simpler (I don’t need Facebook adapters or login with both username and e-mail).

Literally almost any piece of code I reused from this example had errors in it.

If you want to figure it out on your own, with invaluable educational experience, don’t read my comment any further, and try making it to work! 😉

First of all, in findById & findByUsername, you can’t do User.findOne(query).done() anymore, you have to do User.findOne(id, function (err, user) { … }); . In the case of findByUsername it trows an error, but findById fails silently (and you spend hours figuring out why session is not established after successful login).

Next,
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(user.password, salt, function(err, hash) {…} });
doesn’t work anymore (undocumented change in api?), one has to do
bcrypt.hash(user.password, 10, function (err, hash) {…});

Then, to set up the local strategy one has to declare something like this in services/passport.js and add it to config/bootstrap.js (a “passport.loadStrategies();” line):

passport.loadStrategies = function () {
this.use(new LocalStrategy({ passReqToCallback: true, usernameField: ’email’ },
function (req, username, password, done) {

Note: passReqToCallback is useful when you want to localize messages returned, e.g. with i18n
return done(null, false, {message: sails.__.apply(req, [‘Error.Passport.Password.Wrong’])});
otherwise, there will be no req object inside.

Custom middleware is best inserted through an “authenticated” policy, which has to set up session variables (req.isAuthenticated() doesn’t work), see how it’s done e.g. like in https://github.com/kasperisager/sails-generate-auth, but the example there with “sessionAuth” policy is incomplete and “req.session.authenticated” variable doesn’t get set, so one has to add some extra code into api/policies/passport.js.
Inserting middleware Express-style through config/passport.js is obsolete (one has to use “http” variable, not “express”) and seems to break interoperability of other middleware inside Sails.
It is possible to set custom middleware config through config/http.js, but I don’t think it is good to do it this way.

And so on, because I think I might have omitted something.
If you don’t want any headache just use https://github.com/kasperisager/sails-generate-auth (don’t forget to set req.session.authenticated somewhere in api/policies/passport.js). This might not be the best code, this is how I do it:

file api/policies/passport.js:
module.exports = function (req, res, next) {
// Initialize Passport
passport.initialize()(req, res, function () {
// Use the built-in sessions
passport.session()(req, res, function () {
//passport.authenticate(‘session’)(req, res, function () {
// Make the user available throughout the frontend
console.log(“passport.session called, user is “, req.user);
res.locals.user = req.user;
if (typeof req.user != ‘undefined’) req.session.authenticated = true;
else req.session.authenticated = false;
next();
});
});
};

]]>
Comment on [How To] Implement Passport.js Authentication with Sails.js by jiarong https://www.geektantra.com/2013/08/implement-passport-js-authentication-with-sails-js/#comment-86681 Thu, 06 Nov 2014 02:41:42 +0000 http://www.geektantra.com/?p=366#comment-86681 This is a good example. (: helped me out real quick.

]]>
Comment on [How To] Implement Passport.js Authentication with Sails.js by Edygar de Lima https://www.geektantra.com/2013/08/implement-passport-js-authentication-with-sails-js/#comment-86357 Wed, 05 Nov 2014 11:19:13 +0000 http://www.geektantra.com/?p=366#comment-86357 Quick tip: https://github.com/kasperisager/sails-generate-auth

]]>
Comment on Update for jQuery Live Form Validation Plugin by Tom https://www.geektantra.com/2009/10/update-for-jquery-live-form-validation-plugin/#comment-85490 Mon, 03 Nov 2014 17:26:43 +0000 http://www.geektantra.com/?p=143#comment-85490 jQuery(“#test_input”).validate({
expression: “if (isChecked(‘update’) && VAL != ‘0’) return true; else return false;”,
message: “Required Field”
});

is this possible only have the validation if a checkbox in the form is ticked ?

]]>
Comment on jQuery Live Form Validation by 80+ Best JQUERY Plugins (Dropdowns, Slideshows and Effects) — Ghank https://www.geektantra.com/2009/09/jquery-live-form-validation/#comment-80020 Thu, 23 Oct 2014 19:08:36 +0000 http://www.geektantra.com/?p=99#comment-80020 […] iv) jQUERY LIVE FORM VALIDATION: […]

]]>
Comment on jQuery Live Form Validation by Babakumar https://www.geektantra.com/2009/09/jquery-live-form-validation/#comment-71515 Thu, 09 Oct 2014 09:42:36 +0000 http://www.geektantra.com/?p=99#comment-71515 Hi Geektantra,
I need jquery expression for validating decimal number.
The number should allow one or two digits before decimal point and should allow only one digit after decimal point.
Ex:1.2 or 11.2 or 11 or 1 or 0.2 or .2 these formats should allow
Wrong formats :111 or 1.222 or 1.22 or o.22 these formats should not allow
Thanks and Regards,
Babakumar

]]>
Comment on jQuery Live Form Validation by Wilest https://www.geektantra.com/2009/09/jquery-live-form-validation/#comment-67790 Fri, 03 Oct 2014 09:57:28 +0000 http://www.geektantra.com/?p=99#comment-67790 Hi
I’m trying to change the position of the message, how should I go about it?

]]>
Comment on Central Authentication System: CAS by Manish Mishra https://www.geektantra.com/2014/09/central-authentication-systemscas/#comment-63183 Tue, 23 Sep 2014 08:56:03 +0000 http://www.geektantra.com/?p=577#comment-63183 Can we integrate the CAS with an Authorization system?

]]>
Comment on jQuery Live Form Validation by 13 Best jQuery Form Validation Plugins with Code for Developers | Templates Perfect https://www.geektantra.com/2009/09/jquery-live-form-validation/#comment-51395 Mon, 25 Aug 2014 18:01:23 +0000 http://www.geektantra.com/?p=99#comment-51395 […] 11) jQuery form validation –  jquery live form validation […]

]]>