[컴][웹] expressjs 의 passport middleware 의 login 과정

 passportjs / passport-js / express js / express-js / expressjs moduel / middleware


login with passport

passport.authenticate 동작

아래처럼 passport 에 strategy 를 등록하게 된다. 이때 authenticate 을 확인하는 function 을 인자로 넘겨주게 된다. 

passport.use(new Strategy(
    // the below function is registered as `_verify()` 
    async function (username, password, done) {
        const authUser = await Users.query().findOne({
            login_id: username,
        });

        // if(err) 
        //     return done(err);
        // if(!authUser) 
        //     return done(null, false);
        // if(!verifyPassword(authUser.password, password))
        //     return done(null, false);
        return done(null, authUser);

    }
));

// `(user: any, done) => ` will be called `serializedUser()`
passport.serializeUser((user: any, done) => {
    done(null, user.login_id);
});

그리고 나서 흔히 controller 등에서 passport.authenticate 을 호출해서 login 과정, 즉 authentication 과정을 거치게 된다.

passport.authenticate('local', {
    successRedirect: req.session.goingTo || '/profile',
    failureRedirect: '/login',
    failureFlash: true
})(req, res);
passport.lib.middleware.authenticate
    passport.lib.middleware.authenticate.strategy.authenticate
    passport-local.lib.strategy.authenticate
        `_verify()`
            passport.lib.middleware.authenticate.strategy.success
                passport.lib.http.req.logIn(user. optins. done10)
                    this._passport.instance._sm.logIn(this, user, callback)
                    passport.lib.SessionManager.logIn(this, user, callback10)
                        passport.lib.Authenticator.serializeUser(user, req, callback)
                            `serializedUser()`

                        callback10()
                            done10()
                                passport.transformAuthInfo(info, req, callback)
                                passport.lib.Authenticator.transformAuthInfo(fn, req, done20)
                                    done20()
                                        if url is set
                                            // options.successReturnToOrRedirect, options.successRedirect
                                            // req.session.returnTo
                                            res.redirect(url)
                                        next()

댓글 없음:

댓글 쓰기