127 lines
2.9 KiB
TypeScript
127 lines
2.9 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { Student } from '../student/student.entity';
|
|
import * as bcrypt from 'bcrypt';
|
|
import * as nodemailer from 'nodemailer';
|
|
import { NotFoundException, UnauthorizedException } from '@nestjs/common';
|
|
|
|
@Injectable()
|
|
export class AuthService {
|
|
|
|
constructor(
|
|
@InjectRepository(Student)
|
|
private studentRepository: Repository<Student>,
|
|
) {}
|
|
|
|
// REGISTER PASSWORD
|
|
async register(data: any) {
|
|
|
|
const student = await this.studentRepository.findOne({
|
|
where: { EmailAddress: data.email }
|
|
});
|
|
|
|
if (!student) {
|
|
throw new NotFoundException('Email not found');
|
|
|
|
}
|
|
|
|
if (data.password !== data.confirmPassword) {
|
|
throw new Error('Passwords do not match');
|
|
}
|
|
|
|
const hashedPassword = await bcrypt.hash(data.password, 10);
|
|
|
|
student.Password = hashedPassword;
|
|
student.StudentStatus = 'Active'; // ⭐ IMPORTANT LINE
|
|
student.LastUpdatedDate = new Date();
|
|
student.LastUpdatedBy = 'System';
|
|
|
|
await this.studentRepository.save(student);
|
|
|
|
return {
|
|
message: 'Registration successful'
|
|
};
|
|
}
|
|
|
|
// LOGIN
|
|
async login(data: any) {
|
|
|
|
const student = await this.studentRepository.findOne({
|
|
where: { EmailAddress: data.email }
|
|
});
|
|
|
|
if (!student) {
|
|
throw new NotFoundException("Email not found");
|
|
}
|
|
|
|
const match = await bcrypt.compare(data.password, student.Password);
|
|
|
|
if (!match) {
|
|
throw new UnauthorizedException("Invalid password");
|
|
}
|
|
|
|
return {
|
|
message: "Login successful",
|
|
studentId: student.StudentID,
|
|
name: student.Name
|
|
};
|
|
}
|
|
|
|
// FORGOT PASSWORD (SEND RESET LINK)
|
|
async forgotPassword(data: any) {
|
|
|
|
const student = await this.studentRepository.findOne({
|
|
where: { EmailAddress: data.email }
|
|
});
|
|
|
|
if (!student) {
|
|
return { message: "Email not found" };
|
|
}
|
|
|
|
const resetLink = `http://localhost:3000/reset-password?email=${data.email}`;
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
service: 'gmail',
|
|
auth: {
|
|
user: '2003nithi@gmail.com',
|
|
pass: 'qgxu ufou sycd iyjm'
|
|
}
|
|
});
|
|
|
|
await transporter.sendMail({
|
|
from: 'yourgmail@gmail.com',
|
|
to: data.email,
|
|
subject: 'Password Reset Link',
|
|
text: `Click the link to reset your password: ${resetLink}`
|
|
});
|
|
|
|
return {
|
|
message: "Password reset link sent to your email"
|
|
};
|
|
}
|
|
|
|
// RESET PASSWORD
|
|
async resetPassword(data: any) {
|
|
|
|
const student = await this.studentRepository.findOne({
|
|
where: { EmailAddress: data.email }
|
|
});
|
|
|
|
if (!student) {
|
|
return { message: "Invalid email address" };
|
|
}
|
|
const bcrypt=require('bcrypt');
|
|
|
|
const hashedPassword = await bcrypt.hash(data.newPassword, 10);
|
|
|
|
student.Password = hashedPassword;
|
|
|
|
await this.studentRepository.save(student);
|
|
|
|
return {
|
|
message: "Password reset successfully"
|
|
};
|
|
}
|
|
|
|
} |