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

fix: update register page #65

Merged
merged 1 commit into from
Sep 25, 2024
Merged

fix: update register page #65

merged 1 commit into from
Sep 25, 2024

Conversation

krivahtoo
Copy link
Owner

@krivahtoo krivahtoo commented Sep 23, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Implemented a robust logout functionality, ensuring proper token clearance before redirecting to the login page.
    • Enhanced registration form with improved validation, error handling, and user feedback mechanisms.
  • Improvements

    • Updated UI components with new icons for better visual representation of service statuses.
    • Added custom error messages for login validation to enhance user experience.
    • Improved hover effect on navigation icons for better visual feedback.
  • Bug Fixes

    • Corrected loading state management during login and registration processes to ensure accurate feedback.

Copy link

coderabbitai bot commented Sep 23, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes encompass several components of the frontend application, introducing a logout functionality in the navigation header, modifying the dashboard's service status cards with new icons, enhancing user login validation, and implementing a comprehensive registration form with improved error handling. The updates ensure better user experience through streamlined processes and clearer feedback mechanisms.

Changes

File Path Change Summary
frontend/src/lib/components/nav/header.svelte Implemented logout functionality with fetch call, updated class attributes, and modified account settings navigation.
frontend/src/routes/(dashboard)/+page.svelte Removed readable store, replaced icons in service status cards, and adjusted Subscribe component structure.
frontend/src/routes/login/+page.svelte Updated validation schema for login with custom error messages and improved loading state handling.
frontend/src/routes/register/+page.svelte Introduced a new registration function with enhanced validation, error handling, and improved form layout.
frontend/src/routes/settings/+layout.svelte Increased scaling effect of the icon's hover state for improved visual feedback.

Possibly related PRs

  • feat(ui): update dashboard design #27: The changes in header.svelte related to class adjustments may complement the overall UI enhancements in the dashboard design, indicating a potential connection in user interface consistency.

🐇 In the code we hop and play,
With logout features on display.
Icons change, and forms refine,
User journeys intertwine.
Validation sings a sweet refrain,
In our app, joy shall remain! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 8

Outside diff range and nitpick comments (8)
frontend/src/routes/login/+page.svelte (1)

55-55: LGTM: Improved error handling

Setting is_loading to false after handling validation errors is a good practice. It ensures that the UI doesn't get stuck in a loading state when validation fails.

For consistency, consider moving this line to a finally block:

 if (!valid_res.success) {
   // handle error then return
   // console.log(valid_res.error.flatten());
   fieldErrors = { ...{ username: [], password: [] }, ...valid_res.error.flatten().fieldErrors };
-  is_loading = false;
+} finally {
+  is_loading = false;
 }

This ensures is_loading is always set to false, regardless of whether the validation succeeds or fails.

frontend/src/routes/(dashboard)/+page.svelte (2)

2-2: LGTM! Consider organizing imports.

The changes to the imports look good. The removal of unused imports and addition of new icon imports improve code cleanliness.

Consider grouping related imports together for better organization. For example, you could group all Lucide icon imports:

import Activity from 'lucide-svelte/icons/activity';
import Timer from 'lucide-svelte/icons/timer';
import CircleArrowUp from 'lucide-svelte/icons/circle-arrow-up';
import CircleArrowDown from 'lucide-svelte/icons/circle-arrow-down';

Also applies to: 22-23


Line range hint 222-230: Table rendering structure improved.

The changes to the Subscribe component usage in the table rendering look good. They appear to improve the structure slightly without changing the functionality.

For consistency, consider using the same structure for both branches of the conditional rendering:

{#if cell.id !== 'select' && cell.id !== 'actions'}
  <Render of={cell.render()} />
{:else}
  <Render of={cell.render()} />
{/if}

This could be simplified to just:

<Render of={cell.render()} />

Since both branches are identical, the conditional statement appears unnecessary.

frontend/src/routes/register/+page.svelte (5)

71-71: Reset newUser.confirm after registration attempt

Currently, only newUser.password is reset after registration. To ensure all sensitive data is cleared, newUser.confirm should also be reset.

Apply this diff to reset newUser.confirm:

  newUser.password = '';
+ newUser.confirm = '';

122-122: Correct typo and clarify password requirements

There's a typo in the password hint text, and it could be clearer. 'Atleast' should be 'at least', and 'upper letter' should be 'one uppercase letter'.

Apply this diff to correct the text:

- A strong password should contain atleast upper letter, 1 number and 1 symbol
+ A strong password should contain at least one uppercase letter, 1 number, and 1 symbol

155-155: Update label to 'Show passwords'

If the show_pass toggle affects both password fields, update the label to 'Show passwords' for clarity.

Apply this diff:

-   Show password
+   Show passwords

149-152: Rename checkbox id for clarity

The checkbox controlling password visibility has an id of terms, which may be misleading. Renaming it to show-password improves clarity.

Apply this diff:

- <Checkbox id="terms" bind:checked={show_pass} aria-labelledby="terms-label" />
+ <Checkbox id="show-password" bind:checked={show_pass} aria-labelledby="show-password-label" />

- <Label
-   id="terms-label"
-   for="terms"
+ <Label
+   id="show-password-label"
+   for="show-password"
    class="cursor-pointer text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
  >

47-47: Remove debugging console statements

There are commented-out and active console.log statements that may expose sensitive information or clutter the console in production.

Apply this diff to remove them:

- // console.log(valid_res);
- console.log(res);
- // console.log(e);

Also applies to: 60-60, 68-68

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aea95de and 30de591.

Files selected for processing (4)
  • frontend/src/lib/components/nav/header.svelte (5 hunks)
  • frontend/src/routes/(dashboard)/+page.svelte (7 hunks)
  • frontend/src/routes/login/+page.svelte (3 hunks)
  • frontend/src/routes/register/+page.svelte (1 hunks)
Additional comments not posted (9)
frontend/src/routes/login/+page.svelte (2)

37-38: LGTM: Improved validation messages

The custom error messages for username and password validation are clear and informative. This change enhances the user experience by providing specific feedback on input requirements.


176-176: Please clarify the change on this line

The purpose of the change on this line is not clear from the context provided. Could you please explain what modification was made here and why?

To help understand the context, let's check the surrounding code:

frontend/src/lib/components/nav/header.svelte (4)

22-23: LGTM: New imports for enhanced logout functionality

The addition of token and cfetch imports are appropriate for implementing the updated logout functionality. These changes align well with the PR objective of updating the register page, as they contribute to improved authentication flow.


45-47: LGTM: Improved header styling

The changes to the header's class attribute enhance the component's appearance and responsiveness. The addition of dark mode support (dark:border-primary/50) and responsive height adjustment (lg:h-[60px]) are particularly noteworthy improvements.


50-50: LGTM: Improved button positioning

The addition of the "mr-auto" class to the Button component improves its positioning within the header. This change ensures that the button aligns correctly to the left side of its container, which is typically the desired layout for a navigation toggle button.


145-148: LGTM: Enhanced account settings navigation

The addition of the href attribute to the DropdownMenu.Item for account settings improves user navigation. The updated icon (UserRoundCog) and text ("Account") provide clearer visual and textual cues for the user. These changes contribute to a more intuitive and user-friendly interface.

frontend/src/routes/(dashboard)/+page.svelte (3)

171-171: Icon change improves visual representation.

The replacement of CreditCard with CircleArrowUp for the "Up Services" card is a good choice. It better represents the concept of services being up or operational.


181-181: Icon change enhances visual consistency.

The replacement of Users with CircleArrowDown for the "Down" services card is appropriate. It creates a visual parallel with the "Up Services" card and clearly represents the concept of services being down.


191-191: Consistent use of Timer icon for Pending services.

The retention of the Timer icon for the "Pending" services card maintains consistency and appropriately represents the concept of services in a waiting or pending state.

Comment on lines +37 to +41
cfetch('/logout').finally(() => {
token.set(null);
user.set(null);
goto('/login');
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved logout flow, consider error handling

The updated logout function now properly communicates with the server before clearing client-side state, which is a significant improvement. The use of finally ensures that the client-side cleanup always occurs.

Consider adding error handling to log any issues that might occur during the logout process:

 function logout() {
-  cfetch('/logout').finally(() => {
+  cfetch('/logout').catch(error => {
+    console.error('Logout failed:', error);
+  }).finally(() => {
     token.set(null);
     user.set(null);
     goto('/login');
   });
 }

This addition would help with debugging potential issues without affecting the user experience.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cfetch('/logout').finally(() => {
token.set(null);
user.set(null);
goto('/login');
});
cfetch('/logout').catch(error => {
console.error('Logout failed:', error);
}).finally(() => {
token.set(null);
user.set(null);
goto('/login');
});

frontend/src/routes/register/+page.svelte Outdated Show resolved Hide resolved
frontend/src/routes/register/+page.svelte Outdated Show resolved Hide resolved
frontend/src/routes/register/+page.svelte Outdated Show resolved Hide resolved
frontend/src/routes/register/+page.svelte Outdated Show resolved Hide resolved
frontend/src/routes/register/+page.svelte Outdated Show resolved Hide resolved
frontend/src/routes/register/+page.svelte Outdated Show resolved Hide resolved
@krivahtoo krivahtoo merged commit 3671e59 into main Sep 25, 2024
5 checks passed
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 30de591 and 095e810.

📒 Files selected for processing (5)
  • frontend/src/lib/components/nav/header.svelte (5 hunks)
  • frontend/src/routes/(dashboard)/+page.svelte (7 hunks)
  • frontend/src/routes/login/+page.svelte (5 hunks)
  • frontend/src/routes/register/+page.svelte (1 hunks)
  • frontend/src/routes/settings/+layout.svelte (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • frontend/src/lib/components/nav/header.svelte
  • frontend/src/routes/(dashboard)/+page.svelte
  • frontend/src/routes/login/+page.svelte
🔇 Additional comments not posted (3)
frontend/src/routes/settings/+layout.svelte (1)

35-35: LGTM! Consider verifying visual impact and consistency.

The increased scaling factor (from scale-110 to scale-125) for the Activity icon on hover provides more pronounced visual feedback, which can enhance user experience. However, please ensure that:

  1. The larger scale doesn't cause any layout issues or overlap with surrounding elements.
  2. This hover effect is consistent with other navigation items for a uniform user experience.

It might be helpful to review this change in the context of your overall design system to maintain consistency across the application.

To verify consistency, you can run the following script:

✅ Verification successful

Verification Complete: Hover Scaling Consistent

The increased scaling factor (scale-125) for the Activity icon in frontend/src/routes/settings/+layout.svelte is consistent across the codebase. No inconsistencies were found in other Svelte navigation components.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for inconsistent icon hover effects across navigation components

# Search for similar icon hover effects in other navigation components
rg --type svelte 'class=".*group-hover:scale-[0-9]+'

# Note: If the results show varying scale factors, consider standardizing them for consistency.

Length of output: 85


Script:

#!/bin/bash
# Description: Check for inconsistent icon hover effects across Svelte navigation components

# Search for similar icon hover effects in other Svelte navigation components
rg 'class="[^"]*group-hover:scale-[0-9]+' -g '*.svelte'

# Note: If the results show varying scale factors, consider standardizing them for consistency.

Length of output: 175

frontend/src/routes/register/+page.svelte (2)

9-9: LGTM: New imports enhance functionality.

The new imports add necessary components and utilities for improved error handling, loading state management, and navigation. These additions contribute to a better user experience.

Also applies to: 11-11, 13-15


39-53: LGTM: Robust form validation schema.

The Zod schema provides comprehensive validation for the registration form, including password complexity requirements. This is a significant improvement in form validation and security.

Comment on lines +18 to +37
/** @type {string[]} */
let errors = [];

let is_loading = false;
let show_pass = false;
$: type = show_pass ? 'text' : 'password';

let confirm = '';
let newUser = {
username: '',
password: '',
confirm: ''
};

/** @type {{ username: string[], password: string[], confirm: string[] }} */
let fieldErrors = {
username: [],
password: [],
confirm: []
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure consistent use of confirm variable.

The state management looks good overall, but there's an inconsistency in how the confirm password is handled.

Apply this diff to resolve the inconsistency:

-let confirm = '';
 let newUser = {
   username: '',
   password: '',
   confirm: ''
 };

This change ensures that confirm is consistently used as part of the newUser object throughout the component.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/** @type {string[]} */
let errors = [];
let is_loading = false;
let show_pass = false;
$: type = show_pass ? 'text' : 'password';
let confirm = '';
let newUser = {
username: '',
password: '',
confirm: ''
};
/** @type {{ username: string[], password: string[], confirm: string[] }} */
let fieldErrors = {
username: [],
password: [],
confirm: []
};
/** @type {string[]} */
let errors = [];
let is_loading = false;
let show_pass = false;
$: type = show_pass ? 'text' : 'password';
let newUser = {
username: '',
password: '',
confirm: ''
};
/** @type {{ username: string[], password: string[], confirm: string[] }} */
let fieldErrors = {
username: [],
password: [],
confirm: []
};

Comment on lines +111 to +199
<Alert.Title>Error</Alert.Title>
{#each errors as err, idx (idx)}
<Alert.Description>{err}</Alert.Description>
{/each}
</Alert.Root>
{/if}
<div class="grid gap-1">
<Label for="username">Username</Label>
<Input
id="username"
class={fieldErrors.username.length > 0 ? 'border-red-300' : ''}
bind:value={newUser.username}
placeholder="Enter username..."
required
/>
{#each fieldErrors.username as er}
<p class="text-xs text-destructive">{er}</p>
{/each}
</div>
<Input id="password" {type} placeholder="Enter new pass.." />
</div>
<div class="grid gap-2">
<Label for="confirm-password">Confirm Password</Label>
<Input id="confirm-password" type="password" placeholder="Re-type the pass.." />
</div>
<div class="flex items-center space-x-2">
<Checkbox id="terms" bind:checked={show_pass} aria-labelledby="terms-label" />
<Label
id="terms-label"
for="terms"
class="cursor-pointer text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
<div class="grid gap-1">
<div class="flex items-center">
<Label for="password">Password</Label>
<HoverCard.Root>
<HoverCard.Trigger
class="ml-auto inline-block text-sm underline-offset-1 hover:underline"
>
<CircleHelp class="text-secondary-foreground dark:text-secondary" size={20} />
</HoverCard.Trigger>
<HoverCard.Content class="dark:border-primary/40">
A strong password must contain at least one uppercase letter, 1 number, and 1 symbol
</HoverCard.Content>
</HoverCard.Root>
</div>
<Input
id="password"
class={fieldErrors.password.length > 0 ? 'border-red-300' : ''}
{type}
bind:value={newUser.password}
placeholder="Enter new pass.."
/>
{#each fieldErrors.password as er}
<p class="text-xs text-destructive">{er}</p>
{/each}
</div>
<div class="grid gap-1">
<Label for="confirm-password">Confirm Password</Label>
<Input
id="confirm-password"
class={fieldErrors.confirm.length > 0 ? 'border-red-300' : ''}
type="password"
bind:value={confirm}
placeholder="Re-type the pass.."
/>
{#each fieldErrors.confirm as er}
<p class="text-xs text-destructive">{er}</p>
{/each}
</div>
<div class="flex items-center space-x-2">
<Checkbox
id="show-password"
bind:checked={show_pass}
aria-labelledby="show-password-label"
/>
<Label
id="show-password-label"
for="show-password"
class="cursor-pointer text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
Show password
</Label>
</div>
<Button
type="submit"
disabled={!newUser.username || !newUser.password || !confirm || is_loading}
class="w-full"
>
Show password
</Label>
{#if is_loading}
<LoaderCircle class="mr-2 h-4 w-4 animate-spin" />
{:else}
Create an account
{/if}
</Button>
</div>
<Button type="submit" class="w-full">Create an account</Button>
</div>
</form>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve form accessibility and user experience.

The form structure and implementation have been significantly improved. However, there are a few suggestions to enhance accessibility and user experience:

  1. Add aria-invalid attributes to input fields with errors:
 <Input
   id="username"
   class={fieldErrors.username.length > 0 ? 'border-red-300' : ''}
+  aria-invalid={fieldErrors.username.length > 0}
   bind:value={newUser.username}
   placeholder="Enter username..."
   required
 />

Apply similar changes to password and confirm password fields.

  1. Use aria-describedby to associate error messages with input fields:
 <div class="grid gap-1">
   <Label for="username">Username</Label>
   <Input
     id="username"
     class={fieldErrors.username.length > 0 ? 'border-red-300' : ''}
+    aria-describedby="username-error"
     bind:value={newUser.username}
     placeholder="Enter username..."
     required
   />
-  {#each fieldErrors.username as er}
-    <p class="text-xs text-destructive">{er}</p>
-  {/each}
+  <div id="username-error" aria-live="polite">
+    {#each fieldErrors.username as er}
+      <p class="text-xs text-destructive">{er}</p>
+    {/each}
+  </div>
 </div>

Apply similar changes to password and confirm password fields.

  1. Update the confirm password input to use the type variable for consistency:
 <Input
   id="confirm-password"
   class={fieldErrors.confirm.length > 0 ? 'border-red-300' : ''}
-  type="password"
+  {type}
   bind:value={newUser.confirm}
   placeholder="Re-type the pass.."
 />

These changes will improve the accessibility of the form and provide a more consistent user experience.

Committable suggestion was skipped due to low confidence.

Comment on lines +55 to +97
async function register() {
is_loading = true;
let valid_res = baseSchema.safeParse(newUser);
// console.log(valid_res);
if (!valid_res.success) {
console.log(valid_res.error.flatten());
// @ts-ignore
fieldErrors = {
...{ username: [], password: [], confirm: [] },
...valid_res.error.flatten().fieldErrors
};
toast.error('Validation Errors', {
description: 'Please correct the errors in the form and try again'
});
is_loading = false;
} else {
try {
let res = await cfetch('/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newUser),
credentials: 'same-origin'
});
console.log(res);
if (res.ok) {
goto('/login');
} else {
let data = await res.json();
errors = [data.error];
}
} catch (e) {
// console.log(e);
toast.error('Network error', {
description:
'Failed to connect to server. Please check your internet connection and try again.'
});
errors = [`${e}`];
} finally {
newUser.password = '';
is_loading = false;
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance error handling in the register function.

The register function implementation looks good overall, but there are a few areas for improvement:

  1. Consider using a more specific error type for network errors:
-errors = [`${e}`];
+errors = ['Network error: Unable to connect to the server. Please try again later.'];
  1. Add a catch-all error message for unexpected server responses:
 } else {
   let data = await res.json();
-  errors = [data.error];
+  errors = data.error ? [data.error] : ['An unexpected error occurred. Please try again.'];
 }
  1. Clear the confirm password field along with the password field:
 } finally {
   newUser.password = '';
+  newUser.confirm = '';
   is_loading = false;
 }

These changes will provide more informative error messages to users and ensure consistent clearing of sensitive form fields.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async function register() {
is_loading = true;
let valid_res = baseSchema.safeParse(newUser);
// console.log(valid_res);
if (!valid_res.success) {
console.log(valid_res.error.flatten());
// @ts-ignore
fieldErrors = {
...{ username: [], password: [], confirm: [] },
...valid_res.error.flatten().fieldErrors
};
toast.error('Validation Errors', {
description: 'Please correct the errors in the form and try again'
});
is_loading = false;
} else {
try {
let res = await cfetch('/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newUser),
credentials: 'same-origin'
});
console.log(res);
if (res.ok) {
goto('/login');
} else {
let data = await res.json();
errors = [data.error];
}
} catch (e) {
// console.log(e);
toast.error('Network error', {
description:
'Failed to connect to server. Please check your internet connection and try again.'
});
errors = [`${e}`];
} finally {
newUser.password = '';
is_loading = false;
}
}
}
async function register() {
is_loading = true;
let valid_res = baseSchema.safeParse(newUser);
// console.log(valid_res);
if (!valid_res.success) {
console.log(valid_res.error.flatten());
// @ts-ignore
fieldErrors = {
...{ username: [], password: [], confirm: [] },
...valid_res.error.flatten().fieldErrors
};
toast.error('Validation Errors', {
description: 'Please correct the errors in the form and try again'
});
is_loading = false;
} else {
try {
let res = await cfetch('/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newUser),
credentials: 'same-origin'
});
console.log(res);
if (res.ok) {
goto('/login');
} else {
let data = await res.json();
errors = data.error ? [data.error] : ['An unexpected error occurred. Please try again.'];
}
} catch (e) {
// console.log(e);
toast.error('Network error', {
description:
'Failed to connect to server. Please check your internet connection and try again.'
});
errors = ['Network error: Unable to connect to the server. Please try again later.'];
} finally {
newUser.password = '';
newUser.confirm = '';
is_loading = false;
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant