N
Nuxt4w ago
Dracula

in defineWebSocketHandler how to allow authenticated users connecting?

what i seen most of the time in upgrade you can stop from connecting lets say if user has cookie. thanks for the help.
upgrade(req) {
console.log(this)
let session = '';
const cookies = req.headers.cookie.split(';') as string[];
cookies.forEach(el => {
if(el.includes('session=')) session = el.split('session=')[1].trim();
})

if(session == '') {
// socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
// socket.destroy();
return;
}

return {headers: {}};
},
upgrade(req) {
console.log(this)
let session = '';
const cookies = req.headers.cookie.split(';') as string[];
cookies.forEach(el => {
if(el.includes('session=')) session = el.split('session=')[1].trim();
})

if(session == '') {
// socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
// socket.destroy();
return;
}

return {headers: {}};
},
0 Replies
No replies yetBe the first to reply to this messageJoin