Major refractor, cleaning and improvements.

This commit is contained in:
2024-08-24 03:08:43 +02:00
parent e0b376cb83
commit 39f4e79a88
30 changed files with 123 additions and 69 deletions

View File

@ -1,16 +1,23 @@
// 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'
/**
* Socket io jwt auth middleware
* @param socket
* @param next
*/
export async function Authentication(socket: TSocket, next: any) {
if (!socket.request.headers.cookie) {
console.log('No cookie provided')
return next(new Error('Authentication error'))
}
/**
* Parse cookies
*/
const cookies = socket.request.headers.cookie.split('; ').reduce((prev: any, current: any) => {
const [name, value] = current.split('=')
prev[name] = value
@ -19,6 +26,9 @@ export async function Authentication(socket: TSocket, next: any) {
const token = cookies['token']
/**
* Verify token, if valid, set user on socket and continue
*/
if (token) {
verify(token, config.JWT_SECRET, async (err: any, decoded: any) => {
if (err) {