How to serialize/deserialize multiple local strategie - node.js

How can I serialize/deserialize multiple local strategies:
I have already tried this Use multiple local strategies in PassportJS but not success.
This is my code :
passport.use('user', new LocalStrategy( function(username, password, done) {
UserSchema.findOne({ username: username }, function(err, user) {
// ...
return done(null, user);
});
}));
passport.use('admin', new LocalStrategy( function(username, password, done) {
adminSchema.findOne({ username: username }, function(err, user) {
// ...
return done(null, user);
});
}));
passport.serializeUser(function(user, done){
done(null, user.id);
});
passport.deserializeUser(function(id, done){
adminSchema.findById(id, function(err, user){
if(err) done(err);
if(user){
done(null, user);
} else {
user.findById(id, function(err, user){
if(err) done(err);
done(null, user);
})
}
});

I was working on the same problem. I took your code and changed a few things and it worked for me! Make sure to log out of one account before trying another or just use an incognito tab.
Here is the updated code I used:
passport.use('user', new LocalStrategy( function(username, password, done) {
UserSchema.findOne({ username: username }, function(err, user) {
// ...
return done(null, user);
});
}));
passport.use('admin', new LocalStrategy( function(username, password, done) {
adminSchema.findOne({ username: username }, function(err, user) {
// ...
return done(null, user);
});
}));
passport.serializeUser(function(user, done){
done(null, user.id);
});
passport.deserializeUser(function(id, done){
adminSchema.findById(id, function(err, user){
if(err) done(err);
if(user){
done(null, user);
} else {
UserSchema.findById(id, function(err, user){
if(err) done(err);
done(null, user);
})
}
});
My schema names are different so I changed them to match yours. One issue you might have is using UserSchema instead of userSchema. Try that next if it doesn't work.
Hope this helps!

Related

How to use passport.js for different kinds user authentication?

I'm trying to have 2 different users by having 2 collections in my MongoDB database. So, I wants to have
separate authentication. I had done this.
passport.use('patient', new LocalStrategy({usernameField:'email'},
function(username, password, done){
patient.findOne({email: username}).then(user => {
if(!user){
console.log("no patient exists");
return done(null, false);
}
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) throw err;
if (isMatch) {
return done(null, user);
} else {
console.log("incorrect patient password");
return done(null, false);
}
});
})
}));
And I had serialized and deserialized my passport.
passport.serializeUser(function(user, done) {
console.log(user._id );
console.log(user.speciality);
done(null, {id: user._id, isPatient: user.speciality===undefined});});
passport.deserializeUser(function(user, done) {
// User.findById(id, function(err, user) {
// done(err, user);
// });
if(user.isPatient){
patient.findById(user.id, function(err, user){
done(err, user);
});
}else{
doctor.findById(user.id, function(err, user){
done(err, user);
})
}});
But when I'm trying to login my website is only loading but ain't showing any error or redirecting.
Please help me.

How to disable serializeuser and deserializeuser for specific strategy

I have code like this:
passport.serializeUser(function (user, done) {
console.log('serializing user ' +user._id);
return done(null, user._id);
});
passport.deserializeUser(function (id, done) {
console.log(id);
Users.findById(id, function (err, user) {
console.log('deserialize user ' +id);
done(err, user);
});
});
passport.use('login', new LocalStrategy({
passReqToCallback: true
},
function (req, username, password, done) {
Users.findOne({username: username}, function (err, user) {
if(err){
return done(err);
}
if(!user){
console.log('User Not Found with username '+username);
return done(null, false);
}
if(!isValidPassword(user, password)){
console.log('Invalid Password');
return done(null, false); // redirect back to login page
}
return done(null, user);
}
);
})
);
passport.use('signup', new LocalStrategy({
passReqToCallback: true
},
function (req, username, password, done) {
Users.findOne({username: username}, function (err, user) {
if(err){
return done(err);
}
if(user){
console.log('User already exists with username: '+username);
return done('User already exists', false);
}
else{
var newUser = new Users();
newUser.username = username;
newUser.password = createHash(password);
newUser.save(function (err) {
if(err){
console.log('Error in Saving user: '+err);
throw err;
}
console.log(newUser.username +' Registered Successfully');
return done(null, newUser);
});
}
});
})
);
var isValidPassword = function (user, password){
return bCrypt.compareSync(password, user.password);
};
var createHash = function (password){
return bCrypt.hashSync(password,bCrypt.genSaltSync(10), null);
};
The problem is, when client sign-up new user and registered successfully. client will be automatically authenticated.
How to make client is still not authenticated until he/she is log-in??
So the flow I want is:
1. client register new ID
2. Client still not authenticated until he/she is login
3. client login
4. Client is authenticated now

Passport.js positive result

I am attempting to authenticate a user in a Node app using Sequelize and Passport. I am able to hit my database but can't seem to get a positive result. Basically, I have a simple frontend form that accepts a username and password (using the respective names "username" and "password") and then the following Passport definition:
passport.use(new localStrategy(
{usernameField: 'email'},
function(req, email, password, done) {
models.TeacherX.findOne({ email: email }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));
Then I call it with:
router.post('/login',
passport.authenticate('local', { failureRedirect: '/login?msg=failure'}),
function(req, res) {
res.redirect("/?msg=positive");
});
Since you are passing the request to the verify callback, you need to set the property passReqToCallback to true or remove the req param from the verify callback. Try this:
passport.use(new localStrategy(
{passReqToCallback: true ,
usernameField: 'email'},
function(req, email, password, done) {
models.TeacherX.findOne({ email: email }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));

I was able to authenticate an existing user but I couldn't create new user account? Here is my Code

//*********************This was to authenticate user*********************
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use(new LocalStrategy(function(username, password, done) {
process.nextTick(function() {
UserDetails.findOne({
'username': username,
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
if (user.password != password) {
return done(null, false);
}
return done(null, user);
});
});
}));
//*********************This was to create new user*********************
passport.use(new LocalStrategy(function(username, password, done) {
process.nextTick(function() {
UserDetails.findOne({
'username': username,
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
var newUser = new UserDetails({"username":newUsername, "password":newPassword});
newUser.save(function (err, newUser) {
if (err)
{
return console.error(err);
}
console.log("Document Saved!");
});
return done(null, true);
}
});
});
}));
I have a login.jade file and signup.jade. So, when I want to sign in a user - if the user is in the database, it will authenticate the user successfully. But if the user does not exist it will insert a new user the database.
So my question is what should I change in code so that the a new user would be able to create account using the signup page and and an existing user would be able use login?
Thanks

Nodejs - passport oauth2 authentification for express app with redis DB

I'm having trouble creating the Redis DB model for Passport Amazon oAuth2. I am using "redis-connect" module and I have got a running Redis local server. my redis server successsfully stores session data so the module and the server is running well. I only need help with the User variable.
Here is my code:
//create a user model
var User = new RedisStore({ host:'127.0.0.1', port:6379, prefix:'token' });
passport.serializeUser(function(user, done) {
console.log('serializeUser: ' + user._id);
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user){
console.log(user);
if(!err){ done(null, user);}
else {done(err, null);}
});
});
passport.use(new AmazonStrategy({
clientID: "foo",
clientSecret: "SECRET",
callbackURL: "https://localhost/auth/amazon/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOne({ oauthID: profile.id }, function(err, user) {
if(err) { console.log(err); }
if (!err && user !== null) {
done(null, user);
} else {
var user = new User({
oauthID: profile.id,
name: profile.displayName,
created: Date.now()
});
user.save(function(err) {
if(err) {
console.log(err);
} else {
console.log("saving user ...");
done(null, user);
}
});
}
});
}
));
Also the findById function is not defined. I have no idea how... I believed it was part of the redis-connect module. Can anyone help me out here?

Resources