mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 12:47:13 +01:00
Fix Website auth.
This commit is contained in:
parent
1af93a17a3
commit
e28ee6597a
23 changed files with 108 additions and 105 deletions
91
lib/cache.ts
Normal file
91
lib/cache.ts
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import { getWebsite, getUser, getSession } from '../queries';
|
||||
import redis, { DELETED } from 'lib/redis';
|
||||
import { Role, Team, TeamUser, User, UserRole, UserWebsite, Website } from '@prisma/client';
|
||||
|
||||
async function fetchObject(key, query) {
|
||||
const obj = await redis.get(key);
|
||||
|
||||
if (!obj) {
|
||||
return query().then(async data => {
|
||||
if (data) {
|
||||
await redis.set(key, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
async function storeObject(key, data) {
|
||||
return redis.set(key, data);
|
||||
}
|
||||
|
||||
async function deleteObject(key) {
|
||||
return redis.set(key, DELETED);
|
||||
}
|
||||
|
||||
async function fetchWebsite(id) {
|
||||
return fetchObject(`website:${id}`, () => getWebsite({ id }));
|
||||
}
|
||||
|
||||
async function storeWebsite(data) {
|
||||
const { id } = data;
|
||||
const key = `website:${id}`;
|
||||
|
||||
return storeObject(key, data);
|
||||
}
|
||||
|
||||
async function deleteWebsite(id) {
|
||||
return deleteObject(`website:${id}`);
|
||||
}
|
||||
|
||||
async function fetchUser(id): Promise<
|
||||
User & {
|
||||
userRole?: (UserRole & { role: Role })[];
|
||||
teamUser?: (TeamUser & { team: Team })[];
|
||||
userWebsite?: (UserWebsite & { website: Website })[];
|
||||
}
|
||||
> {
|
||||
return fetchObject(`user:${id}`, () => getUser({ id }, true));
|
||||
}
|
||||
|
||||
async function storeUser(data) {
|
||||
const { id } = data;
|
||||
const key = `user:${id}`;
|
||||
|
||||
return storeObject(key, data);
|
||||
}
|
||||
|
||||
async function deleteUser(id) {
|
||||
return deleteObject(`user:${id}`);
|
||||
}
|
||||
|
||||
async function fetchSession(id) {
|
||||
return fetchObject(`session:${id}`, () => getSession({ id }));
|
||||
}
|
||||
|
||||
async function storeSession(data) {
|
||||
const { id } = data;
|
||||
const key = `session:${id}`;
|
||||
|
||||
return storeObject(key, data);
|
||||
}
|
||||
|
||||
async function deleteSession(id) {
|
||||
return deleteObject(`session:${id}`);
|
||||
}
|
||||
|
||||
export default {
|
||||
fetchWebsite,
|
||||
storeWebsite,
|
||||
deleteWebsite,
|
||||
fetchUser,
|
||||
storeUser,
|
||||
deleteUser,
|
||||
fetchSession,
|
||||
storeSession,
|
||||
deleteSession,
|
||||
enabled: redis.enabled,
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue