Added socket.io

This commit is contained in:
2024-07-31 19:01:55 -04:00
parent fc66225cf5
commit 441e372987
23 changed files with 551 additions and 151 deletions
+17 -7
View File
@@ -1,17 +1,27 @@
'use strict';
const {Auth} = require('../models/auth');
const { Auth } = require('../controller/auth');
async function auth(req, res, next){
try{
let user = await Auth.checkToken({token: req.header('auth-token')});
if(user.username){
req.user = user;
return next();
}
req.token = await Auth.checkToken(req.header('auth-token'));
req.user = await req.token.getUser();
return next();
}catch(error){
next(error);
}
}
module.exports = {auth};
async function authIO(socket, next){
try{
let token = await Auth.checkToken(socket.handshake.auth.token || 0);
socket.user = await token.getUser();
console.log('socket is good!')
next();
}catch(error){
console.log('reject for', socket.handshake.auth.token)
next(error);
}
}
module.exports = {auth, authIO};