1
0
forked from noxious/server

use camelcase file names from now on...

This commit is contained in:
2024-08-21 20:55:58 +02:00
parent acc9eaae9e
commit 6b97e7d9cb
55 changed files with 396 additions and 116 deletions

View File

@ -0,0 +1,36 @@
// socket io jwt auth middleware
import { verify } from 'jsonwebtoken'
import { TSocket } from '../utilities/types'
import config from '../utilities/config'
import UserRepository from '../repositories/userRepository'
import { User } from '@prisma/client'
export async function Authentication(socket: TSocket, next: any) {
if (!socket.request.headers.cookie) {
console.log('No cookie provided')
return next(new Error('Authentication error'))
}
const cookies = socket.request.headers.cookie.split('; ').reduce((prev: any, current: any) => {
const [name, value] = current.split('=')
prev[name] = value
return prev
}, {})
const token = cookies['token']
if (token) {
verify(token, config.JWT_SECRET, async (err: any, decoded: any) => {
if (err) {
console.log('err')
return next(new Error('Authentication error'))
}
socket.user = (await UserRepository.getById(decoded.id)) as User
next()
})
} else {
console.log('No token provided')
next(new Error('Authentication error'))
}
}