Skip to content

Passport-oauth2 not working on AWS lambda  #9

@JoseDRojasA

Description

@JoseDRojasA

I'm trying to implement OAuth2 with Google in an AWS lambda function.

Expected behavior

I should be able to login into my app with Google using oauth2 with my code deployed in AWS Lambda.

Actual behavior

I have a timeout error in lambda logs.
When I test it locally it works perfectly, but When I deploy it in AWS, It is not working.

I have 2 routes:

/login/google
/login/google/redirect
The first one works, It shows me the page to select my google account, when I select my google account, It redirects me to the second route (/login/google/redirect/) but I received a timeout error.

In GCP, I have the following Authorized domains
image

I also checked Authorized redirect URIs and they look good.
image

Steps to reproduce

// google-strategy-guard.ts

import { Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";

@Injectable()
export class GoogleStrategyGuard extends AuthGuard('google') {
  logIn<TRequest extends { logIn: Function; } = any>(request: TRequest): Promise<void> {
    console.log('EXECUTING logIn')
    return super.logIn(request);
  }

  handleRequest<TUser = any>(err: any, user: any, info: any, context: any, status?: any): TUser {
    console.log('EXECUTING handleRequest')
    console.log('user', user);
    console.log('info', info);
    console.log('context', context);
    console.log('status', status);
    const result = super.handleRequest(err, user, info, context, status);
    console.log('result', result);
    return result;
  }

  canActivate(context) {
    console.log('Context', context);
    return super.canActivate(context);
  }

}

// google-strategy-service.ts

import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Configuration } from '@nexus/configuration';
import { UserDTO } from '@nexus/models';
import Axios from 'axios';
import { Strategy } from '@passport-next/passport-oauth2';
import { GoogleOAuthUser } from './google-oauth-user';

const { authentication: { googleStrategy } } = Configuration();

const authorizationURL = 'https://accounts.google.com/o/oauth2/v2/auth';
const tokenURL =  'https://www.googleapis.com/oauth2/v4/token';
const userProfileURL = 'https://www.googleapis.com/oauth2/v3/userinfo';

@Injectable()
export class GoogleStrategyService extends PassportStrategy(Strategy, 'google') {
  constructor() {
    console.log(googleStrategy)
    super({...googleStrategy, authorizationURL, tokenURL});
  }

  async validate(accessToken: string) {
    const profile: GoogleOAuthUser = await Axios.get(`${userProfileURL}?access_token=${accessToken}`);
    console.log('Executing validate')
    console.log('profile', profile);
    const { name, email, picture } = profile.data;
    const user: Partial<UserDTO> = {
      email: email,
      name,
      picture,
      accessToken,
    }
    console.log('HERE I HAVE A LOG');
    console.log('user', user);
    return user;
  }
}

//

I suspect the issue is on my Authorized domains but not sure how to solve it.

Thank you in advance.

Environment

  • Operating System: Linux
  • Node version: 14.18.1
  • passport version: @passport-next/passport-oauth2 2.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions