护照序列化用户:下一个不是功能

时间:2019-02-13 12:13:58

标签: azure passport.js azure-ad-b2c koa koa-passport

我遇到koa-passport的问题。 我正在使用Azure B2C身份验证,但出现以下错误:

  TypeError: next is not a function
      at p.then.cont (/my-custom-path/node_modules/koa-passport/lib/framework/koa.js:150:16)
      at process._tickCallback (internal/process/next_tick.js:68:7)

似乎在done()回调为passport.serializeUser后出现问题了

这是我的passport.ts

import { Repository } from 'typeorm';
import { validate, ValidationError } from 'class-validator';
import { User, getManager } from 'my-project-shared';
import * as passport from 'koa-passport';
import { OIDCStrategy } from 'passport-azure-ad';

...

const azureOIDCOptions = {
  identityMetadata,
  clientID,
  isB2C: true,
  validateIssuer: true,
  loggingLevel: 'info',
  responseType: 'id_token',
  responseMode: 'query',
  allowHttpForRedirectUrl: true, 
  redirectUrl,
  passReqToCallback: false,
}

/**
 * Sign in using Azure B2C for Web
 */
passport.use(new OIDCStrategy(azureOIDCOptions, async (profile, done) => {
  console.log('AZURE B2C')
  // get a user repository to perform operations with user
  const userRepository: Repository<User> = getManager().getRepository(User);
  const userToBeSaved: User = new User();

  userToBeSaved.firstName = profile.name.givenName;
  userToBeSaved.lastName = profile.name.familyName;
  userToBeSaved.email = profile.emails[0];

  // validate user entity
  const errors: ValidationError[] = await validate(userToBeSaved); // errors is an array of validation errors

  if (errors.length === 0) {
    try {
      const user = await userRepository.save(userToBeSaved);
      done(null, user.id);
    } catch (e) {
      done(e)
    }
  }
}));

passport.serializeUser((user, done) => {
  console.log('serialize', user);
  done(null, user)
})

passport.deserializeUser(async (userId, done) => {
  console.log('deserialize');
  const userRepository: Repository<User> = getManager().getRepository(User);
  const user: User = await userRepository.findOne(userId);

  return user;
});

0 个答案:

没有答案