-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: authentication, form validation
- Loading branch information
Showing
20 changed files
with
452 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,4 +75,8 @@ | |
body { | ||
@apply bg-background text-foreground; | ||
} | ||
html, | ||
body { | ||
@apply h-full; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,115 @@ | ||
import * as argon2 from "argon2"; | ||
import { SvelteKitAuth } from "@auth/sveltekit"; | ||
import CredentialsProvider from "@auth/sveltekit/providers/credentials"; | ||
import { getUserByEmailOrUsername } from "$lib/server/repositories/user-repository"; | ||
import { type Handle, redirect } from "@sveltejs/kit"; | ||
import { sequence } from "@sveltejs/kit/hooks"; | ||
import { wrapResult } from "$lib/utils"; | ||
import { authSchema } from "$lib/schema/auth"; | ||
import { dev } from "$app/environment"; | ||
|
||
export const handle = SvelteKitAuth({ | ||
providers: [CredentialsProvider({ | ||
authorize: async (credentials) => { | ||
const authorization: Handle = async ({ event, resolve }) => { | ||
const pathname = event.url.pathname; | ||
// redirect for index path | ||
if (pathname === "/") { | ||
throw redirect(303, "/app"); | ||
} | ||
|
||
const session = await event.locals.getSession(); | ||
|
||
if (!session) { | ||
if (pathname.startsWith("/app")) { | ||
throw redirect(303, "/sign-in"); | ||
} | ||
} | ||
|
||
if (session) { | ||
if (pathname.startsWith("/sign-in")) { | ||
throw redirect(303, "/app"); | ||
} | ||
})], | ||
}); | ||
} | ||
|
||
return resolve(event); | ||
}; | ||
|
||
export const handle = sequence( | ||
SvelteKitAuth({ | ||
providers: [ | ||
CredentialsProvider({ | ||
id: "credentials", | ||
authorize: async (credentials) => { | ||
const validatedCredentials = authSchema.safeParse(credentials); | ||
if (!validatedCredentials.success) { | ||
return null; | ||
} | ||
|
||
// TODO(elianiva): only for temporary development purpose, please remove later | ||
if (dev) { | ||
if ( | ||
validatedCredentials.data.username === "admin" && | ||
validatedCredentials.data.password === "password" | ||
) { | ||
return { | ||
id: "admin", | ||
username: "admin", | ||
fullname: "Administrator", | ||
email: "admin@localhost", | ||
role: "admin", | ||
}; | ||
} | ||
} | ||
|
||
const [user, userError] = await wrapResult( | ||
getUserByEmailOrUsername(validatedCredentials.data.username as string), | ||
); | ||
if (user === null || userError !== null) { | ||
return null; | ||
} | ||
|
||
const [isPasswordMatch, verifyError] = await wrapResult( | ||
argon2.verify(user.password, validatedCredentials.data.password as string, { | ||
type: argon2.argon2i, | ||
}), | ||
); | ||
if (!isPasswordMatch || verifyError !== null) { | ||
return null; | ||
} | ||
|
||
return { | ||
id: user._id.toHexString(), | ||
username: user.username, | ||
fullname: user.fullname, | ||
email: user.email, | ||
role: user.role, | ||
}; | ||
}, | ||
}), | ||
], | ||
session: { | ||
strategy: "jwt", | ||
}, | ||
callbacks: { | ||
async jwt({ token, user }) { | ||
if (user) { | ||
token.sub = user.id; | ||
token.user = { | ||
id: user.id, | ||
username: user.username, | ||
fullname: user.fullname, | ||
email: user.email, | ||
role: user.role, | ||
}; | ||
} | ||
return token; | ||
}, | ||
async session({ session, token }) { | ||
session.user = token.user; | ||
return session; | ||
}, | ||
}, | ||
pages: { | ||
signIn: "/sign-in", | ||
}, | ||
}), | ||
authorization, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Root from "./label.svelte"; | ||
|
||
export { | ||
Root, | ||
// | ||
Root as Label | ||
}; |
Oops, something went wrong.