forked from noxious/server
Added extra logging to HTTP endpoints
This commit is contained in:
parent
bf75ad001b
commit
6a76c4797a
@ -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,6 +19,7 @@ class UserService {
|
|||||||
* @param password
|
* @param password
|
||||||
*/
|
*/
|
||||||
async login(username: string, password: string): Promise<boolean | User> {
|
async login(username: string, password: string): Promise<boolean | User> {
|
||||||
|
try {
|
||||||
const user = await UserRepository.getByUsername(username)
|
const user = await UserRepository.getByUsername(username)
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return false
|
return false
|
||||||
@ -25,10 +27,15 @@ class UserService {
|
|||||||
|
|
||||||
const passwordMatch = await bcrypt.compare(password, user.password)
|
const passwordMatch = await bcrypt.compare(password, user.password)
|
||||||
if (!passwordMatch) {
|
if (!passwordMatch) {
|
||||||
|
httpLogger.error(`Failed to login user: ${username}`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
} catch (error: any) {
|
||||||
|
httpLogger.error(`Error logging in user: ${error instanceof Error ? error.message : String(error)}`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,6 +45,7 @@ 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> {
|
||||||
|
try {
|
||||||
const user = await UserRepository.getByUsername(username)
|
const user = await UserRepository.getByUsername(username)
|
||||||
if (user) {
|
if (user) {
|
||||||
return false
|
return false
|
||||||
@ -45,6 +53,7 @@ class UserService {
|
|||||||
|
|
||||||
const userByEmail = await UserRepository.getByEmail(email)
|
const userByEmail = await UserRepository.getByEmail(email)
|
||||||
if (userByEmail) {
|
if (userByEmail) {
|
||||||
|
httpLogger.error(`User already exists: ${email}`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,6 +65,10 @@ class UserService {
|
|||||||
password: hashedPassword
|
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,6 +135,7 @@ class UserService {
|
|||||||
* @param password
|
* @param password
|
||||||
*/
|
*/
|
||||||
async newPassword(urlToken: string, password: string): Promise<boolean | User> {
|
async newPassword(urlToken: string, password: string): Promise<boolean | User> {
|
||||||
|
try {
|
||||||
const tokenData = await PasswordResetTokenRepository.getByToken(urlToken)
|
const tokenData = await PasswordResetTokenRepository.getByToken(urlToken)
|
||||||
if (!tokenData) {
|
if (!tokenData) {
|
||||||
return false
|
return false
|
||||||
@ -133,6 +148,10 @@ class UserService {
|
|||||||
password: hashedPassword
|
password: hashedPassword
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} catch (error: any) {
|
||||||
|
httpLogger.error(`Error setting new password: ${error instanceof Error ? error.message : String(error)}`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user