Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tbcct 14 integrate monorepo in client #10

Merged
merged 6 commits into from
Sep 18, 2024
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
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.2",
"@ts-rest/nest": "^3.51.0",
"bcrypt": "catalog:",
"class-transformer": "catalog:",
"lodash": "^4.17.21",
Expand Down
38 changes: 25 additions & 13 deletions api/src/modules/auth/authentication/authentication.controller.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
import { Body, Controller, Post, UseGuards, Headers } from '@nestjs/common';
import {
Body,
Controller,
Post,
UseGuards,
Headers,
UseInterceptors,
ClassSerializerInterceptor,
} from '@nestjs/common';
import { User } from '@shared/entities/users/user.entity';
import { AuthenticationService } from '@api/modules/auth/authentication/authentication.service';
import { LoginDto } from '@api/modules/auth/dtos/login.dto';
import { LocalAuthGuard } from '@api/modules/auth/guards/local-auth.guard';
import { GetUser } from '@api/modules/auth/decorators/get-user.decorator';
import { Public } from '@api/modules/auth/decorators/is-public.decorator';
import { PasswordRecoveryService } from '@api/modules/auth/services/password-recovery.service';
import { authContract } from '@shared/contracts/auth/auth.contract';
import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';

@Controller('authentication')
@Controller()
@UseInterceptors(ClassSerializerInterceptor)
export class AuthenticationController {
constructor(
private authService: AuthenticationService,
private readonly passwordRecovery: PasswordRecoveryService,
) {}

@Public()
@Post('signup')
async signup(@Body() signupDto: LoginDto) {
return this.authService.signUp(signupDto);
}

@Public()
@UseGuards(LocalAuthGuard)
@Post('login')
async login(@GetUser() user: User) {
return this.authService.logIn(user);
@TsRestHandler(authContract.login)
async login(@GetUser() user: User): Promise<ControllerResponse> {
return tsRestHandler(authContract.login, async () => {
const userWithAccessToken = await this.authService.logIn(user);
return {
body: userWithAccessToken,
status: 201,
};
});
}

// TODO: Wrap this in a ts-rest handler
@Public()
@Post('recover-password')
@Post('authentication/recover-password')
async recoverPassword(
@Headers('origin') origin: string,
@Body() body: { email: string },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LoginDto } from '@api/modules/auth/dtos/login.dto';
import { JwtPayload } from '@api/modules/auth/strategies/jwt.strategy';
import { EventBus } from '@nestjs/cqrs';
import { UserSignedUpEvent } from '@api/modules/events/user-events/user-signed-up.event';
import { UserWithAccessToken } from '@shared/dtos/user.dto';

@Injectable()
export class AuthenticationService {
Expand All @@ -32,7 +33,7 @@ export class AuthenticationService {
this.eventBus.publish(new UserSignedUpEvent(newUser.id, newUser.email));
}

async logIn(user: User): Promise<{ user: User; accessToken: string }> {
async logIn(user: User): Promise<UserWithAccessToken> {
const payload: JwtPayload = { id: user.id };
const accessToken: string = this.jwt.sign(payload);
return { user, accessToken };
Expand Down
5 changes: 5 additions & 0 deletions api/src/types/controller-response.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Due to the inability to import TsRest return type, we need to esxplixitly define the return type of the route handler
*/

export type ControllerResponse = unknown;
12 changes: 0 additions & 12 deletions api/test/e2e/features/sign-up.feature

This file was deleted.

94 changes: 0 additions & 94 deletions api/test/e2e/steps/sign-up.steps.ts

This file was deleted.

10 changes: 10 additions & 0 deletions client/lib/queryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { router } from "@shared/contracts";
import { initQueryClient } from "@ts-rest/react-query";

// TODO: We need to get the baseUrl from the environment, pending to decide where to store this data. Right now the API
// is getting all the conf from the shared folder

export const client = initQueryClient(router, {
validateResponse: true,
baseUrl: "localhost:4000",
});
11 changes: 6 additions & 5 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
"lint": "next lint"
},
"dependencies": {
"@ts-rest/react-query": "^3.51.0",
"next": "14.2.8",
"react": "^18",
"react-dom": "^18",
"next": "14.2.8"
"react-dom": "^18"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.2.8",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"eslint": "^8",
"eslint-config-next": "14.2.8"
"typescript": "^5"
}
}
Loading
Loading