1.LocalStrategy 本地策略
This strategy is used to authenticate users based on a username and password.
Anytime a request is made to authorize an application, we must ensure that a user is logged in before asking them to approve the request.
此策略用于根据用户名和密码对使用者进行身份验证。
每当请求授权应用程序时, 我们必须确保用户登录后才要求他们批准请求。
passport.use(new LocalStrategy(
(username, password, done) => {
db.users.findByUsername(username, (error, user) => {
if (error) return done(error);
if (!user) return done(null, false);
if (user.password !== password) return done(null, false);
return done(null, user);
});
}
));
passport.serializeUser((user, done) => done(null, user.id)); 序列化,即输入user,输出user.id
passport.deserializeUser((id, done) => {
db.users.findById(id, (error, user) => done(error, user)); 反序列化,即输入user id,输出us