How to pass dynamic params to redirect callback url in google strategy?

59 Views Asked by At

I want to create a login and register by Google with different roles, like this

/social-auth/google?role=ADMIN

/social-auth/google?role=USER

and to redirect the URL like this

/social-auth/google/redirect?role=ADMIN

/social-auth/google/redirect?role=USER

I have made it like this

google.strategy.ts

import { PassportStrategy } from '@nestjs/passport';
import { Profile, Strategy, VerifyCallback } from 'passport-google-oauth20';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
  constructor(config: ConfigService) {
    super({
      clientID: config.get('GOOGLE_CLIENT_ID'),
      clientSecret: config.get('GOOGLE_CLIENT_SECRET'),
      callbackURL: config.get('GOOGLE_CALLBACK_URL'),
      scope: ['email', 'profile'],
    });
  }

  async validate(
    accessToken: string,
    refreshToken: string,
    profile: Profile,
    done: VerifyCallback,
  ): Promise<any> {
    const { name, emails, photos } = profile;

    const user = {
      email: emails[0].value,
      firstName: name.givenName,
      lastName: name.familyName,
      username: profile.displayName,
      accessToken,
      user_id: profile.id,
    };

    done(null, user);
  }
}

social-auth.controller.ts

import { Controller, Get, Query, Req, UseGuards } from '@nestjs/common';
import { SocialAuthService } from './social-auth.service';
import { AuthGuard } from '@nestjs/passport';
import { ApiOkResponse, ApiQuery, ApiTags } from '@nestjs/swagger';
import { Role } from '@prisma/client';

@ApiTags('social-auth')
@Controller('social-auth')
export class SocialAuthController {
  constructor(private readonly socialAuthService: SocialAuthService) {}

  @ApiOkResponse({
    description: 'Google login',
  })
  @ApiQuery({
    name: 'role',
    required: true,
    type: String,
    enum: ['ADMIN', 'FREELANCER', 'CLIENT'],
    description: 'role of user',
  })
  @Get('google')
  @UseGuards(AuthGuard('google'))
  async googleAuth(@Req() req: any, @Query('role') role: string) {}

  @ApiOkResponse({
    description: 'Google login redirect',
  })
  @Get('google/redirect')
  @UseGuards(AuthGuard('google'))
  googleAuthRedirect(@Req() req: any) {
    return this.socialAuthService.googleLogin(req);
  }
}

social-auth.service.ts

import { Module } from '@nestjs/common';
import { SocialAuthService } from './social-auth.service';
import { SocialAuthController } from './social-auth.controller';
import { GoogleStrategy } from './strategy/google.strategy';
import { AuthService } from '../auth/auth.service';
import { PrismaService } from '../infra/database/prisma/prisma.service';
import { JwtModule } from '@nestjs/jwt';

@Module({
  imports: [JwtModule.register({})],
  controllers: [SocialAuthController],
  providers: [SocialAuthService, GoogleStrategy, AuthService, PrismaService],
})
export class SocialAuthModule {}

How do i do it with dynamic parmas callback url? Or are there any suggestions for a better way to register and log in to Google with different roles?

0

There are 0 best solutions below