2022-06-05 22:58:18 +10:00
|
|
|
const { User, Theme, Session } = require("../models")
|
2022-08-03 19:11:19 +10:00
|
|
|
const { Op } = require("sequelize")
|
2022-06-05 22:58:18 +10:00
|
|
|
|
|
|
|
module.exports = async function (socket, next) {
|
|
|
|
try {
|
2022-08-03 19:11:19 +10:00
|
|
|
const token = socket.handshake.auth.token
|
|
|
|
const session = await Session.findOne({
|
|
|
|
where: {
|
|
|
|
session: token,
|
|
|
|
expiredAt: {
|
|
|
|
[Op.gt]: new Date()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (session) {
|
|
|
|
const user = await User.findOne({
|
|
|
|
where: { id: session.userId },
|
|
|
|
attributes: {
|
|
|
|
exclude: ["totp", "compassSession", "password"]
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Theme,
|
|
|
|
as: "themeObject"
|
|
|
|
}
|
|
|
|
]
|
2022-06-05 22:58:18 +10:00
|
|
|
})
|
2022-08-03 19:11:19 +10:00
|
|
|
if (user) {
|
|
|
|
await user.update({
|
|
|
|
lastSeenAt: new Date().toISOString()
|
2022-06-05 22:58:18 +10:00
|
|
|
})
|
2022-08-03 19:11:19 +10:00
|
|
|
socket.user = user
|
2022-07-29 01:12:29 +10:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|