Skip to content

Commit

Permalink
Merge branch 'IMG-51-register-form-server-side-functionality'
Browse files Browse the repository at this point in the history
  • Loading branch information
MrExplode committed Nov 29, 2023
2 parents 3865351 + 859904d commit 079021d
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/lib/components/ui/alert/alert-description.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import { cn } from "$lib/utils";
import type { HTMLAttributes } from "svelte/elements";
type $$Props = HTMLAttributes<HTMLDivElement>;
let className: $$Props["class"] = undefined;
export { className as class };
</script>

<div class={cn("text-sm [&_p]:leading-relaxed", className)} {...$$restProps}>
<slot />
</div>
21 changes: 21 additions & 0 deletions src/lib/components/ui/alert/alert-title.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts">
import { cn } from "$lib/utils";
import type { HTMLAttributes } from "svelte/elements";
import type { HeadingLevel } from ".";
type $$Props = HTMLAttributes<HTMLHeadingElement> & {
level?: HeadingLevel;
};
let className: $$Props["class"] = undefined;
export let level: $$Props["level"] = "h5";
export { className as class };
</script>

<svelte:element
this={level}
class={cn("mb-1 font-medium leading-none tracking-tight", className)}
{...$$restProps}
>
<slot />
</svelte:element>
21 changes: 21 additions & 0 deletions src/lib/components/ui/alert/alert.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts">
import { cn } from "$lib/utils";
import type { HTMLAttributes } from "svelte/elements";
import { alertVariants, type Variant } from ".";
type $$Props = HTMLAttributes<HTMLDivElement> & {
variant?: Variant;
};
let className: $$Props["class"] = undefined;
export let variant: $$Props["variant"] = "default";
export { className as class };
</script>

<div
class={cn(alertVariants({ variant }), className)}
{...$$restProps}
role="alert"
>
<slot />
</div>
33 changes: 33 additions & 0 deletions src/lib/components/ui/alert/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { tv, type VariantProps } from "tailwind-variants";

import Root from "./alert.svelte";
import Description from "./alert-description.svelte";
import Title from "./alert-title.svelte";

export const alertVariants = tv({
base: "relative w-full rounded-lg border p-4 [&>svg]:absolute [&>svg]:text-foreground [&>svg]:left-4 [&>svg]:top-4 [&>svg+div]:translate-y-[-3px] [&:has(svg)]:pl-11",

variants: {
variant: {
default: "bg-background text-foreground",
destructive:
"text-destructive border-destructive/50 dark:border-destructive [&>svg]:text-destructive text-destructive"
}
},
defaultVariants: {
variant: "default"
}
});

export type Variant = VariantProps<typeof alertVariants>["variant"];
export type HeadingLevel = "h1" | "h2" | "h3" | "h4" | "h5" | "h6";

export {
Root,
Description,
Title,
//
Root as Alert,
Description as AlertDescription,
Title as AlertTitle
};
61 changes: 61 additions & 0 deletions src/routes/dash/register/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { PageServerLoad, Actions } from './$types'
import { fail, redirect } from '@sveltejs/kit'
import { setError, superValidate } from 'sveltekit-superforms/server'
import { registerSchema } from '$lib/schema'

interface ResponseType {
message: string
details: any
error: {
statusCode: number
statusPhrase: string
}
}

export const load: PageServerLoad = () => {
return {
form: superValidate(registerSchema)
}
}

export const actions: Actions = {
default: async (event) => {
const form = await superValidate(event, registerSchema)
if (!form.valid) {
return fail(400, {
form
})
}
const response = await fetch('https://api.unideb.tech/auth/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: form.data.username,
email: form.data.email,
password: form.data.password,
inviteKey: form.data.inviteToken
})
})
if (response.status === 200) {
console.log(response)
throw redirect(303, '/dash/login/')
} else {
console.log(response)
const resp = (await response.json()) as ResponseType
let responseError = resp.details
let responseMessage = resp.message
switch (true) {
case responseError[0] === 0:
return setError(form, 'username', responseMessage)
case responseError[0] === 1:
return setError(form, 'email', responseMessage)
case responseError[0] === 2:
return setError(form, 'password', responseMessage)
case responseError[0] === 3:
return setError(form, 'inviteToken', responseMessage)
}
}
}
}
8 changes: 6 additions & 2 deletions src/routes/dash/register/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
<script lang="ts">
import * as Form from '$lib/components/ui/form'
import * as Card from '$lib/components/ui/card'
import { Label } from '$components/ui/label'
import { Button } from '$components/ui/button'
import type { RegisterSchema } from '$lib/schema'
import type { SuperValidated } from 'sveltekit-superforms'
import RegisterForm from '$lib/RegisterForm.svelte'
import Centered from '$lib/Centered.svelte'
import logo from '$lib/assets/icons/bordered/logo_slate-700.png'
export let form: SuperValidated<RegisterSchema>
export let data: { errorMessage?: string } = {}
</script>


<Centered>
<img class="w-48 lg:w-1/8 mb-10" src={logo} alt="Logo" />
<Card.Root class="w-100 bg-slate-900">
<Card.Header>
<Card.Title class="text-2xl">Register a New Account</Card.Title>
</Card.Header>

<Card.Content>
<RegisterForm {form} />
<Label class="">Already have an account?</Label>
Expand Down

0 comments on commit 079021d

Please sign in to comment.