Skip to content

Commit

Permalink
Refactor user registration API to check for existing username or email
Browse files Browse the repository at this point in the history
  • Loading branch information
Adammatthiesen committed Dec 2, 2024
1 parent 3376ccb commit 9472135
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
24 changes: 9 additions & 15 deletions packages/studiocms_auth/src/routes/api/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,15 @@ export const POST: APIRoute = async (context: APIContext): Promise<Response> =>

if (!checkemail.success) return badFormDataEntry('Invalid email', checkemail.error.message);

// Check if the username/email is already used
const existingUsername = await db
.select()
.from(tsUsers)
.where(eq(tsUsers.username, username))
.get();

const existingEmail = await db
.select()
.from(tsUsers)
.where(eq(tsUsers.email, checkemail.data))
.get();

if (existingUsername) return badFormDataEntry('Invalid username', 'Username is already in use');
if (existingEmail) return badFormDataEntry('Invalid email', 'Email is already in use');
// Check if the username or email is already used by another user
const [usernameSearch, emailSearch] = await db.batch([
db.select().from(tsUsers).where(eq(tsUsers.username, username)),
db.select().from(tsUsers).where(eq(tsUsers.email, checkemail.data)),
]);

if (usernameSearch.length > 0)
return badFormDataEntry('Invalid username', 'Username is already in use');
if (emailSearch.length > 0) return badFormDataEntry('Invalid email', 'Email is already in use');

// Create a new user
const newUser = await createLocalUser(name, username, email, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const t = useTranslations(lang, '@studiocms/dashboard:index');

<header class="page-header">
<div class="page-title-container">
<h1 class="page-title">{t("welcome-title")}, <UserName server:defer><Fragment slot='fallback' set:text='User' /></UserName></h1>
<h1 class="page-title">{t("welcome-title")}, <UserName server:defer><Fragment slot='fallback' set:text='User' /></UserName>.</h1>
</div>
<div class="page-actions-container">
<Button color='primary' variant='flat' as="a" href={'https://chat.studiocms.dev'} target='_blank' rel='noreferrer,noopener'>
Expand Down
24 changes: 8 additions & 16 deletions packages/studiocms_dashboard/src/components/SingleSidebar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,24 @@ const tPath = useTranslatedPath(lang);
new SingleSidebarHelper('nav-toggle');
new DropdownHelper('sidebar-user-dropdown', true);

const dropdownList = document.querySelector<HTMLUListElement>('#sidebar-user-dropdown-dropdown');
// Get the dropdown list items
const dropdownListItems = document.querySelector<HTMLUListElement>('#sidebar-user-dropdown-dropdown')!.querySelectorAll<HTMLLIElement>('li');

if (!dropdownList) return;

const dropdownListItems = dropdownList.querySelectorAll<HTMLLIElement>('li');

const dropdownOptions = document.querySelector<HTMLDivElement>('#dropdown-options')!;

console.log(dropdownOptions);
// Get the dropdown options (URLs for each dropdown item)
const { dataset: linkData } = document.querySelector<HTMLDivElement>('#dropdown-options')!;

// Create a record of links (key: dropdown item value, value: URL)
const links: Record<string, string> = {
'user-settings': dropdownOptions.dataset.usersettings!,
'view-site': dropdownOptions.dataset.viewsite!,
'log-out': dropdownOptions.dataset.logout!,
'user-settings': linkData.usersettings!,
'view-site': linkData.viewsite!,
'log-out': linkData.logout!,
};

for (const item of dropdownListItems) {

console.log(item);

item.addEventListener('click', () => {
const value = item.dataset.value!;

window.location.href = `${links[value]}`;

console.log("Clicked", value, links[value]);
})
}

Expand Down

0 comments on commit 9472135

Please sign in to comment.