Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions apps/api/src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { AuthService, GoogleStrategy, JwtStrategy, LocalStrategy } from '@gh/auth';
import { PrismaService } from '@gh/prisma';
import { UsersService } from '@gh/users';
import { AuthModule as LibAuthModule } from '@gh/auth';
import { PrismaModule } from '@gh/prisma';
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { AuthController } from './auth.controller';

@Module({
imports: [
PassportModule,
JwtModule,
],
providers: [AuthService, LocalStrategy, JwtStrategy, GoogleStrategy, PrismaService, UsersService],
imports: [LibAuthModule, PrismaModule],
controllers: [AuthController],
})
export class AuthModule {}
2 changes: 2 additions & 0 deletions apps/api/src/modules/github/github.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AuthModule } from '../auth/auth.module';
import { UsersModule } from '../users/users.module';
import { GithubController } from './github.controller';
import { GithubService } from './github.service';

Expand All @@ -14,6 +15,7 @@ import { GithubService } from './github.service';
load: [globalConfig],
}),
AuthModule,
UsersModule,
],
providers: [GithubService],
controllers: [GithubController],
Expand Down
23 changes: 13 additions & 10 deletions apps/api/src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
import { JwtAuthGuard } from '@gh/auth';
import { UsersService } from '@gh/users';
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, UseGuards } from '@nestjs/common';
import { CreateUserDto, UpdateUserDto } from './dtos';
import { User } from '@gh/prisma';
import { CreateUserDto, UpdateUserDto } from '@gh/shared';
import { loggedMethod, MICROSERVICE_NAME_USERS } from '@gh/shared/utils';
import { Body, Controller, Delete, Get, Inject, Param, ParseIntPipe, Patch, Post, UseGuards } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';

@Controller('auth-users')
export class UsersController {
constructor(private usersService: UsersService) {}
constructor(@Inject(MICROSERVICE_NAME_USERS) private readonly usersMicroservice: ClientProxy) {}

@UseGuards(JwtAuthGuard)
@Get()
@loggedMethod('API Gateway: Users controller: Get all users')
getUsers() {
return this.usersService.getUsers();
return this.usersMicroservice.send<User[]>('get_users', {});
}

@UseGuards(JwtAuthGuard)
@Get(':id')
getUserById(@Param('id', ParseIntPipe) id: number) {
return this.usersService.getUserById(id);
return this.usersMicroservice.send<User>('get_user_by_id', id);
}

@UseGuards(JwtAuthGuard)
@Get('email/:emailAddress')
getUserByEmail(@Param('emailAddress') email: string) {
return this.usersService.getUserByEmail(email);
return this.usersMicroservice.send<User>('get_user_by_email', email);
}

@UseGuards(JwtAuthGuard)
@Post()
createUser(@Body() data: CreateUserDto) {
return this.usersService.createUser(data);
return this.usersMicroservice.send<User>('create_user', data);
}

@UseGuards(JwtAuthGuard)
@Patch(':id')
updateUser(@Param('id', ParseIntPipe) id: number, @Body() data: UpdateUserDto) {
return this.usersService.updateUser(id, data);
return this.usersMicroservice.send<User>('update_user', data);
}

@UseGuards(JwtAuthGuard)
@Delete(':id')
deleteUserById(@Param('id', ParseIntPipe) id: number) {
return this.usersService.deleteUserById(id);
return this.usersMicroservice.send<User>('delete_user_by_id', id);
}
}
23 changes: 19 additions & 4 deletions apps/api/src/modules/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { PrismaService } from '@gh/prisma';
import { UsersService } from '@gh/users';
import { MICROSERVICE_NAME_USERS } from '@gh/shared/utils';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { UsersController } from './users.controller';

@Module({
providers: [UsersService, PrismaService],
exports: [UsersService],
imports: [
ClientsModule.registerAsync([
{
name: MICROSERVICE_NAME_USERS,
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
transport: Transport.TCP,
options: {
host: configService.get<string>('USERS_MICROSERVICE_HOST') ?? 'localhost',
port: configService.get<number>('USERS_MICROSERVICE_PORT') ?? 3001,
},
}),
inject: [ConfigService],
},
]),
],
controllers: [UsersController],
})
export class UsersModule {}
3 changes: 2 additions & 1 deletion apps/ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
"strictTemplates": true,
"typeCheckHostBindings": true,
}
}
3 changes: 1 addition & 2 deletions libs/auth/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export default [
onlyDependOnLibsWithTags: [
'scope:lib-config',
'scope:lib-prisma',
'scope:lib-shared',
'scope:lib-users',
'scope:lib-shared'
]
},
],
Expand Down
31 changes: 31 additions & 0 deletions libs/auth/src/lib/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { MICROSERVICE_NAME_USERS } from '@gh/shared/utils';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { AuthService } from './auth.service';
import { GoogleStrategy } from './google.strategy';
import { JwtStrategy } from './jwt.strategy';
import { LocalStrategy } from './local.strategy';

@Module({
imports: [JwtModule,
ClientsModule.registerAsync([
{
name: MICROSERVICE_NAME_USERS,
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
transport: Transport.TCP,
options: {
host: configService.get<string>('USERS_MICROSERVICE_HOST') ?? 'localhost',
port: configService.get<number>('USERS_MICROSERVICE_PORT') ?? 3001,
},
}),
inject: [ConfigService],
},
]),
],
providers: [AuthService, LocalStrategy, JwtStrategy, GoogleStrategy],
exports: [AuthService],
})
export class AuthModule {}
10 changes: 6 additions & 4 deletions libs/auth/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { globalConfig } from '@gh/config';
import { User as PrismaUser } from '@gh/prisma';
import { AuthProfile } from '@gh/shared/models';
import { UsersService } from '@gh/users';
import { MICROSERVICE_NAME_USERS } from '@gh/shared/utils';
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { ClientProxy } from '@nestjs/microservices';
import axios from 'axios';
import bcrypt from 'bcrypt';
import { firstValueFrom } from 'rxjs';

@Injectable()
export class AuthService {
constructor(
@Inject(globalConfig.KEY) private readonly config: ConfigType<typeof globalConfig>,
private readonly usersService: UsersService,
@Inject(MICROSERVICE_NAME_USERS) private readonly usersMicroservice: ClientProxy,
private readonly jwtService: JwtService,
) {}

async validateUser(email: string, password: string) {
const user = await this.usersService.getUserByEmail(email);
const user = await firstValueFrom(this.usersMicroservice.send<PrismaUser>('get_user_by_email', email));

if (await bcrypt.compare(password, user?.password)) {
const { password, ...result } = user;
Expand All @@ -29,7 +31,7 @@ export class AuthService {
}

async validateGoogleUser(email: string) {
const user = await this.usersService.getUserByEmail(email);
const user = await firstValueFrom(this.usersMicroservice.send<PrismaUser>('get_user_by_email', email));

if (user) {
const { password, ...result } = user;
Expand Down
8 changes: 5 additions & 3 deletions libs/auth/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export * from './auth.service'
export * from './auth.module';
export * from './auth.service';
export * from './google-auth.guard';
export * from './google.strategy';
export * from './jwt-auth.guard';
export * from './jwt.strategy'
export * from './jwt.strategy';
export * from './local-auth.guard';
export * from './local.strategy'
export * from './local.strategy';

6 changes: 6 additions & 0 deletions libs/config/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ export const globalConfig = registerAs('config', () => ({
database: {
url: process.env['DATABASE_URL'],
},
microServices: {
users: {
host: process.env['USERS_MICROSERVICE_HOST'] ?? 'localhost',
port: process.env['USERS_MICROSERVICE_PORT'] ?? 3001,
},
}
}));
1 change: 1 addition & 0 deletions libs/prisma/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './prisma.module';
export * from './prisma.service';
8 changes: 8 additions & 0 deletions libs/prisma/src/lib/prisma.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';

@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}
1 change: 1 addition & 0 deletions libs/shared/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './dtos';
export * from './models';
12 changes: 12 additions & 0 deletions libs/shared/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class MicroserviceMetadata {
constructor(
public readonly name: string,
) {}
}

export const MICROSERVICES = {
users: {
name: 'USERS_MICROSERVICE',
} as MicroserviceMetadata
} as const;
export const MICROSERVICE_NAME_USERS = 'USERS_MICROSERVICE';
2 changes: 2 additions & 0 deletions libs/shared/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './constants';
export * from './decorators';

1 change: 1 addition & 0 deletions libs/users/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './users.module';
export * from './users.service';
10 changes: 10 additions & 0 deletions libs/users/src/lib/users.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PrismaModule } from '@gh/prisma';
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';

@Module({
imports: [PrismaModule],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
2 changes: 1 addition & 1 deletion libs/users/src/lib/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type User = {
export class UsersService {
constructor(
@Inject(globalConfig.KEY) private readonly config: ConfigType<typeof globalConfig>,
private prisma: PrismaService,
private readonly prisma: PrismaService,
) {}

async getUsers() {
Expand Down
10 changes: 10 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@
"options": {
"targetName": "eslint:lint"
}
},
{
"plugin": "@nx/webpack/plugin",
"options": {
"buildTargetName": "webpack:build",
"serveTargetName": "webpack:serve",
"previewTargetName": "preview",
"buildDepsTargetName": "build-deps",
"watchDepsTargetName": "watch-deps"
}
}
]
}
Loading
Loading