-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moved logic to auth icroservice
- Loading branch information
Showing
40 changed files
with
261 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common' | ||
|
||
import { AuthResolver } from './auth.resolver' | ||
import { KafkaModule } from '@code-gear/api/common' | ||
import { Microservice } from '@code-gear/api/common' | ||
|
||
@Module({ | ||
controllers: [], | ||
providers: [AuthResolver], | ||
imports: [KafkaModule.forRoot(Microservice.AUTH)], | ||
exports: [] | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { GqlAuthGuard } from '@code-gear/api/common' | ||
import { Microservice } from '@code-gear/api/common' | ||
import { GqlLocalAuthGuard } from '@code-gear/api/common' | ||
import { JwtAuthGuard } from '@code-gear/api/common' | ||
import { User } from '@code-gear/api/common' | ||
import { WithUser } from '@code-gear/api/common' | ||
import { graphqlArg } from '@code-gear/config' | ||
import { Inject } from '@nestjs/common' | ||
import { OnModuleInit } from '@nestjs/common' | ||
import { UseGuards } from '@nestjs/common' | ||
import { Args } from '@nestjs/graphql' | ||
import { Mutation } from '@nestjs/graphql' | ||
import { Query } from '@nestjs/graphql' | ||
import { Resolver } from '@nestjs/graphql' | ||
|
||
import { AccessTokenResponse } from '@code-gear/api/contracts' | ||
import { AuthTopic } from '@code-gear/api/contracts' | ||
import { SignIn } from '@code-gear/api/contracts' | ||
import { UserResponse } from '@code-gear/api/contracts' | ||
import { ClientKafka } from '@nestjs/microservices' | ||
import { Observable } from 'rxjs' | ||
|
||
@Resolver(() => UserResponse) | ||
export class AuthResolver implements OnModuleInit { | ||
constructor( | ||
@Inject(Microservice.AUTH) private readonly authClient: ClientKafka | ||
) {} | ||
|
||
async onModuleInit() { | ||
this.authClient.subscribeToResponseOf(AuthTopic.SIGN_IN) | ||
this.authClient.subscribeToResponseOf(AuthTopic.GET_PROFILE) | ||
await this.authClient.connect() | ||
} | ||
|
||
@Mutation(() => AccessTokenResponse) | ||
@UseGuards(GqlAuthGuard, GqlLocalAuthGuard) | ||
async signIn( | ||
@Args(graphqlArg) payload: SignIn, | ||
@WithUser() user: User | ||
): Promise<Observable<AccessTokenResponse>> { | ||
return this.authClient.send<AccessTokenResponse>(AuthTopic.SIGN_IN, user) | ||
} | ||
|
||
@Query(() => UserResponse) | ||
@UseGuards(JwtAuthGuard) | ||
async getProfile(@WithUser() user): Promise<UserResponse> { | ||
return user | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import { Controller } from '@nestjs/common' | ||
import { QueryBus } from '@nestjs/cqrs' | ||
import { Controller } from '@nestjs/common' | ||
import { QueryBus } from '@nestjs/cqrs' | ||
import { User } from '@code-gear/api/common' | ||
import { MessagePattern } from '@nestjs/microservices' | ||
import { Payload } from '@nestjs/microservices' | ||
import { AccessTokenResponse } from '@code-gear/api/contracts' | ||
import { AuthTopic } from '@code-gear/api/contracts' | ||
import { AuthService } from './auth.service' | ||
|
||
@Controller() | ||
export class TemplateConsumer { | ||
constructor(private readonly query: QueryBus) {} | ||
export class AuthConsumer { | ||
constructor( | ||
private readonly query: QueryBus, | ||
private readonly authService: AuthService | ||
) {} | ||
|
||
@MessagePattern(AuthTopic.SIGN_IN) | ||
async signIn(@Payload() user: User): Promise<AccessTokenResponse> { | ||
return this.authService.generateToken(user.username) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
import { HttpModule } from '@nestjs/axios' | ||
import { Module } from '@nestjs/common' | ||
import { HttpModule } from '@nestjs/axios' | ||
import { Module } from '@nestjs/common' | ||
|
||
import { TemplateConsumer } from './auth.consumer' | ||
import { EnvModule } from '@code-gear/api/common' | ||
import { ListenerModule } from '@code-gear/api/common' | ||
import { KafkaService } from '@code-gear/api/common' | ||
import { QueryHandlers } from '@/queries/handlers' | ||
import { CqrsModule } from '@nestjs/cqrs' | ||
import { AuthConsumer } from './auth.consumer' | ||
import { DatabaseModule } from '@code-gear/api/common' | ||
import { EnvModule } from '@code-gear/api/common' | ||
import { JwtModule } from '@code-gear/api/common' | ||
import { KafkaService } from '@code-gear/api/common' | ||
import { ListenerModule } from '@code-gear/api/common' | ||
import { CqrsModule } from '@nestjs/cqrs' | ||
import { AuthService } from './auth.service' | ||
import { AccountsRepository } from './repository/accounts.repository' | ||
import { JwtStrategy } from '@/strategies/jwt.strategy' | ||
import { LocalStrategy } from '@/strategies/local.strategy' | ||
|
||
@Module({ | ||
imports: [ | ||
HttpModule, | ||
EnvModule, | ||
CqrsModule, | ||
JwtModule, | ||
DatabaseModule, | ||
ListenerModule.forRoot({ | ||
isMicroservice: true, | ||
}), | ||
isMicroservice: true | ||
}) | ||
], | ||
providers: [KafkaService, ...QueryHandlers], | ||
controllers: [TemplateConsumer], | ||
exports: [], | ||
providers: [ | ||
KafkaService, | ||
AuthService, | ||
AccountsRepository, | ||
JwtStrategy, | ||
LocalStrategy | ||
], | ||
controllers: [AuthConsumer], | ||
exports: [] | ||
}) | ||
export class TemplateModule {} | ||
export class AuthModule {} |
Oops, something went wrong.