如何解决护照登录未调用策略文件中的函数
我正在尝试将护照用于 SSO 。我的问题是,当我使用任何选项登录时,一切正常,除了data saving
...我认为functions
文件中的strategy
没有被调用(日志是都不起作用)。
例如Google strategy
:
@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy,'google') {
constructor(private userService: UserService) {
super({
clientID: GOOGLE_CLIENT_ID,clientSecret: GOOGLE_CLIENT_SECRET,callbackURL: 'http://localhost:4200',scope: ['email','profile'],});
}
async validate(
accessToken: string,refreshToken: string,profile: any,done: VerifyCallback,): Promise<any> {
try {
console.log(profile);
const user = profile;
this.userService.FindOrCreate(profile);
done(null,user);
} catch (err) {
done(err,null);
}
}
}
控制器:
@Get('google')
@UseGuards(AuthGuard('google'))
async twitterauth(@Req() req) {
return await this.authService.login(req.user);
}
AuthService:
@Injectable()
export class AuthService {
private readonly logger = new Logger(AuthService.name);
constructor(
private userService: UserService,private readonly jwtService: JwtService,) {}
async validateUser(email: string,password: string): Promise<User> {
const user: User = await this.userService.findOne({
where: { email },});
if (!user) {
return null;
} else {
if (await bcrypt.compare(password,user.password)) {
return user;
} else {
this.logger.error('Password is incorrect.');
return null;
}
}
}
async login(user: any) {
const payload = { email: user.email,role: user.role };
return {
// eslint-disable-next-line @typescript-eslint/camelcase
access_token: this.jwtService.sign(payload),};
}
}
其他策略(fb,linkedin,instagram,github)完全相同,问题也相同。
解决方法
在聊天中发现的问题是Google在OAuth流中调用的回调不属于同一服务器,因此NestJS服务器无法对传入的数据做出反应,因此为什么从未调用过validate
。
该回调路由需要指向您的NestJS服务器,以便它可以处理数据库的保存逻辑,或有角度的应用程序需要将返回的路由重新路由回NestJS服务器。无论哪种方式,都不会调用您的验证,因为您的Nest服务器永远不会从Google获得带有所有敏感信息的回调
使回调指向您的服务器,以使数据按照预期的Passport格式进行格式化,这很有可能。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。