nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.73k stars 7.63k forks source link

I have a question about Google Social Login. #13790

Closed jungsikjeong closed 3 months ago

jungsikjeong commented 3 months ago

Is there an existing issue for this?

Current behavior

my-server_1    | TypeError: res.setHeader is not a function
my-server_1    |     at JwtGoogleStrategy.strategy.redirect (/myFolder/node_modules/passport/lib/middleware/authenticate.js:340:13)
my-server_1    |     at stored (/myFolder/node_modules/passport-oauth2/lib/strategy.js:285:14)
my-server_1    |     at NullStore.store (/myFolder/node_modules/passport-oauth2/lib/state/null.js:5:3)
my-server_1    |     at JwtGoogleStrategy.OAuth2Strategy.authenticate (/myFolder/node_modules/passport-oauth2/lib/strategy.js:297:28)
my-server_1    |     at attempt (/myFolder/node_modules/passport/lib/middleware/authenticate.js:378:16)
my-server_1    |     at authenticate (/myFolder/node_modules/passport/lib/middleware/authenticate.js:379:7)
my-server_1    |     at /myFolder/node_modules/@nestjs/passport/dist/auth.guard.js:88:3
my-server_1    |     at new Promise (<anonymous>)
my-server_1    |     at /myFolder/node_modules/@nestjs/passport/dist/auth.guard.js:80:83
my-server_1    |     at GqlAuthGuard.canActivate (/myFolder/node_modules/@nestjs/passport/dist/auth.guard.js:44:32)

Currently this error occurs.

Minimum reproduction code

https://codesandbox.io/p/devbox/github/nestjs/typescript-starter/tree/master

Steps to reproduce

No response

Expected behavior

  1. auth.resolver.ts This is part of the code.

    @UseGuards(GqlAuthGuard('google'))
    @Mutation(() => String)
    loginGoogle(
    @Context() context: IContext, 
    ): void {
    console.log('context::', context); 
    }
  2. GqlAuthGuard.ts

    
    import { ExecutionContext } from '@nestjs/common';
    import { GqlExecutionContext } from '@nestjs/graphql';
    import { AuthGuard } from '@nestjs/passport';

export const GqlAuthGuard = (name: string) => { return class GqlAuthGuard extends AuthGuard(name) { getRequest(context: ExecutionContext) { const gqlContext = GqlExecutionContext.create(context);

  return gqlContext.getContext().req;
}

}; };


3. jwt-social-google.strategy.ts

```js
import { UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-google-oauth20';

export class JwtGoogleStrategy extends PassportStrategy(Strategy, 'google') {
  constructor() {
    super({
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: 'http://localhost:3001/login',
      scope: ['email', 'profile'],
    });
  }

  validate(accessToken, profile, done) {
    console.log('profile:', profile);
    return 'success';
    if (!profile) {
      // return done(new UnauthorizedException(), false);
    }
    // return done(null, profile);
  }
}
  1. Of course, I finished connecting to the module.
    • auth.module.ts
      
      import { Module } from '@nestjs/common';
      import { AuthResolver } from './auth.resolver';
      import { AuthService } from './auth.service';

import { JwtModule } from '@nestjs/jwt'; import { UsersModule } from '../users/users.module'; import { JwtAccessStrategies } from './strategies/jwt-access.strategy'; import { JwtRefreshStrategies } from './strategies/jwt-refresh.strategy'; import { JwtGoogleStrategy } from './strategies/jwt-social-google.strategy';

@Module({ imports: [ JwtModule.register({}), // UsersModule, ],

providers: [ JwtAccessStrategies, JwtRefreshStrategies, JwtGoogleStrategy, AuthResolver, // AuthService, ], }) export class AuthModule {}


5. app.module.ts

```js
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './apis/auth/auth.module';
import { UsersModule } from './apis/users/users.module';
import { PostsModule } from './apis/posts/posts.module';
import { FilesModule } from './apis/files/files.module';
import { TagsModule } from './apis/tags/tags.module';

@Module({
  imports: [
    AuthModule, //
    UsersModule,
    PostsModule,
    FilesModule,
    TagsModule,
    ConfigModule.forRoot(),
    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      autoSchemaFile: 'src/commons/graphql/schema.gql',
      context: ({ req, res }) => ({ req, res }),
      playground: true,
    }),

    TypeOrmModule.forRoot({
      type: process.env.DATABASE_TYPE as 'mysql',
      host: process.env.DATABASE_HOST,
      port: Number(process.env.DATABASE_PORT),
      username: process.env.DATABASE_USERNAME,
      password: process.env.DATABASE_PASSWORD,
      database: process.env.DATABASE_DATABASE,
      entities: [__dirname + '/apis/**/*.entity.*'],
      synchronize: true,
      logging: true,
    }),
  ],
})
export class AppModule {}

I don't think I'm wrong, but why does this error occur?

Package

Other package

No response

NestJS version

No response

Packages versions

{
  "name": "server",
  "version": "0.0.1",
  "description": "",
  "author": "jungsikjeong",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@apollo/server": "^4.10.4",
    "@nestjs/apollo": "^12.2.0",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.2.3",
    "@nestjs/core": "^10.0.0",
    "@nestjs/graphql": "^12.2.0",
    "@nestjs/jwt": "^10.2.0",
    "@nestjs/passport": "^10.0.3",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/typeorm": "^10.0.2",
    "@types/bcrypt": "^5.0.2",
    "@types/graphql-upload": "^8.0.12",
    "@types/passport-jwt": "^4.0.1",
    "aws-sdk": "^2.1652.0",
    "bcrypt": "^5.1.1",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "graphql": "^16.9.0",
    "graphql-upload": "^13.0.0",
    "mysql2": "^3.10.2",
    "passport": "^0.7.0",
    "passport-google-oauth20": "^2.0.0",
    "passport-jwt": "^4.0.1",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1",
    "typeorm": "^0.3.20",
    "uuid": "^10.0.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/passport-google-oauth20": "^2.0.16",
    "@types/supertest": "^6.0.0",
    "@types/uuid": "^10.0.0",
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

No response

In which operating systems have you tested?

Other

No response

micalevisk commented 3 months ago

:warning: We use GitHub Issues to track bug reports, feature requests and regressions

Try the following instead: