kiran Gopal
kiran Gopal

Reputation: 450

How to implement LocalStrategy and a CustomStrategy together?

I am creating an application for an online quiz. The tech stack for server side is node, express, passport, mongo, and mongoose. The client side is Angular.

In this application, I need to create two kinds of authentication and session. For admin, I need to implement LocalStrategy (username, password) with fixed session time. And for the candidate, a CustomStrategy (emailID) with individual sessions and expiry time.

How can I implement this?

Upvotes: 0

Views: 103

Answers (1)

kiran Gopal
kiran Gopal

Reputation: 450

I have used a CustomStrategy and LocalStrategy together.

passport.use(new LocalStrategy({
    usernameField: 'email'
}, function(email, password, done) {
    User.findOne({
        email: email.toLowerCase()
    }, function(err, user) {
        if (!user) {
            return done(null, false, {
                msg: 'Email ' + email + ' not found.'
            });
        }
        user.comparePassword(password, function(err, isMatch) {
            if (isMatch) {
                return done(null, user);
            } else {
                return done(null, false, {
                    msg: 'Invalid email or password.'
                });
            }
        });
    });
}));



passport.use(new CustomStrategy(
    function(req, done) {
        Invite.findById(req.params.id, function(err, invite) {
            if (err) {
                console.log(err)
            }
            if (!invite) {
                return done(null, false, {
                    msg: 'Invite not found.'
                });
            }
            done(null, invite);
        });
    }
));

Upvotes: 1

Related Questions