Update teams features.

This commit is contained in:
Mike Cao 2023-02-01 18:39:54 -08:00
parent 89f2fd601e
commit 656df4f846
23 changed files with 278 additions and 113 deletions

View file

@ -38,11 +38,11 @@ export default async (
if (user && checkPassword(password, user.password)) {
if (redis.enabled) {
const key = `auth:${getRandomChars(32)}`;
const authKey = `auth:${getRandomChars(32)}`;
await redis.set(key, user);
await redis.set(authKey, user);
const token = createSecureToken({ key }, secret());
const token = createSecureToken({ authKey }, secret());
return ok(res, { token, user });
}

View file

@ -4,7 +4,7 @@ import { canCreateTeam } from 'lib/auth';
import { uuid } from 'lib/crypto';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createTeam, getUserTeams } from 'queries';
export interface TeamsRequestBody {
@ -34,13 +34,14 @@ export default async (
const { name } = req.body;
const created = await createTeam({
const team = await createTeam({
id: uuid(),
name,
userId,
accessCode: getRandomChars(16),
});
return ok(res, created);
return ok(res, team);
}
return methodNotAllowed(res);

34
pages/api/teams/join.ts Normal file
View file

@ -0,0 +1,34 @@
import { Team } from '@prisma/client';
import { NextApiRequestQueryBody } from 'lib/types';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, notFound } from 'next-basics';
import { createTeamUser, getTeam } from 'queries';
import { ROLES } from 'lib/constants';
export interface TeamsRequestBody {
accessCode: string;
}
export default async (
req: NextApiRequestQueryBody<any, TeamsRequestBody>,
res: NextApiResponse<Team[] | Team>,
) => {
await useAuth(req, res);
if (req.method === 'POST') {
const { accessCode } = req.body;
const team = await getTeam({ accessCode });
if (!team) {
return notFound(res, 'message.team-not-found');
}
await createTeamUser(req.auth.user.id, team.id, ROLES.teamMember);
return ok(res, team);
}
return methodNotAllowed(res);
};