api login

This commit is contained in:
JakkrapartXD 2026-01-09 10:14:13 +00:00
parent bd0daf5858
commit 1a7473362b
8 changed files with 613 additions and 60 deletions

View file

@ -19,34 +19,33 @@ export class AuthService {
* User login
*/
async login(data: LoginRequest): Promise<LoginResponse> {
const { username, password } = data;
const { email, password } = data;
// Find user with role and profile
const user = await prisma.user.findUnique({
where: { username },
where: { email },
include: {
role: true,
profile: true
}
});
if (!user) {
logger.warn('Login attempt with invalid username', { username });
throw new UnauthorizedError('Invalid username or password');
logger.warn('Login attempt with invalid email', { email });
throw new UnauthorizedError('Invalid email or password');
}
// Verify password
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) {
logger.warn('Login attempt with invalid password', { username });
throw new UnauthorizedError('Invalid username or password');
logger.warn('Login attempt with invalid password', { email });
throw new UnauthorizedError('Invalid email or password');
}
// Generate tokens
const token = this.generateAccessToken(user.id, user.username, user.email, user.role.code);
const token = this.generateAccessToken(user.id, user.email, user.email, user.role.code);
const refreshToken = this.generateRefreshToken(user.id);
logger.info('User logged in successfully', { userId: user.id, username: user.username });
logger.info('User logged in successfully', { userId: user.id, email: user.email });
return {
token,