只想在LocalStrategy的回调中获取http request或request.body,正如您在附件文件中看到的那样,我们这样编码 新的LocalStrategy({ usernameField:“电子邮件”, passwordField:“ pwd” },(用户名:任意,密码:任意,完成:任意)=> { 我们可以获取用户名和密码,但我想获取整个req.body
代码
import UserDetailsRepo from '../../repo/UserDetailsRepo'
import UserDetails from '../../model/UserDetails'
import * as passport from 'passport'
import { Strategy as LocalStrategy } from 'passport-local'
// import JwtConfiguration from './express-jwt-config'
import * as HttpStatus from 'http-status-codes'
class PassportAuth {
public passport: any;
constructor() {
this.passport = passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'pwd'
},(username: any, password: any, done: any) => {
UserDetailsRepo.fetch(username)
.then(function (userDetails: UserDetails) {
if (!userDetails) {
return done(null, false, { errorCode: HttpStatus.UNAUTHORIZED, message: 'Incorrect username.' });
}
if (!userDetails.validatePassword(password)) {
return done(null, false, { errorCode: HttpStatus.UNAUTHORIZED, message: 'Incorrect password.' });
}
return done(null, userDetails);
})
.catch((err: any) => {
return done(err);
})
}))
// passport.use(JwtConfiguration.getStrategy())
passport.serializeUser(function (user, done) {
if(!user) {
done({ errorCode: HttpStatus.UNPROCESSABLE_ENTITY,message:'ser' },user)
} else {
done(null, user);
}
});
passport.deserializeUser(function (user, done) {
console.log("Deseriaize User");
console.log(user);
done(null, user);
});
}
}
export default new PassportAuth().passport;
router.post('/login', passport.authenticate('local'), (req: Request, res: Response, next: NextFunction) => {
passport.authenticate('local', (err: any, user: UserDetails, info: any) => {
if (user) {
let loginUser = user.checkAttributes(req.body.role, req.body.department);
// if (loginUser) {
req.logIn(loginUser, function (err) {
if (err) {
next(err)
}
next()
});
// } else {
// next({ errorCode: HttpStatus.UNPROCESSABLE_ENTITY })
// }
} else {
next(info)
}
})(req, res, next)
}, (req: Request, res: Response) => {
res.send(req.body)
res.end()
});
答案 0 :(得分:0)
如果您看下面的代码
this.passport = passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'pwd',
passReqToCallback:true
},(req:any,username: any, password: any, done: any) => {
UserDetailsRepo.fetch(username)
.then(function (userDetails: UserDetails) {
if (!userDetails) {
return done(null, false, { errorCode: HttpStatus.UNAUTHORIZED, message: 'Incorrect username.' });
}
if (!userDetails.validatePassword(password)) {
return done(null, false, { errorCode: HttpStatus.UNAUTHORIZED, message: 'Incorrect password.' });
}
try {
return done(null, userDetails.getLoginUserDetails(req.body.role,req.body.department));
} catch (e){
return done(null, false, { errorCode: HttpStatus.UNAUTHORIZED, message: e.message } );
}
})
.catch((err: any) => {
return done(err);
})
}))
passReqToCallback :true被添加到LocalStrategy中,当我们将其设置为true时,我们将获得 request 作为LocalStrategy的 callback 中的第一个参数>功能即 (要求:任意,用户名:任意,密码:任意,已完成:任意)
在哪里研究? 如果您在LocalStrategy构造函数的代码中看到了
declare class Strategy extends PassportStrategy {
constructor(
options: IStrategyOptionsWithRequest,
verify: VerifyFunctionWithRequest
);
constructor(options: IStrategyOptions, verify: VerifyFunction);
constructor(verify: VerifyFunction);
name: string;
}
在上面的代码中有两个主要接口 IStrategyOptionsWithRequest , IStrategyOptions
interface IStrategyOptions {
usernameField?: string;
passwordField?: string;
session?: boolean;
passReqToCallback?: false;
}
interface IStrategyOptionsWithRequest {
usernameField?: string;
passwordField?: string;
session?: boolean;
passReqToCallback: true;
}
现在很明显,通过将true或false值传递给 passReqToCallback ,我们将在LocalStrategy的回调中获得 request 对象。
为什么要在回调中将请求作为第一参数? 如果您查看上面的构造函数代码,则有两个函数 VerifyFunctionWithRequest 和 VerifyFunction VerifyFunctionWithRequest 接口中的第一个参数是 req 希望很清楚...
interface VerifyFunctionWithRequest {
(
req: express.Request,
username: string,
password: string,
done: (error: any, user?: any, options?: IVerifyOptions) => void
): void;
}
interface VerifyFunction {
(
username: string,
password: string,
done: (error: any, user?: any, options?: IVerifyOptions) => void
): void;
}