Device info expires in 10 mins

This commit is contained in:
Hongbo Wu 2022-10-11 19:03:33 +08:00
parent 2210a38982
commit 2ba9247a98

View file

@ -105,6 +105,25 @@ export const createApp = (): {
app.use('/api/', apiLimiter)
}
// set user device from request header to Redis
app.use('/api/', async (req, res, next) => {
const device = req.header('X-Device')
const token =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
req.header('Authorization') || (req.cookies['auth'] as string | undefined)
if (device && token) {
const key = `device:${token}`
if (!(await redisClient.exists(key))) {
await redisClient.set(key, device, {
EX: 600, // 10 minutes
NX: true,
})
}
}
next()
})
// respond healthy to auto-scaler.
app.get('/_ah/health', (req, res) => res.sendStatus(200))
@ -134,18 +153,6 @@ export const createApp = (): {
// The error handler must be before any other error middleware and after all routes
app.use(Sentry.Handlers.errorHandler())
// set user device from request header to Redis
app.use('/api/', async (req, res, next) => {
const device = req.header('X-Device')
const token =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
req.header('Authorization') || (req.cookies['auth'] as string | undefined)
if (device && token) {
await redisClient.set(`device:${token}`, device)
}
next()
})
const apollo = makeApolloServer()
const httpServer = createServer(app)