Refactored useApi usage.

This commit is contained in:
Mike Cao 2022-12-27 20:20:44 -08:00
parent 561cde6e7e
commit cad0b73e42
26 changed files with 67 additions and 85 deletions

View file

@ -1,18 +0,0 @@
export function chunk(arr, size) {
const chunks = [];
let index = 0;
while (index < arr.length) {
chunks.push(arr.slice(index, size + index));
index += size;
}
return chunks;
}
export function sortArrayByMap(arr, map = [], key) {
if (!arr) return [];
if (map.length === 0) return arr;
return map.map(id => arr.find(item => item[key] === id));
}

View file

@ -1,14 +1,14 @@
import { getItem, setItem, removeItem } from 'next-basics';
import { AUTH_TOKEN } from './constants';
export function getAuthToken() {
export function getClientAuthToken() {
return getItem(AUTH_TOKEN);
}
export function setAuthToken(token) {
export function setClientAuthToken(token) {
setItem(AUTH_TOKEN, token);
}
export function removeAuthToken() {
export function removeClientAuthToken() {
removeItem(AUTH_TOKEN);
}

View file

@ -21,7 +21,7 @@ export const useSession = createMiddleware(async (req, res, next) => {
return badRequest(res);
}
req.session = session;
(req as any).session = session;
next();
});
@ -50,6 +50,6 @@ export const useAuth = createMiddleware(async (req, res, next) => {
user.isAdmin = user.role === ROLES.admin;
}
req.auth = { user, token, shareToken, key };
(req as any).auth = { user, token, shareToken, key };
next();
});

View file

@ -3,7 +3,7 @@ import { validate } from 'uuid';
import { secret, uuid } from 'lib/crypto';
import cache from 'lib/cache';
import clickhouse from 'lib/clickhouse';
import { getClientInfo, getJsonBody } from 'lib/request';
import { getClientInfo, getJsonBody } from 'lib/detect';
import { createSession, getSession, getWebsite } from 'queries';
export async function findSession(req) {