#How do I get authInfo after authenticating with Google strategy passport?

5 messages · Page 1 of 1 (latest)

dry beacon
#

I have implemented the Google OAuth passport strategy and it works, but I'm passing some additional data to the callback endpoint, so I can redirect the user to a different URL if it's a new user or an existing user.

This is my Google strategy:

Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy) {
  constructor(
    ...
  ) {
    ...
  }

  async validate(
    ...
  ): Promise<void> {
    ...

    const authInfo = { newUser: !userExists };
    done(null, user, authInfo);
  }
}

And this works, after that, my callback endpoint is called and I can get the user from the request. The issue is that I need the authInfo in the callback, so I can redirect the user based on if it's a new user or existing user. The problem is that I can see the authInfo in the request, when I use the handleRequest in my AuthGuard, but not inside the controller.

This is my AuthGuard:

@Injectable()
export class GoogleAuthGuard extends AuthGuard('google') {
  async canActivate(context: ExecutionContext): Promise<boolean> {
    const result = (await super.canActivate(context)) as boolean;
    const request = context.switchToHttp().getRequest();
    await super.logIn(request);
    return result;
  }

  handleRequest(
    ...args: Parameters<
      InstanceType<ReturnType<typeof AuthGuard>>['handleRequest']
    >
  ) {
    console.log(args);
    return super.handleRequest(...args);
  }
}

The log above is logging the authInfo and looking at @nestjs/passport source code, I can see that the authInfo is being set in the request.

But when I try to get the authInfo in my callback function, it's always undefined:

@UseGuards(GoogleAuthGuard)
@Get('/google/callback')
async googleCallback(
  @Request()
  request: ExpressRequest,
): Promise<{ msg: string }> {
  console.log(request.authInfo); // This is always undefined
  return { msg: 'OK' };
}

How do I get the authInfo in my callback?

silk spindle
#

hi

sturdy prism
dry beacon
sturdy prism