Refactor auth logic.

This commit is contained in:
Mike Cao 2022-10-11 21:48:33 -07:00
parent edd1751b81
commit 5e2d23f18f
10 changed files with 40 additions and 30 deletions

View file

@ -9,10 +9,10 @@ export default async (req, res) => {
const website = await getWebsiteByShareId(id);
if (website) {
const websiteId = website.websiteId;
const token = createToken({ websiteId: websiteId }, secret());
const { websiteId, websiteUuid } = website;
const token = createToken({ websiteId, websiteUuid }, secret());
return ok(res, { websiteId, token });
return ok(res, { websiteId, websiteUuid, token });
}
return notFound(res);

View file

@ -5,6 +5,10 @@ import { useAuth, useCors } from 'lib/middleware';
import { validate } from 'uuid';
export default async (req, res) => {
await useAuth(req, res);
const { isAdmin, userId, accountUuid } = req.auth;
const { id } = req.query;
const websiteId = +id;
@ -23,9 +27,6 @@ export default async (req, res) => {
}
if (req.method === 'POST') {
await useAuth(req, res);
const { isAdmin: currentUserIsAdmin, userId: currentUserId, accountUuid } = req.auth;
const { name, domain, owner, enable_share_url } = req.body;
let account;
@ -37,7 +38,7 @@ export default async (req, res) => {
const shareId = enable_share_url ? website.shareId || getRandomChars(8) : null;
if (website.userId !== currentUserId && !currentUserIsAdmin) {
if (website.userId !== userId && !isAdmin) {
return unauthorized(res);
}