@nestjs/passport#PassportSerializer TypeScript Examples

The following examples show how to use @nestjs/passport#PassportSerializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: google-data.serializer.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
@Injectable()
export default class GoogleDataSerializer extends PassportSerializer {
  constructor(private readonly usersService: UsersService) {
    super();
  }

  serializeUser(user: UserDocument, done: CallableFunction) {
    done(null, user);
  }

  async deserializeUser(user: UserDocument, done: CallableFunction) {
    const foundUser = await this.usersService.getVerifiedUserByEmail(user.email);
    if (!foundUser) {
      return done(new UnauthorizedException());
    }

    return done(null, { ...user, role: foundUser.role, _id: foundUser._id });
  }
}
Example #2
Source File: local.serializer.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
@Injectable()
export default class LocalSerializer extends PassportSerializer {
  constructor(private readonly usersService: UsersService) {
    super();
  }

  serializeUser(user: User, done: CallableFunction) {
    done(null, user);
  }

  async deserializeUser(user: UserDocument, done: CallableFunction) {
    const foundUser = await this.usersService.getVerifiedUserById(user._id);

    if (!foundUser) {
      return done(new UnauthorizedException('The user does not exist'));
    }

    return done(null, user);
  }
}
Example #3
Source File: google-data.serializer.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
@Injectable()
export default class GoogleDataSerializer extends PassportSerializer {
  constructor(private readonly usersService: UsersService) {
    super();
  }

  serializeUser(user: UserEntity, done: CallableFunction) {
    done(null, user);
  }

  async deserializeUser(user: UserEntity, done: CallableFunction) {
    const foundUser = await this.usersService.getVerifiedUserByEmail(user.email);

    if (!foundUser) {
      return done(new UnauthorizedException());
    }

    return done(null, { ...user, role: foundUser.role, id: foundUser.id });
  }
}
Example #4
Source File: local.serializer.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
@Injectable()
export default class LocalSerializer extends PassportSerializer {
  constructor(private readonly usersService: UsersService) {
    super();
  }

  public serializeUser(user: UserEntity, done: CallableFunction) {
    done(null, user);
  }

  public async deserializeUser(user: UserEntity, done: CallableFunction) {
    const foundUser: UserEntity | void = await this.usersService.getVerifiedUserById(user.id);

    if (!foundUser) {
      return done(new UnauthorizedException('The user does not exist'));
    }

    return done(null, user);
  }
}
Example #5
Source File: local.serializer.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
@Injectable()
export default class LocalSerializer extends PassportSerializer {
  constructor(private readonly usersService: UsersService) {
    super();
  }

  serializeUser(user: User, done: CallableFunction) {
    done(null, user);
  }

  async deserializeUser(user: UserDocument, done: CallableFunction) {
    const foundUser = await this.usersService.getById(user._id);

    if (!foundUser) {
      return done(new UnauthorizedException('The user does not exist'));
    }

    return done(null, user);
  }
}
Example #6
Source File: local.serializer.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
@Injectable()
export default class LocalSerializer extends PassportSerializer {
  constructor(private readonly usersService: UsersService) {
    super();
  }

  public serializeUser(user: UserEntity, done: CallableFunction) {
    done(null, user);
  }

  public async deserializeUser(user: UserEntity, done: CallableFunction) {
    const foundUser: UserEntity | void = await this.usersService.getById(
      user.id,
    );

    if (!foundUser) {
      return done(new UnauthorizedException('The user does not exist'));
    }

    return done(null, user);
  }
}
Example #7
Source File: session.serializer.ts    From codeclannigeria-backend with MIT License 6 votes vote down vote up
@Injectable()
export class SessionSerializer extends PassportSerializer {
  serializeUser(user: User, done: (err: Error, user: User) => void): any {
    done(null, user);
  }
  deserializeUser(user: User, done: (err: Error, user: User) => void): any {
    done(null, user);
  }
}