1
0
forked from noxious/server

Added extra logging to HTTP endpoints

This commit is contained in:
Dennis Postma 2024-11-05 00:33:12 +01:00
parent bf75ad001b
commit 6a76c4797a

View File

@ -5,6 +5,7 @@ import prisma from '../utilities/prisma'
import { User, PasswordResetToken } from '@prisma/client' import { User, PasswordResetToken } from '@prisma/client'
import config from '../utilities/config' import config from '../utilities/config'
import NodeMailer from 'nodemailer' import NodeMailer from 'nodemailer'
import { gameLogger, httpLogger } from '../utilities/logger'
/** /**
* User service * User service
@ -18,17 +19,23 @@ class UserService {
* @param password * @param password
*/ */
async login(username: string, password: string): Promise<boolean | User> { async login(username: string, password: string): Promise<boolean | User> {
const user = await UserRepository.getByUsername(username) try {
if (!user) { const user = await UserRepository.getByUsername(username)
if (!user) {
return false
}
const passwordMatch = await bcrypt.compare(password, user.password)
if (!passwordMatch) {
httpLogger.error(`Failed to login user: ${username}`)
return false
}
return user
} catch (error: any) {
httpLogger.error(`Error logging in user: ${error instanceof Error ? error.message : String(error)}`)
return false return false
} }
const passwordMatch = await bcrypt.compare(password, user.password)
if (!passwordMatch) {
return false
}
return user
} }
/** /**
@ -38,24 +45,30 @@ class UserService {
* @param password * @param password
*/ */
async register(username: string, email: string, password: string): Promise<boolean | User> { async register(username: string, email: string, password: string): Promise<boolean | User> {
const user = await UserRepository.getByUsername(username) try {
if (user) { const user = await UserRepository.getByUsername(username)
return false if (user) {
} return false
const userByEmail = await UserRepository.getByEmail(email)
if (userByEmail) {
return false
}
const hashedPassword = await bcrypt.hash(password, 10)
return prisma.user.create({
data: {
username,
email,
password: hashedPassword
} }
})
const userByEmail = await UserRepository.getByEmail(email)
if (userByEmail) {
httpLogger.error(`User already exists: ${email}`)
return false
}
const hashedPassword = await bcrypt.hash(password, 10)
return prisma.user.create({
data: {
username,
email,
password: hashedPassword
}
})
} catch (error: any) {
httpLogger.error(`Error registering user: ${error instanceof Error ? error.message : String(error)}`)
return false
}
} }
/** /**
@ -111,6 +124,7 @@ class UserService {
return true return true
} catch (error: any) { } catch (error: any) {
httpLogger.error(`Error sending password reset email: ${error instanceof Error ? error.message : String(error)}`)
return false return false
} }
} }
@ -121,18 +135,23 @@ class UserService {
* @param password * @param password
*/ */
async newPassword(urlToken: string, password: string): Promise<boolean | User> { async newPassword(urlToken: string, password: string): Promise<boolean | User> {
const tokenData = await PasswordResetTokenRepository.getByToken(urlToken) try {
if (!tokenData) { const tokenData = await PasswordResetTokenRepository.getByToken(urlToken)
if (!tokenData) {
return false
}
const hashedPassword = await bcrypt.hash(password, 10)
return prisma.user.update({
where: { id: tokenData.userId },
data: {
password: hashedPassword
}
})
} catch (error: any) {
httpLogger.error(`Error setting new password: ${error instanceof Error ? error.message : String(error)}`)
return false return false
} }
const hashedPassword = await bcrypt.hash(password, 10)
return prisma.user.update({
where: { id: tokenData.userId },
data: {
password: hashedPassword
}
})
} }
} }