1
0
forked from noxious/server

Updated socket middleware

This commit is contained in:
Dennis Postma 2024-12-28 22:19:08 +01:00
parent 6e58de8840
commit 1223ae42ef

View File

@ -1,12 +1,13 @@
import { verify } from 'jsonwebtoken'
import config from '#application/config'
import { gameLogger } from '#application/logger'
import Logger, { LoggerType } from '#application/logger'
import { TSocket } from '#application/types'
class SocketAuthenticator {
private socket: TSocket
private readonly next: any
private readonly logger = Logger.type(LoggerType.APP)
constructor(socket: TSocket, next: any) {
this.socket = socket
@ -15,14 +16,14 @@ class SocketAuthenticator {
public async authenticate(): Promise<void> {
if (!this.socket.request.headers.cookie) {
gameLogger.warn('No cookie provided')
this.logger.warn('No cookie provided')
return this.next(new Error('Authentication error'))
}
const token = this.parseCookies()['token']
if (!token) {
gameLogger.warn('No token provided')
this.logger.warn('No token provided')
return this.next(new Error('Authentication error'))
}
@ -40,7 +41,7 @@ class SocketAuthenticator {
private verifyToken(token: string): void {
verify(token, config.JWT_SECRET, (err: any, decoded: any) => {
if (err) {
gameLogger.error('Invalid token')
this.logger.error('Invalid token')
return this.next(new Error('Authentication error'))
}