mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-25 02:36:20 +01:00
39 lines
883 B
JavaScript
39 lines
883 B
JavaScript
import { createMiddleware, unauthorized, badRequest, serverError } from 'next-basics';
|
|
import cors from 'cors';
|
|
import { getSession } from './session';
|
|
import { getAuthToken, getShareToken } from './auth';
|
|
|
|
export const useCors = createMiddleware(cors());
|
|
|
|
export const useSession = createMiddleware(async (req, res, next) => {
|
|
let session;
|
|
|
|
try {
|
|
session = await getSession(req);
|
|
} catch (e) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(e);
|
|
|
|
return serverError(res, e.message);
|
|
}
|
|
|
|
if (!session) {
|
|
return badRequest(res);
|
|
}
|
|
|
|
req.session = session;
|
|
next();
|
|
});
|
|
|
|
export const useAuth = createMiddleware(async (req, res, next) => {
|
|
const token = await getAuthToken(req);
|
|
const shareToken = await getShareToken(req);
|
|
|
|
if (!token && !shareToken) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
req.auth = { ...token, shareToken };
|
|
next();
|
|
});
|