forked from noxious/server
30 lines
808 B
TypeScript
30 lines
808 B
TypeScript
import { Server } from 'socket.io'
|
|
import { TSocket } from '../utilities/types'
|
|
import { gameLogger } from '../utilities/logger'
|
|
import UserRepository from '../repositories/userRepository'
|
|
|
|
export default class LoginEvent {
|
|
constructor(
|
|
private readonly io: Server,
|
|
private readonly socket: TSocket
|
|
) {}
|
|
|
|
public listen(): void {
|
|
this.socket.on('login', this.handleLogin.bind(this))
|
|
}
|
|
|
|
private handleLogin(): void {
|
|
try {
|
|
if (!this.socket.userId) {
|
|
gameLogger.warn('Login attempt without user data')
|
|
return
|
|
}
|
|
|
|
this.socket.emit('logged_in', { user: UserRepository.getById(this.socket.userId) })
|
|
gameLogger.info(`User logged in: ${this.socket.userId}`)
|
|
} catch (error: any) {
|
|
gameLogger.error('login error', error.message)
|
|
}
|
|
}
|
|
}
|