Skip to content

Commit

Permalink
Updated redis calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Dec 12, 2024
1 parent bb5affe commit 62a8b29
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@react-spring/web": "^9.7.3",
"@tanstack/react-query": "^5.28.6",
"@umami/prisma-client": "^0.14.0",
"@umami/redis-client": "^0.21.0",
"@umami/redis-client": "^0.24.0",
"chalk": "^4.1.1",
"chart.js": "^4.4.2",
"chartjs-adapter-date-fns": "^3.0.0",
Expand Down
10 changes: 6 additions & 4 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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());
Expand Down
14 changes: 9 additions & 5 deletions src/lib/load.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import redis from '@umami/redis-client';
import { redisEnabled } from '@umami/redis-client';
import { saveAuth } from 'lib/auth';
import { secret } from 'lib/crypto';
import { useValidate } from 'lib/middleware';
Expand Down Expand Up @@ -49,7 +49,7 @@ export default async (
const user = await getUserByUsername(username, { includePassword: true });

if (user && checkPassword(password, user.password)) {
if (redis.enabled) {
if (redisEnabled) {
const token = await saveAuth({ userId: user.id });

return ok(res, { token, user });
Expand Down
8 changes: 5 additions & 3 deletions src/pages/api/auth/logout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { methodNotAllowed, ok } from 'next-basics';
import redis from '@umami/redis-client';
import { getClient, redisEnabled } from '@umami/redis-client';
import { useAuth } from 'lib/middleware';
import { getAuthToken } from 'lib/auth';
import { NextApiRequest, NextApiResponse } from 'next';
Expand All @@ -8,8 +8,10 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
await useAuth(req, res);

if (req.method === 'POST') {
if (redis.enabled) {
await redis.client.del(getAuthToken(req));
if (redisEnabled) {
const redis = getClient();

await redis.del(getAuthToken(req));
}

return ok(res);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/auth/sso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { NextApiRequestAuth } from 'lib/types';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { badRequest, ok } from 'next-basics';
import redis from '@umami/redis-client';
import { redisEnabled } from '@umami/redis-client';
import { saveAuth } from 'lib/auth';

export default async (req: NextApiRequestAuth, res: NextApiResponse) => {
await useAuth(req, res);

if (redis.enabled && req.auth.user) {
if (redisEnabled && req.auth.user) {
const token = await saveAuth({ userId: req.auth.user.id }, 86400);

return ok(res, { user: req.auth.user, token });
Expand Down
10 changes: 7 additions & 3 deletions src/queries/prisma/website.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prisma, Website } from '@prisma/client';
import redis from '@umami/redis-client';
import { getClient } from '@umami/redis-client';
import prisma from 'lib/prisma';
import { PageResult, PageParams } from 'lib/types';
import WebsiteFindManyArgs = Prisma.WebsiteFindManyArgs;
Expand Down Expand Up @@ -182,7 +182,9 @@ export async function resetWebsite(
}),
]).then(async data => {
if (cloudMode) {
await redis.client.set(`website:${websiteId}`, data[3]);
const redis = getClient();

await redis.set(`website:${websiteId}`, data[3]);
}

return data;
Expand Down Expand Up @@ -225,7 +227,9 @@ export async function deleteWebsite(
}),
]).then(async data => {
if (cloudMode) {
await redis.client.del(`website:${websiteId}`);
const redis = getClient();

await redis.del(`website:${websiteId}`);
}

return data;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3326,10 +3326,10 @@
chalk "^4.1.2"
debug "^4.3.4"

"@umami/redis-client@^0.21.0":
version "0.21.0"
resolved "https://registry.yarnpkg.com/@umami/redis-client/-/redis-client-0.21.0.tgz#96426b28860b8b06fae8825fc2f2d9575b64e863"
integrity sha512-PpdJunvT4sAsVWIeEl+cHU6iSV2r/Df9dof2gdUwSigfD88ACsVs1/BvlWERxk/T93rTgVJWSpLvdw/oMYvkcw==
"@umami/redis-client@^0.24.0":
version "0.24.0"
resolved "https://registry.yarnpkg.com/@umami/redis-client/-/redis-client-0.24.0.tgz#8af489250396be76bc0906766343620589774c4b"
integrity sha512-yUZmC87H5QZKNA6jD9k/7d8WDaXQTDROlpyK7S+V2csD96eAnMNi7JsWAVWx9T/584QKD8DsSIy87PTWq1HNPw==
dependencies:
debug "^4.3.4"
redis "^4.5.1"
Expand Down

0 comments on commit 62a8b29

Please sign in to comment.