Major refractor, cleaning and improvements.
This commit is contained in:
@ -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) {
|
||||
|
Reference in New Issue
Block a user