Colubrina/backend/lib/authorize_socket.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-06-05 22:58:18 +10:00
const { User, Theme, Session } = require("../models")
const parseCookie = (str) =>
str
.split(";")
.map((v) => v.split("="))
.reduce((acc, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim())
return acc
}, {})
module.exports = async function (socket, next) {
try {
const cookies = parseCookie(socket.handshake.headers.cookie)
if (cookies["session"]) {
const session = await Session.findOne({
where: { session: cookies["session"] }
})
if (session) {
const user = await User.findOne({
where: { id: session.userId },
attributes: {
2022-07-31 14:56:43 +10:00
exclude: ["totp", "password", "emailToken"]
2022-06-05 22:58:18 +10:00
},
include: [
{
model: Theme,
as: "themeObject"
}
],
raw: true
})
if (user) {
2022-07-29 19:20:19 +10:00
if (user.banned) {
socket.user = {
id: null,
username: "Not Authenticated"
}
next()
} else {
socket.user = user
next()
}
2022-06-05 22:58:18 +10:00
}
} else {
2022-07-29 01:12:29 +10:00
socket.user = {
id: null,
username: "Not Authenticated"
}
next()
2022-06-05 22:58:18 +10:00
}
} else {
2022-07-29 01:12:29 +10:00
socket.user = {
id: null,
username: "Not Authenticated"
}
next()
2022-06-05 22:58:18 +10:00
}
} catch (error) {
2022-07-29 01:12:29 +10:00
socket.user = {
id: null,
username: "Not Authenticated"
}
next()
2022-06-05 22:58:18 +10:00
}
}