password change and password resert implemented
This commit is contained in:
1 parent
7ecb1fea2e
commit
de05b2ccbe
7 files changed
+260
-36
No files matched your search
@@ -6,6 +6,7 @@
|
||||
|
||||
DATABASE_URL="postgresql://root:admin@localhost:5432/imk?schema=public"
|
||||
JWT_SECRET=some-secret
|
||||
JWT_RESET_SECRET=some-reset-secret-key
|
||||
AWS_REGION=EU2
|
||||
AWS_ACCESS_KEY_ID=4d2f5655369a02100375e3247d7e1fe6
|
||||
AWS_SECRET_ACCESS_KEY=6d4723e14c0d799b89948c24dbe983e4
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
UseGuards,
|
||||
Get,
|
||||
Request,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { LoginDto } from '../dto/login.dto';
|
||||
@@ -39,6 +40,23 @@ export class AuthController {
|
||||
async createAdmin(@Body() createUserDto: CreateUserDto) {
|
||||
return this.authService.createUser(createUserDto);
|
||||
}
|
||||
@Post('forgot-password')
|
||||
async forgotPassword(@Body('email') email: string) {
|
||||
if (!email) {
|
||||
throw new BadRequestException('Email is required');
|
||||
}
|
||||
return this.authService.requestPasswordReset(email);
|
||||
}
|
||||
|
||||
@Post('reset-password')
|
||||
async resetPassword(
|
||||
@Body() body: { token: string; newPassword: string },
|
||||
) {
|
||||
if (!body.token || !body.newPassword) {
|
||||
throw new BadRequestException('Token and new password are required');
|
||||
}
|
||||
return this.authService.resetPassword(body.token, body.newPassword);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('user-info')
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable, ConflictException } from '@nestjs/common';
|
||||
import { Injectable, ConflictException, NotFoundException, BadRequestException } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
@@ -41,35 +41,6 @@ export class AuthService {
|
||||
};
|
||||
}
|
||||
|
||||
// async createUser(
|
||||
// createUserDto: CreateUserDto,
|
||||
// isAdmin: boolean = false,
|
||||
// ): Promise<any> {
|
||||
// const existingUser = await this.prisma.user.findUnique({
|
||||
// where: { email: createUserDto.email },
|
||||
// });
|
||||
|
||||
// if (existingUser) {
|
||||
// throw new ConflictException('Email already exists');
|
||||
// }
|
||||
|
||||
// const hashedPassword = await bcrypt.hash(createUserDto.password, 10);
|
||||
|
||||
// const newUser = await this.prisma.user.create({
|
||||
// data: {
|
||||
// email: createUserDto.email,
|
||||
// password: hashedPassword,
|
||||
// name: createUserDto.name,
|
||||
// isAdmin: isAdmin,
|
||||
// },
|
||||
// });
|
||||
|
||||
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
// const { password, ...result } = newUser;
|
||||
// console.log(result);
|
||||
// return result;
|
||||
// }
|
||||
|
||||
async createUser(createUserDto: CreateUserDto) {
|
||||
const user = await this.prisma.user.create({
|
||||
data: {
|
||||
@@ -85,14 +56,35 @@ export class AuthService {
|
||||
|
||||
async requestPasswordReset(email: string) {
|
||||
const user = await this.prisma.user.findUnique({ where: { email } });
|
||||
if (!user) return;
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
const resetToken = this.jwtService.sign(
|
||||
{ email },
|
||||
{ email, userId: user.id },
|
||||
{ expiresIn: '1h', secret: process.env.JWT_RESET_SECRET },
|
||||
);
|
||||
|
||||
await this.emailService.sendPasswordResetEmail(email, resetToken);
|
||||
return { message: 'Password reset instructions sent to your email' };
|
||||
}
|
||||
|
||||
async resetPassword(token: string, newPassword: string) {
|
||||
try {
|
||||
const payload = this.jwtService.verify(token, {
|
||||
secret: process.env.JWT_RESET_SECRET,
|
||||
});
|
||||
|
||||
const hashedPassword = await bcrypt.hash(newPassword, 10);
|
||||
await this.prisma.user.update({
|
||||
where: { email: payload.email },
|
||||
data: { password: hashedPassword },
|
||||
});
|
||||
|
||||
return { message: 'Password reset successful' };
|
||||
} catch (error) {
|
||||
throw new BadRequestException('Invalid or expired reset token');
|
||||
}
|
||||
}
|
||||
async getUserInfo(userId: number) {
|
||||
if (!userId) {
|
||||
|
||||
Reference in new issue
Block a user