Skip to content

Commit

Permalink
feat: 🎸 add passport module
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatpotato13 committed Jan 12, 2025
1 parent 43ac515 commit 3c7ef06
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"test:e2e": "jest --config ./test/jest-e2e.json",
"sdk": "nestia sdk 'src/**/*.controller.ts' --out 'src/api'",
"swagger": "nestia swagger 'src/**/*.controller.ts' --out './public/swagger.json'",
"prisma:generate": "prisma generate",
"prisma:migrate": "prisma migrate dev",
"prisma:generate": "prisma generate",
"prisma:studio": "prisma studio",
"prisma:format": "prisma format",
"prepare": "ts-patch install && typia patch && husky"
Expand Down
7 changes: 2 additions & 5 deletions src/modules/auth/app/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class AuthController {
private googleAuthEnabled: boolean;

constructor(
@Inject("AuthService") private readonly _service: AuthService,
@Inject("AuthService") private readonly service: AuthService,
@Optional()
@Inject(GoogleOauthConfig.KEY)
config?: ConfigType<typeof GoogleOauthConfig>
Expand Down Expand Up @@ -62,10 +62,7 @@ export class AuthController {
@Get("login/google/callback")
@UseGuards(AuthGuard("google"))
handleRedirect(@Req() req: Request): Promise<TokensResponseDto> {
if (!this.googleAuthEnabled) {
throw new Error("Google authentication is not configured");
}
const { user } = req;
return this._service.googleLogin(UserDto.of(user as any));
return this.service.googleLogin(UserDto.of(user as any));
}
}
2 changes: 1 addition & 1 deletion src/modules/auth/app/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TokensResponseDto, UserDto } from "../domain/dtos";
export class AuthService {
constructor(
private readonly commandBus: CommandBus,
private readonly queryBus: QueryBus
private readonly _queryBus: QueryBus
) {}

public async googleLogin(user: UserDto): Promise<TokensResponseDto> {
Expand Down
28 changes: 2 additions & 26 deletions src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigType } from "@nestjs/config";
import { CqrsModule } from "@nestjs/cqrs";
import { GoogleOauthConfig } from "@src/config";
import { JwtModule } from "@src/shared/modules";
import { JwtModule, PassportModule } from "@src/shared/modules";
import { PrismaService } from "@src/shared/services/prisma.service";

import { AuthController } from "./app/auth.controller";
import { AuthService } from "./app/auth.service";
import { CommandHandlers } from "./domain/commands/handlers";
import { QueryHandlers } from "./domain/queries/handlers";
import { GoogleStrategy } from "./infrastructures/google.strategy";

@Module({
imports: [
ConfigModule.forFeature(GoogleOauthConfig),
CqrsModule,
JwtModule
],
imports: [PassportModule, CqrsModule, JwtModule],
providers: [
{ provide: "AuthService", useClass: AuthService },
{ provide: "PrismaService", useClass: PrismaService },
{
provide: GoogleStrategy,
useFactory: (
config: ConfigType<typeof GoogleOauthConfig>,
service: AuthService
) => {
if (
!config.clientId ||
!config.clientSecret ||
!config.redirect
) {
return null;
}
return new GoogleStrategy(config, service);
},
inject: [GoogleOauthConfig.KEY, "AuthService"]
},
...CommandHandlers,
...QueryHandlers
],
Expand Down
4 changes: 1 addition & 3 deletions src/modules/auth/infrastructures/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { GoogleStrategy } from "./google.strategy";

export { GoogleStrategy };
export {};
9 changes: 9 additions & 0 deletions src/shared/modules/elasticsearch/elasticsearch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ export class ElasticsearchService {
node: this.config.node
});
}

public async index(index: string, document: any): Promise<void> {
await this.client.index({ index, document });
}

public async search(index: string, query: any): Promise<any> {
const result = await this.client.search({ index, body: query });
return result;
}
}
9 changes: 8 additions & 1 deletion src/shared/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ElasticsearchModule } from "./elasticsearch/elasticsearch.module";
import { JwtModule } from "./jwt/jwt.module";
import { KafkaModule } from "./kafka/kafka.module";
import { PassportModule } from "./passport/passport.module";
import { RedisModule } from "./redis/redis.module";

export { ElasticsearchModule, JwtModule, KafkaModule, RedisModule };
export {
ElasticsearchModule,
JwtModule,
KafkaModule,
PassportModule,
RedisModule
};
4 changes: 3 additions & 1 deletion src/shared/modules/kafka/kafka.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ClientsModule } from "@nestjs/microservices";
import { KafkaConfg } from "@src/config";
import { KafkaConfigService } from "@src/config/modules/kafka/kafka.config.service";

import { KafkaService } from "./kafka.service";

@Module({
imports: [
ClientsModule.registerAsync([
Expand All @@ -14,7 +16,7 @@ import { KafkaConfigService } from "@src/config/modules/kafka/kafka.config.servi
}
])
],
providers: [],
providers: [KafkaService],
controllers: []
})
export class KafkaModule {}
13 changes: 13 additions & 0 deletions src/shared/modules/kafka/kafka.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Inject, Injectable } from "@nestjs/common";
import { ClientKafka } from "@nestjs/microservices";

@Injectable()
export class KafkaService {
constructor(@Inject("KAFKA") private readonly kafkaProducer: ClientKafka) {
void this.kafkaProducer.connect();
}

public sendMessage(topic: string, message: string): void {
this.kafkaProducer.emit(topic, message);
}
}
27 changes: 27 additions & 0 deletions src/shared/modules/passport/passport.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigType } from "@nestjs/config";
import { GoogleOauthConfig } from "@src/config";

import { GoogleStrategy } from "./strategies/google.strategy";

@Module({
imports: [ConfigModule.forFeature(GoogleOauthConfig)],
providers: [
{
provide: GoogleStrategy,
useFactory: (config: ConfigType<typeof GoogleOauthConfig>) => {
if (
!config.clientId ||
!config.clientSecret ||
!config.redirect
) {
return null;
}
return new GoogleStrategy(config);
},
inject: [GoogleOauthConfig.KEY]
}
],
controllers: []
})
export class PassportModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import { PassportStrategy } from "@nestjs/passport";
import { GoogleOauthConfig } from "@src/config";
import { Profile, Strategy, VerifyCallback } from "passport-google-oauth20";

import { AuthService } from "../app/auth.service";

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, "google") {
constructor(
@Inject(GoogleOauthConfig.KEY)
private readonly config: ConfigType<typeof GoogleOauthConfig>,
@Inject("AuthService") private readonly service: AuthService
private readonly config: ConfigType<typeof GoogleOauthConfig>
) {
super({
clientID: config.clientId,
Expand Down

0 comments on commit 3c7ef06

Please sign in to comment.