Skip to content

Commit e931db0

Browse files
committedNov 4, 2024
finish the auth controllers and modules and start the profile module and services
1 parent e6649fa commit e931db0

9 files changed

+153
-36
lines changed
 

‎api/README.md ‎README.md

+8
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,11 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
9797
## License
9898

9999
Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
100+
101+
# IBDEV TODO APP
102+
## Tech Stack:
103+
* Backend:
104+
- Nests
105+
- Prisma & Mysql
106+
* Frontend:
107+
- React

‎api/src/auth/auth.controller.ts

+46-31
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,76 @@
1-
import { Body, Controller, Get, HttpCode, HttpStatus, Patch, Post, Query, Req, Res, UseGuards } from '@nestjs/common';
1+
import {
2+
Body,
3+
Controller,
4+
Get,
5+
HttpCode,
6+
HttpStatus,
7+
Patch,
8+
Post,
9+
Query,
10+
Req,
11+
Res,
12+
UseGuards,
13+
} from '@nestjs/common';
214
import { AuthService } from './auth.service';
315
import { LocalUserRegisterDto } from './dto/LocalUserRegisterDto';
416
import { LocalUserLoginDto } from './dto/LocalUserLoginDto';
517
import { AuthGuard } from '@nestjs/passport';
618
import { AuthReq, GeneralAuthenticator } from './guards/GeneralAuthenticator';
7-
import { query, Response } from 'express';
19+
import { Response } from 'express';
820
import { ResetPasswordDto } from './dto/ResetPasswordDto';
921
import { ChangePasswordDto } from './dto/ChangePasswordDto';
1022

11-
12-
@Controller("auth")
23+
@Controller('auth')
1324
export class AuthController {
14-
15-
16-
constructor(private readonly authService:AuthService) {}
17-
25+
constructor(private readonly authService: AuthService) {}
26+
// check authenticated User
1827
@Get('check')
1928
@UseGuards(GeneralAuthenticator)
20-
checkAuth(@Req() req:AuthReq){
21-
return this.authService.checkAuth(req)
29+
checkAuth(@Req() req: AuthReq) {
30+
return this.authService.checkAuth(req);
2231
}
2332
@Get('google')
2433
@UseGuards(AuthGuard('google'))
25-
googleRedirect(){}
26-
27-
28-
@Get("/reset")
29-
checkIsValidResetUrl(@Query("token") token:string){
30-
return this.authService.checkResetPasswordUrlValid(token);
31-
}
34+
googleRedirect() {}
3235

3336
@Get('google/redirect')
3437
@UseGuards(AuthGuard('google'))
35-
googleLoginUserRedirect(@Req() req:AuthReq, @Res({passthrough:true})res:Response){
36-
37-
38-
return this.authService.loginGoogleUSer(req,res);
38+
googleLoginUserRedirect(
39+
@Req() req: AuthReq,
40+
@Res({ passthrough: true }) res: Response,
41+
) {
42+
return this.authService.loginGoogleUSer(req, res);
3943
}
4044

4145
// local authentication routes
42-
@Post("register")
46+
@Post('register')
4347
registerUserByEmail(@Body() localUserRegisterDto: LocalUserRegisterDto) {
4448
return this.authService.registerLocalUser(localUserRegisterDto);
4549
}
46-
@Post("login")
50+
@Post('login')
4751
@HttpCode(HttpStatus.OK)
48-
loginLocalAccount(@Body() localUserLoginDto: LocalUserLoginDto, @Res({passthrough:true})res:Response) {
52+
loginLocalAccount(
53+
@Body() localUserLoginDto: LocalUserLoginDto,
54+
@Res({ passthrough: true }) res: Response,
55+
) {
4956
return this.authService.loginLocalUser(localUserLoginDto, res);
5057
}
5158

52-
@Post("reset")
53-
resetPasswordRequest(@Body() resetPasswordDto:ResetPasswordDto){
59+
// reset password mechanisme
60+
@Get('/reset')
61+
checkIsValidResetUrl(@Query('token') token: string) {
62+
return this.authService.checkResetPasswordUrlValid(token);
63+
}
64+
@Post('reset')
65+
@HttpCode(HttpStatus.OK)
66+
resetPasswordRequest(@Body() resetPasswordDto: ResetPasswordDto) {
5467
return this.authService.requestResetPassword(resetPasswordDto);
5568
}
56-
@Patch("reset")
57-
changePassword(@Body() changePasswordDto:ChangePasswordDto, @Query('token') token:string){
58-
return this.authService.changePassword(changePasswordDto,token);
59-
69+
@Patch('reset')
70+
changePassword(
71+
@Body() changePasswordDto: ChangePasswordDto,
72+
@Query('token') token: string,
73+
) {
74+
return this.authService.changePassword(changePasswordDto, token);
6075
}
61-
}
76+
}

‎api/src/auth/auth.service.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export class AuthService {
229229
<p>We received a request to reset your password. Click the button below to set up a new password:</p>
230230
<a href="${process.env.CLIENT_URL}/reset?token=${token}" class="button">Reset Password</a>
231231
<p>If you didn't request a password reset, please ignore this email or contact support if you have concerns.</p>
232-
<p>Thanks,<br>The {{Your Brand}} Team</p>
232+
<p>Thanks,<br>The IBDEV Team</p>
233233
</div>
234234
<div class="footer">
235235
<p>If you have any questions, contact us at support@ibdev-todo.com</p>
@@ -246,15 +246,15 @@ export class AuthService {
246246
if (!user || !user.password)
247247
throw new NotFoundException('can not find user', {
248248
cause: 'invalid informations',
249-
description: 'the email pprovided is not linked to any local account',
249+
description: 'the email provided is not linked to any local account',
250250
});
251251
const token = await this.jwtService.signAsync(
252252
{ email: email },
253253
{ expiresIn: '15m', secret: process.env.REFRESH_PASS_SECRET },
254254
);
255255
const htmlEmailToSend: string = this.mailCreator(token);
256256
const result = await this.mailerService.sendMail({
257-
from: `<IBDEV-TODO>:${process.env.EMAIL}`,
257+
from: `"IBDEV-TODO"<ibrahimelkhalilbenyahia@gmail.com>`,
258258
to: email,
259259
subject: 'Reset Password',
260260
html: htmlEmailToSend,

‎api/src/auth/guards/GoogleAuthenticator.ts

-2
This file was deleted.

‎api/src/decorators/User.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createParamDecorator, ExecutionContext } from "@nestjs/common";
2+
import { AuthReq } from "src/auth/guards/GeneralAuthenticator";
3+
4+
5+
export const User = createParamDecorator((key:'pid' | 'uid',cotext:ExecutionContext):string=>{
6+
const request = cotext.switchToHttp().getRequest<AuthReq>();
7+
return request.user[key];
8+
})
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { IsOptional, IsString, Matches, MaxLength } from "class-validator"
2+
3+
export class UpdateProfileDto {
4+
@IsString()
5+
@IsOptional()
6+
@MaxLength(35)
7+
@Matches(/^[A-Za-zÀ-ÖØ-öø-ÿ]+(?:[-'\s][A-Za-zÀ-ÖØ-öø-ÿ]+)*$/, {message:"invalid name typed"})
8+
first_name?:string
9+
@IsString()
10+
@IsOptional()
11+
@MaxLength(35)
12+
@Matches(/^[A-Za-zÀ-ÖØ-öø-ÿ]+(?:[-'\s][A-Za-zÀ-ÖØ-öø-ÿ]+)*$/, {message:"invalid name typed"})
13+
last_name?:string
14+
@IsString()
15+
@IsOptional()
16+
@MaxLength(300)
17+
bio?:string
18+
}

‎api/src/profile/profile.controller.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Controller,Delete,Get,Patch,UseGuards } from "@nestjs/common";
2+
import { GeneralAuthenticator } from "src/auth/guards/GeneralAuthenticator";
3+
4+
@Controller("profile")
5+
@UseGuards(GeneralAuthenticator)
6+
export class ProfileController{
7+
8+
9+
@Get()
10+
getUserProfile(){}
11+
12+
@Patch()
13+
updateUserProfile(){}
14+
15+
@Delete()
16+
deleteUserProfile(){}
17+
}

‎api/src/profile/profile.module.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from "@nestjs/common";
2+
import { ProfileController } from "./profile.controller";
3+
4+
5+
@Module({
6+
providers:[],
7+
controllers:[ProfileController],
8+
imports:[],
9+
exports:[]
10+
})
11+
export class ProfileModule {}

‎api/src/profile/profile.service.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Injectable, NotFoundException } from '@nestjs/common';
2+
import { Profile } from '@prisma/client';
3+
import { PrismaService } from 'src/prisma/prisma.service';
4+
import { UpdateProfileDto } from './dto/UpdateProfileDto';
5+
6+
@Injectable()
7+
export class ProfileService {
8+
constructor(private readonly prismaService: PrismaService) {}
9+
10+
async getUserProfile(profileId: number) {
11+
const profile: Profile = await this.prismaService.profile.findUnique({
12+
where: { id: profileId },
13+
});
14+
if (!profile)
15+
throw new NotFoundException('Can not find the profile', {
16+
description: 'id does not refer to any profile',
17+
cause: 'invalid id',
18+
});
19+
return { profile };
20+
}
21+
22+
async updateUserProfile(
23+
profileId: number,
24+
updateProfileDto: UpdateProfileDto,
25+
) {
26+
const profile: Profile = await this.prismaService.profile.findUnique({
27+
where: { id: profileId },
28+
});
29+
if (!profile)
30+
throw new NotFoundException('Can not find the profile', {
31+
description: 'id does not refer to any profile',
32+
cause: 'invalid id',
33+
});
34+
35+
await this.prismaService.profile.update({
36+
where: { id: profileId },
37+
data: updateProfileDto,
38+
});
39+
40+
return {message:"user profile updated successfull"}
41+
}
42+
}

0 commit comments

Comments
 (0)
Please sign in to comment.