-
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.
- Loading branch information
Showing
10 changed files
with
99 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
npm config set legacy-peer-deps true |
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,7 +1,11 @@ | ||
import { login } from "./login"; | ||
import { signup } from "./signup"; | ||
import { settings } from "./settings"; | ||
import { logout } from "./logout"; | ||
|
||
export const server = { | ||
login, | ||
signup, | ||
settings, | ||
logout, | ||
}; |
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,14 @@ | ||
import { ActionError, defineAction } from "astro:actions"; | ||
import { lucia } from "@lib/auth/index"; | ||
|
||
export const logout = defineAction({ | ||
accept: "form", | ||
handler: async (_, { locals: {user, session}}) => { | ||
if (!user || !session) throw new ActionError({ | ||
code: "UNAUTHORIZED", | ||
message: "You must be logged in to log out." | ||
}) | ||
|
||
await lucia.invalidateSession(session.id); | ||
}, | ||
}); |
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 { ActionError, defineAction } from "astro:actions"; | ||
import { z } from "astro:schema"; | ||
import { emptyString, slug } from "@lib/schema"; | ||
import { db, users } from "@lib/db"; | ||
import { hashOptions } from "@lib/auth/index"; | ||
import { hash } from "@node-rs/argon2"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
export const settings = defineAction({ | ||
accept: "form", | ||
input: z.object({ | ||
name: emptyString().nullable().or(z.string()), | ||
slug: emptyString().nullable().or(slug()), | ||
password: emptyString().nullable().or(z.string()), | ||
}), | ||
handler: async ({name, slug, password}, {locals: {user}}) => { | ||
if (!user) | ||
throw new ActionError({ | ||
code: "UNAUTHORIZED", | ||
message: "You need to be logged in to edit your preferences." | ||
}) | ||
|
||
if (name == user.name) name = null; | ||
if (slug == user.slug) slug = null; | ||
if (!(name || slug || password)) return; | ||
|
||
await db.update(users).set({ | ||
name: name ?? undefined, | ||
slug: slug ?? undefined, | ||
password: password ? await hash(password, hashOptions) : undefined, | ||
}).where(eq(users.id, user.id)) | ||
}, | ||
}); |
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
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 |
---|---|---|
@@ -1,8 +1,5 @@ | ||
--- | ||
import Layout from "@layouts/Layout.astro"; | ||
const { user } = Astro.locals; | ||
if (!user) return Astro.redirect("/account/signup"); | ||
if (user) return Astro.redirect("/account/settings"); | ||
else return Astro.redirect("/account/signup"); | ||
--- | ||
|
||
<Layout title="Account">Welcome, {user.name}!</Layout> |
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,40 @@ | ||
--- | ||
import Layout from "@layouts/Layout.astro"; | ||
import Form from "@comps/forms/Form.astro"; | ||
import Input from "@comps/forms/Input.astro"; | ||
import Button from "@comps/forms/Button.astro"; | ||
import { actions } from "astro:actions"; | ||
const { user } = Astro.locals; | ||
if (!user) return Astro.redirect("/account/signup"); | ||
--- | ||
|
||
<Layout title="Settings"> | ||
<Form method="POST" action={"/account/settings" + actions.settings}> | ||
<Input name="name" label="Name" value={user.name} /> | ||
<Input name="slug" label="Handle" value={user.slug} /> | ||
<Input type="password" name="password" label="Password" /> | ||
<Button>Update</Button> | ||
</Form> | ||
<h2>Account Management</h2> | ||
<Form> | ||
<Button formmethod="POST" formaction={"/account/signup" + actions.logout} | ||
>Log out</Button | ||
> | ||
</Form> | ||
</Layout> | ||
|
||
<script> | ||
import { slug } from "@lib/schema"; | ||
|
||
const nameInput = document.getElementById("name-input") as HTMLInputElement; | ||
const slugInput = document.getElementById("slug-input") as HTMLInputElement; | ||
|
||
function setSlug(this: HTMLInputElement) { | ||
const { success, data } = slug().safeParse(this.value); | ||
if (success) slugInput.value = data; | ||
} | ||
|
||
nameInput.addEventListener("input", setSlug); | ||
slugInput.addEventListener("input", setSlug); | ||
</script> |