Skip to content

Commit

Permalink
Fixed register route for api
Browse files Browse the repository at this point in the history
  • Loading branch information
keirim committed Oct 4, 2024
1 parent 813fcab commit cc24427
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/pages/api/auth/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { jsonUserReplacer } from 'lib/utils/client';
import { extname } from 'path';

const logger = Logger.get('user');
const MAX_USERNAME_LENGTH = 12;

async function handler(req: NextApiReq, res: NextApiRes) {
const user = await req.user();
Expand All @@ -26,34 +27,32 @@ async function handler(req: NextApiReq, res: NextApiRes) {
code?: string;
};

if (!username) badRequest = true;
if (!password) badRequest = true;
if (!username || !password) return res.badRequest('Bad Username/Password');

// Validate username length
if (username.length > MAX_USERNAME_LENGTH) {
return res.badRequest(`Username cannot exceed ${MAX_USERNAME_LENGTH} characters`);
}

const existing = await prisma.user.findFirst({
where: {
username,
},
select: {
username: true,
},
where: { username },
select: { username: true },
});

if (existing) badRequest = true;

if (badRequest) return res.badRequest('Bad Username/Password');
if (existing) return res.badRequest('Bad Username/Password');

if (code) {
if (config.features.invites) {
const invite = await prisma.invite.findUnique({
where: {
code,
},
where: { code },
});

if (!invite || invite?.used) return res.badRequest('Bad invite');
usedInvite = true;
} else return res.badRequest('Bad Username/Password');
} else if (config.features.invites && !user?.administrator) return res.badRequest('Bad invite');
} else if (config.features.invites && !user?.administrator) {
return res.badRequest('Bad invite');
}

const hashed = await hashPassword(password);

Expand Down Expand Up @@ -85,7 +84,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
});

logger.debug(
`registered user${usedInvite ? ' via invite ' + code : ''} ${JSON.stringify(newUser, jsonUserReplacer)}`,
`registered user${usedInvite ? ' via invite ' + code : ''} ${JSON.stringify(newUser, jsonUserReplacer)}`
);

delete newUser.password;
Expand Down

0 comments on commit cc24427

Please sign in to comment.