Updated redis calls.

This commit is contained in:
Mike Cao 2024-12-11 19:43:23 -08:00
parent bb5affe29a
commit 62a8b29453
9 changed files with 41 additions and 27 deletions

View file

@ -1,7 +1,7 @@
import { Report } from '@prisma/client';
import redis from '@umami/redis-client';
import { getClient } from '@umami/redis-client';
import debug from 'debug';
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER, ROLES } from 'lib/constants';
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
import { secret } from 'lib/crypto';
import { NextApiRequest } from 'next';
import { createSecureToken, ensureArray, getRandomChars, parseToken } from 'next-basics';
@ -14,10 +14,12 @@ const cloudMode = process.env.CLOUD_MODE;
export async function saveAuth(data: any, expire = 0) {
const authKey = `auth:${getRandomChars(32)}`;
await redis.client.set(authKey, data);
const redis = getClient();
await redis.set(authKey, data);
if (expire) {
await redis.client.expire(authKey, expire);
await redis.expire(authKey, expire);
}
return createSecureToken({ authKey }, secret());

View file

@ -1,12 +1,14 @@
import { getWebsiteSession, getWebsite } from 'queries';
import { Website, Session } from '@prisma/client';
import redis from '@umami/redis-client';
import { getClient, redisEnabled } from '@umami/redis-client';
export async function fetchWebsite(websiteId: string): Promise<Website> {
let website = null;
if (redis.enabled) {
website = await redis.client.fetch(`website:${websiteId}`, () => getWebsite(websiteId), 86400);
if (redisEnabled) {
const redis = getClient();
website = await redis.fetch(`website:${websiteId}`, () => getWebsite(websiteId), 86400);
} else {
website = await getWebsite(websiteId);
}
@ -21,8 +23,10 @@ export async function fetchWebsite(websiteId: string): Promise<Website> {
export async function fetchSession(websiteId: string, sessionId: string): Promise<Session> {
let session = null;
if (redis.enabled) {
session = await redis.client.fetch(
if (redisEnabled) {
const redis = getClient();
session = await redis.fetch(
`session:${sessionId}`,
() => getWebsiteSession(websiteId, sessionId),
86400,

View file

@ -1,6 +1,6 @@
import cors from 'cors';
import debug from 'debug';
import redis from '@umami/redis-client';
import { getClient, redisEnabled } from '@umami/redis-client';
import { getAuthToken, parseShareToken } from 'lib/auth';
import { ROLES } from 'lib/constants';
import { secret } from 'lib/crypto';
@ -54,8 +54,10 @@ export const useAuth = createMiddleware(async (req, res, next) => {
if (userId) {
user = await getUser(userId);
} else if (redis.enabled && authKey) {
const key = await redis.client.get(authKey);
} else if (redisEnabled && authKey) {
const redis = getClient();
const key = await redis.get(authKey);
if (key?.userId) {
user = await getUser(key.userId);