2021-07-06 19:10:04 +02:00
|
|
|
const redis = require("../redis/authRedis")
|
|
|
|
|
|
|
|
const EXPIRY_SECONDS = 86400
|
|
|
|
|
|
|
|
async function getSessionsForUser(userId) {
|
|
|
|
const client = await redis.getSessionClient()
|
2021-07-08 00:29:19 +02:00
|
|
|
const sessions = await client.scan(userId)
|
|
|
|
return sessions.map(session => session.value)
|
2021-07-06 19:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeSessionID(userId, sessionId) {
|
|
|
|
return `${userId}/${sessionId}`
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
exports.createASession = async (userId, session) => {
|
2021-07-06 19:10:04 +02:00
|
|
|
const client = await redis.getSessionClient()
|
2021-08-05 10:59:08 +02:00
|
|
|
const sessionId = session.sessionId
|
|
|
|
session = {
|
2021-07-08 00:30:14 +02:00
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
lastAccessedAt: new Date().toISOString(),
|
2021-08-05 10:59:08 +02:00
|
|
|
...session,
|
2021-07-08 00:29:19 +02:00
|
|
|
userId,
|
|
|
|
}
|
|
|
|
await client.store(makeSessionID(userId, sessionId), session, EXPIRY_SECONDS)
|
2021-07-06 19:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.invalidateSessions = async (userId, sessionId = null) => {
|
|
|
|
let sessions = []
|
|
|
|
if (sessionId) {
|
|
|
|
sessions.push({ key: makeSessionID(userId, sessionId) })
|
|
|
|
} else {
|
|
|
|
sessions = await getSessionsForUser(userId)
|
2021-09-21 13:27:53 +02:00
|
|
|
sessions.forEach(
|
|
|
|
session =>
|
|
|
|
(session.key = makeSessionID(session.userId, session.sessionId))
|
|
|
|
)
|
2021-07-06 19:10:04 +02:00
|
|
|
}
|
|
|
|
const client = await redis.getSessionClient()
|
|
|
|
const promises = []
|
|
|
|
for (let session of sessions) {
|
|
|
|
promises.push(client.delete(session.key))
|
|
|
|
}
|
|
|
|
await Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
2021-07-08 00:29:19 +02:00
|
|
|
exports.updateSessionTTL = async session => {
|
2021-07-06 19:10:04 +02:00
|
|
|
const client = await redis.getSessionClient()
|
2021-07-08 00:29:19 +02:00
|
|
|
const key = makeSessionID(session.userId, session.sessionId)
|
2021-07-08 00:30:14 +02:00
|
|
|
session.lastAccessedAt = new Date().toISOString()
|
2021-07-08 00:29:19 +02:00
|
|
|
await client.store(key, session, EXPIRY_SECONDS)
|
2021-07-06 19:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.endSession = async (userId, sessionId) => {
|
|
|
|
const client = await redis.getSessionClient()
|
|
|
|
await client.delete(makeSessionID(userId, sessionId))
|
|
|
|
}
|
|
|
|
|
2021-07-08 00:29:19 +02:00
|
|
|
exports.getUserSessions = getSessionsForUser
|
|
|
|
|
2021-07-06 19:10:04 +02:00
|
|
|
exports.getSession = async (userId, sessionId) => {
|
|
|
|
try {
|
|
|
|
const client = await redis.getSessionClient()
|
|
|
|
return client.get(makeSessionID(userId, sessionId))
|
|
|
|
} catch (err) {
|
|
|
|
// if can't get session don't error, just don't return anything
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getAllSessions = async () => {
|
|
|
|
const client = await redis.getSessionClient()
|
2021-07-08 00:29:19 +02:00
|
|
|
const sessions = await client.scan()
|
|
|
|
return sessions.map(session => session.value)
|
2021-07-06 19:10:04 +02:00
|
|
|
}
|