fix(sync): allow last expired jwt wip
Some checks failed
/ build (map[dockerfile:./services/api/Dockerfile name:api]) (push) Successful in 3m4s
/ build (map[dockerfile:./services/app/Dockerfile name:app]) (push) Successful in 2m9s
/ build (map[dockerfile:./services/files/Dockerfile name:files]) (push) Successful in 2m9s
/ build (map[dockerfile:./services/hasura/Dockerfile name:hasura]) (push) Successful in 3m26s
/ build (map[dockerfile:./services/tasks/Dockerfile name:tasks]) (push) Successful in 3m6s
/ build (map[dockerfile:./services/watchers/Dockerfile name:watchers]) (push) Has started running
/ build (map[dockerfile:./services/web/Dockerfile name:web]) (push) Successful in 3m11s
/ deploy (push) Has been cancelled
Some checks failed
/ build (map[dockerfile:./services/api/Dockerfile name:api]) (push) Successful in 3m4s
/ build (map[dockerfile:./services/app/Dockerfile name:app]) (push) Successful in 2m9s
/ build (map[dockerfile:./services/files/Dockerfile name:files]) (push) Successful in 2m9s
/ build (map[dockerfile:./services/hasura/Dockerfile name:hasura]) (push) Successful in 3m26s
/ build (map[dockerfile:./services/tasks/Dockerfile name:tasks]) (push) Successful in 3m6s
/ build (map[dockerfile:./services/watchers/Dockerfile name:watchers]) (push) Has started running
/ build (map[dockerfile:./services/web/Dockerfile name:web]) (push) Successful in 3m11s
/ deploy (push) Has been cancelled
This commit is contained in:
parent
edee8d6bc4
commit
c036839233
2 changed files with 31 additions and 2 deletions
|
@ -72,7 +72,26 @@ module.exports = async function ({ services: { sortRolesByLevel, signJwt } }) {
|
||||||
`
|
`
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
if (!deviceId && deviceUuid) {
|
||||||
|
// First check if a device with this UUID already exists for this user
|
||||||
|
const existingDevice = await sql`
|
||||||
|
SELECT
|
||||||
|
id
|
||||||
|
FROM
|
||||||
|
"device"
|
||||||
|
WHERE
|
||||||
|
"user_id" = ${userId}
|
||||||
|
AND "uuid" = ${deviceUuid}
|
||||||
|
LIMIT 1
|
||||||
|
`
|
||||||
|
|
||||||
|
if (existingDevice.length > 0) {
|
||||||
|
deviceId = existingDevice[0].id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!deviceId) {
|
if (!deviceId) {
|
||||||
|
// Only create new device if UUID doesn't exist
|
||||||
;[{ id: deviceId }] = await sql`
|
;[{ id: deviceId }] = await sql`
|
||||||
INSERT INTO "device" ("user_id", "phone_model", "uuid")
|
INSERT INTO "device" ("user_id", "phone_model", "uuid")
|
||||||
VALUES (${userId}, ${phoneModel}, ${deviceUuid})
|
VALUES (${userId}, ${phoneModel}, ${deviceUuid})
|
||||||
|
@ -81,6 +100,16 @@ module.exports = async function ({ services: { sortRolesByLevel, signJwt } }) {
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the auth_token to reference this device
|
||||||
|
await sql`
|
||||||
|
UPDATE
|
||||||
|
"auth_token"
|
||||||
|
SET
|
||||||
|
"device_id" = ${deviceId}
|
||||||
|
WHERE
|
||||||
|
"auth_token" = ${authToken}
|
||||||
|
`
|
||||||
|
|
||||||
roles = (
|
roles = (
|
||||||
await sql`
|
await sql`
|
||||||
SELECT
|
SELECT
|
||||||
|
|
|
@ -34,12 +34,12 @@ module.exports = function () {
|
||||||
const deviceExpKey = `device:${deviceId}:last_exp`
|
const deviceExpKey = `device:${deviceId}:last_exp`
|
||||||
const storedLastExp = await redis.get(deviceExpKey)
|
const storedLastExp = await redis.get(deviceExpKey)
|
||||||
|
|
||||||
if (storedLastExp && session.exp <= parseInt(storedLastExp, 10)) {
|
if (storedLastExp && session.exp < parseInt(storedLastExp, 10)) {
|
||||||
throw httpError(401, "not the latest jwt")
|
throw httpError(401, "not the latest jwt")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the new expiration date
|
// Store the new expiration date
|
||||||
await redis.set(deviceExpKey, session.exp, "EX", 30 * 24 * 60 * 60) // 30 days TTL
|
await redis.set(deviceExpKey, session.exp)
|
||||||
}
|
}
|
||||||
|
|
||||||
const { userId } = session
|
const { userId } = session
|
||||||
|
|
Loading…
Add table
Reference in a new issue