Updated socket middleware
This commit is contained in:
parent
6e58de8840
commit
1223ae42ef
@ -1,12 +1,13 @@
|
|||||||
import { verify } from 'jsonwebtoken'
|
import { verify } from 'jsonwebtoken'
|
||||||
|
|
||||||
import config from '#application/config'
|
import config from '#application/config'
|
||||||
import { gameLogger } from '#application/logger'
|
import Logger, { LoggerType } from '#application/logger'
|
||||||
import { TSocket } from '#application/types'
|
import { TSocket } from '#application/types'
|
||||||
|
|
||||||
class SocketAuthenticator {
|
class SocketAuthenticator {
|
||||||
private socket: TSocket
|
private socket: TSocket
|
||||||
private readonly next: any
|
private readonly next: any
|
||||||
|
private readonly logger = Logger.type(LoggerType.APP)
|
||||||
|
|
||||||
constructor(socket: TSocket, next: any) {
|
constructor(socket: TSocket, next: any) {
|
||||||
this.socket = socket
|
this.socket = socket
|
||||||
@ -15,14 +16,14 @@ class SocketAuthenticator {
|
|||||||
|
|
||||||
public async authenticate(): Promise<void> {
|
public async authenticate(): Promise<void> {
|
||||||
if (!this.socket.request.headers.cookie) {
|
if (!this.socket.request.headers.cookie) {
|
||||||
gameLogger.warn('No cookie provided')
|
this.logger.warn('No cookie provided')
|
||||||
return this.next(new Error('Authentication error'))
|
return this.next(new Error('Authentication error'))
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = this.parseCookies()['token']
|
const token = this.parseCookies()['token']
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
gameLogger.warn('No token provided')
|
this.logger.warn('No token provided')
|
||||||
return this.next(new Error('Authentication error'))
|
return this.next(new Error('Authentication error'))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ class SocketAuthenticator {
|
|||||||
private verifyToken(token: string): void {
|
private verifyToken(token: string): void {
|
||||||
verify(token, config.JWT_SECRET, (err: any, decoded: any) => {
|
verify(token, config.JWT_SECRET, (err: any, decoded: any) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
gameLogger.error('Invalid token')
|
this.logger.error('Invalid token')
|
||||||
return this.next(new Error('Authentication error'))
|
return this.next(new Error('Authentication error'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user