Skip to content

Commit

Permalink
fix: domain duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
diced committed Aug 27, 2022
1 parent 2e8bee9 commit bd055d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
18 changes: 3 additions & 15 deletions src/components/pages/Manage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default function Manage() {
const modals = useModals();

const [exports, setExports] = useState([]);
const [domains, setDomains] = useState(user.domains ?? []);
const [file, setFile] = useState<File>(null);
const [fileDataURL, setFileDataURL] = useState(user.avatar ?? null);

Expand Down Expand Up @@ -116,7 +115,7 @@ export default function Manage() {
embedTitle: user.embedTitle ?? '',
embedColor: user.embedColor,
embedSiteName: user.embedSiteName ?? '',
domains: user.domains ?? [],
domains: user.domains.join(','),
},
});

Expand All @@ -143,7 +142,7 @@ export default function Manage() {
embedTitle: cleanEmbedTitle === '' ? null : cleanEmbedTitle,
embedColor: cleanEmbedColor === '' ? null : cleanEmbedColor,
embedSiteName: cleanEmbedSiteName === '' ? null : cleanEmbedSiteName,
domains,
domains: values.domains.split(/\s?,\s?/).map(x => x.trim()).filter(x => x !== ''),
};

const newUser = await useFetch('/api/user', 'PATCH', data);
Expand Down Expand Up @@ -260,18 +259,7 @@ export default function Manage() {
<TextInput id='embedTitle' label='Embed Title' {...form.getInputProps('embedTitle')} />
<ColorInput id='embedColor' label='Embed Color' {...form.getInputProps('embedColor')} />
<TextInput id='embedSiteName' label='Embed Site Name' {...form.getInputProps('embedSiteName')} />
<MultiSelect
id='domains'
label='Domains'
data={domains}
placeholder='Leave blank if you dont want random domain selection.'
creatable
searchable
clearable
getCreateLabel={query => `Add ${query}`}
onCreate={query => setDomains((current) => [...current, query])}
{...form.getInputProps('domains')}
/>
<TextInput id='domains' label='Domains' description='A list of domains separated by commas. These domains will be used to randomly output a domain when uploading. This is optional.' placeholder='https://example.com, https://example2.com' {...form.getInputProps('domains')} />

<Group position='right' mt='md'>
<Button
Expand Down
24 changes: 11 additions & 13 deletions src/pages/api/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
},
});
if (existing && user.username !== req.body.username) {
return res.forbid('Username is already taken');
return res.forbid('username is already taken');
}
await prisma.user.update({
where: { id: user.id },
Expand Down Expand Up @@ -64,27 +64,25 @@ async function handler(req: NextApiReq, res: NextApiRes) {
});

const invalidDomains = [];
const domains = [];

for (const domain of req.body.domains) {
try {
const url = new URL(domain);
url.pathname = '/api/version';
const res = await fetch(url.toString());
if (!res.ok) invalidDomains.push({ domain, reason: 'Got a non OK response' });
else {
const body = await res.json();
if (body?.local !== pkg.version) invalidDomains.push({ domain, reason: 'Version mismatch' });
else await prisma.user.update({
where: { id: user.id },
data: { domains: { push: url.origin } },
});
}
domains.push(url.origin);
} catch (e) {
invalidDomains.push({ domain, reason: e.message });
}
}

if (invalidDomains.length) return res.forbid('Invalid domains', { invalidDomains });
if (invalidDomains.length) return res.forbid('invalid domains', { invalidDomains });

await prisma.user.update({
where: { id: user.id },
data: { domains },
});

return res.json({ domains });
}

const newUser = await prisma.user.findFirst({
Expand Down

0 comments on commit bd055d7

Please sign in to comment.