Skip to content

Commit

Permalink
Pass arbitrary info from Strategy#validate to done callback
Browse files Browse the repository at this point in the history
Some passport strategies require to pass arbitrary arguments to done callback. For example https://github.com/jaredhanson/passport-totp#configure-strategy requires to pass key and period. Or it can be info data for req.authInfo (related to nestjs#24).
Now one can return array from Strategy#validate method, like
```ts
async validate(payload: JwtPayload) {
    const user = await this.authService.validateUser(payload);
    if (!user) {
      throw new UnauthorizedException();
    }
    return [user, 'some info'];
  }
```
  • Loading branch information
deathman92 committed Feb 1, 2019
1 parent 6d9c71f commit 49100de
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/passport/passport.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export function PassportStrategy<T extends Type<any> = any>(
const callback = async (...params: any[]) => {
const done = params[params.length - 1];
try {
done(null, await this.validate(...params));
const validateResult = await this.validate(...params));
if (Array.isArray(validateResult)) {
done(null, ...validateResult);
} else {
done(null, validateResult);
}
} catch (err) {
done(err, null);
}
Expand Down

0 comments on commit 49100de

Please sign in to comment.