Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added max length to registration route #619

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions src/pages/api/auth/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ 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();
let badRequest,
usedInvite = false;
let usedInvite = false;

if (!config.features.user_registration && !config.features.invites && !user?.administrator)
return res.badRequest('This endpoint is unavailable due to current configurations');
Expand All @@ -26,34 +26,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
16 changes: 15 additions & 1 deletion src/pages/auth/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default function Register({ code = undefined, title, user_registration })
const [verifyPasswordError, setVerifyPasswordError] = useState('');
const [strength, setStrength] = useState(0);

const MAX_USERNAME_LENGTH = 16;

const setUser = useSetRecoilState(userSelector);
const router = useRouter();

Expand All @@ -30,6 +32,11 @@ export default function Register({ code = undefined, title, user_registration })
const checkUsername = async () => {
setUsername(username.trim());

if (username.length > MAX_USERNAME_LENGTH) {
setUsernameError(`Username cannot exceed ${MAX_USERNAME_LENGTH} characters`);
return;
}

setUsernameError('');

const res = await useFetch('/api/user/check', 'POST', { code, username });
Expand Down Expand Up @@ -101,7 +108,14 @@ export default function Register({ code = undefined, title, user_registration })
<TextInput
label='Username'
value={username}
onChange={(e) => setUsername(e.target.value)}
onChange={(e) => {
setUsername(e.target.value);
if (e.target.value.length > MAX_USERNAME_LENGTH) {
setUsernameError(`Username cannot exceed ${MAX_USERNAME_LENGTH} characters`);
} else {
setUsernameError('');
}
}}
error={usernameError}
onBlur={() => checkUsername()}
/>
Expand Down
Loading