为什么Facebook注册不将数据保存到数据库?

问题描述

我正在尝试使用 nestJS 中的 Passport 创建 Facebook SSO 。当我转到特定的url时,Facebook登录有效,并且将我重定向到要重定向到的页面。我唯一的问题是应用程序没有保存data中的User。我尝试执行log步骤,但是log都不起作用。可能代码的全部部分都没有运行,我也不知道为什么...

Facebook策略:

const FacebookTokenStrategy = strategy.Strategy;

@Injectable()
export class FacebookStrategy {
  constructor(
    private authService: AuthService,private userService: UserService,) {
    console.log('constructor');
    this.init();
  }

  private init(): void {
    use(
      'facebook',new FacebookTokenStrategy(
        {
          clientID: FACEBOOK_CLIENT_ID,clientSecret: FACEBOOK_CLIENT_SECRET,callbackURL: 'http://localhost:4200',profileFields: ['id','name','displayName','emails','photos'],},async (
          accesstoken: string,refreshToken: string,profile: any,done: any,) => {
          try {                                       //This is the part which is not working
            console.log(profile);                     //I get nothing
            const user = profile;                     //
            this.userService.FindOrCreate(profile);   //
            done(null,user);                         //
          } catch (err) {                             //
            done(err,null);                          //end
          }
        },),);
  }
}

控制器:

@Get('facebook')
  @UseGuards(AuthGuard('facebook'))
  async facebookAuth(@Req() req) {
    console.log(req)                                //No response
    return await this.authService.login(req.user);
  }

验证服务:

@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),};
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)