Skip to content

Commit

Permalink
refactor: moved logic to auth icroservice
Browse files Browse the repository at this point in the history
  • Loading branch information
gearonix committed Oct 17, 2023
1 parent 94274b6 commit 4282930
Show file tree
Hide file tree
Showing 40 changed files with 261 additions and 208 deletions.
14 changes: 14 additions & 0 deletions .docker/compose/docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ services:
env_file:
- .env
- .docker/.override.env

service-auth:
build:
dockerfile: apps/server/service-auth/Dockerfile
context: .
container_name: service-auth
depends_on:
- kafka
networks:
- cgnet
restart: always
env_file:
- .env
- .docker/.override.env
8 changes: 4 additions & 4 deletions apps/docs/introduction/about-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ could conduct online interviews and edit code in real time with other people.

---

<img src="/overview/functionality-intro.png" width="1558" height="1012" alt="Log preview"/>
<img src="/overview/functionality-intro.png" width="1558" height="1012" alt="preview"/>

## Customization

Expand All @@ -26,7 +26,7 @@ You can customize the color, background, font size, tab size (default is 4 space
You can *find*, *replace* text, create *tabs*,
and also *run your code* (`javascript` and `python` are currently supported).

<img src="/overview/functionality-html.png" width="1558" height="1012" alt="Log preview" />
<img src="/overview/functionality-html.png" width="1558" height="1012" alt="preview" />

::: tip NOTE

Expand All @@ -38,7 +38,7 @@ All your changes are saved to `localStorage` automatically, so you don't have to

You can also log in and send a link to another person so that he can come in and edit the code with you in *real time*.

<img src="/overview/functionality-login.png" width="1558" height="1012" alt="Log preview" />
<img src="/overview/functionality-login.png" width="1558" height="1012" alt="preview" />

## What is the essence of this project?

Expand Down Expand Up @@ -74,4 +74,4 @@ Something like that! 🚀 💫

---

<img src="/overview/functionality-ending.png" width="1558" height="1012" alt="Log preview" />
<img src="/overview/functionality-ending.png" width="1558" height="1012" alt="preview" />
4 changes: 1 addition & 3 deletions apps/server/gateway/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ import { join } from 'path'
import { ApolloDriver } from '@nestjs/apollo'
import { ApolloDriverConfig } from '@nestjs/apollo'
import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { GraphQLModule } from '@nestjs/graphql'

Check warning on line 5 in apps/server/gateway/src/app.module.ts

View workflow job for this annotation

GitHub Actions / Linting project

'ConfigModule' is defined but never used. Allowed unused vars must match /^_/u

import { AuthModule } from '@/auth'

import { CodeExecutorModule } from './core/code-executor-api'
import { CodeExecutorModule } from './core/code-executor'
import { EnvModule } from '@code-gear/api/common'
import { ListenerModule } from '@code-gear/api/common'

@Module({
imports: [
CodeExecutorModule,
EnvModule,
AuthModule,
ListenerModule.forRoot({
isMicroservice: false
}),
Expand Down
26 changes: 0 additions & 26 deletions apps/server/gateway/src/auth/auth.module.ts

This file was deleted.

36 changes: 0 additions & 36 deletions apps/server/gateway/src/auth/auth.resolver.ts

This file was deleted.

54 changes: 0 additions & 54 deletions apps/server/gateway/src/auth/auth.service.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/server/gateway/src/auth/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions apps/server/gateway/src/auth/responses/index.ts

This file was deleted.

24 changes: 0 additions & 24 deletions apps/server/gateway/src/auth/strategies/jwt.strategy.ts

This file was deleted.

13 changes: 13 additions & 0 deletions apps/server/gateway/src/core/auth/auth.module.ts
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 {}
49 changes: 49 additions & 0 deletions apps/server/gateway/src/core/auth/auth.resolver.ts
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
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EndPoints } from '@code-gear/config'
import { Body } from '@nestjs/common'
import {Body, OnModuleInit} from '@nestjs/common'
import { Controller } from '@nestjs/common'
import { Inject } from '@nestjs/common'
import { Post } from '@nestjs/common'
Expand All @@ -15,7 +15,7 @@ import { ExecutorLanguagesValues } from '@code-gear/api/contracts'

@ApiTags('Code executor API')
@Controller(EndPoints.CODE_EXECUTOR_API)
export class CodeExecutorController {
export class CodeExecutorController implements OnModuleInit {
constructor(
@Inject(Microservice.CODE_EXECUTOR) private executorClient: ClientKafka
) {}
Expand Down
3 changes: 0 additions & 3 deletions apps/server/gateway/src/core/users/index.ts

This file was deleted.

12 changes: 0 additions & 12 deletions apps/server/gateway/src/core/users/users.module.ts

This file was deleted.

22 changes: 18 additions & 4 deletions apps/server/service-auth/src/auth.consumer.ts
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)
}
}
41 changes: 27 additions & 14 deletions apps/server/service-auth/src/auth.module.ts
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 {}
Loading

0 comments on commit 4282930

Please sign in to comment.