Feat/um 114 roles and permissions (#1683)

* Auth checkpoint.

* Merge branch 'dev' into feat/um-114-roles-and-permissions
This commit is contained in:
Brian Cao 2022-12-01 20:53:37 -08:00 committed by GitHub
parent a4e80ca3e5
commit 06bebadbb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 331 additions and 482 deletions

View file

@ -1,9 +1,10 @@
import { Team } from '@prisma/client';
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
import { canCreateTeam } from 'lib/auth';
import { uuid } from 'lib/crypto';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok } from 'next-basics';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createTeam, getTeam, getTeamsByUserId } from 'queries';
export interface TeamsRequestBody {
name: string;
@ -17,16 +18,20 @@ export default async (
await useAuth(req, res);
const {
user: { id },
user: { id: userId },
} = req.auth;
if (req.method === 'GET') {
const teams = await getTeamsByUserId(id);
const teams = await getTeamsByUserId(userId);
return ok(res, teams);
}
if (req.method === 'POST') {
if (await canCreateTeam(userId)) {
return unauthorized(res);
}
const { name } = req.body;
const team = await getTeam({ name });
@ -36,7 +41,7 @@ export default async (
}
const created = await createTeam({
id: id || uuid(),
id: uuid(),
name,
});