From dc80dec6a83cf79587f311d43f88e38bdfaf320d Mon Sep 17 00:00:00 2001 From: Florian BOUILLON Date: Wed, 21 Jun 2023 19:55:52 +0200 Subject: [PATCH] feat: Add account pages Signed-off-by: Florian BOUILLON --- src/components/Passthrough.astro | 3 +- src/pages/account/login.astro | 47 ++++++++++++++++++++++++++++++++ src/pages/account/logout.astro | 4 +++ src/pages/account/register.astro | 45 ++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/pages/account/login.astro create mode 100644 src/pages/account/logout.astro create mode 100644 src/pages/account/register.astro diff --git a/src/components/Passthrough.astro b/src/components/Passthrough.astro index a31a5df..09b4011 100644 --- a/src/components/Passthrough.astro +++ b/src/components/Passthrough.astro @@ -1,6 +1,4 @@ --- -const json = JSON.stringify(Astro.props) - /** * note: you MUST only pass simple items that can go in JSON format natively */ @@ -12,5 +10,6 @@ export function load(): T { return JSON.parse(tag.innerText) } +const json = JSON.stringify(Astro.props) --- diff --git a/src/pages/account/login.astro b/src/pages/account/login.astro new file mode 100644 index 0000000..792489e --- /dev/null +++ b/src/pages/account/login.astro @@ -0,0 +1,47 @@ +--- +import URLManager from '@dzeio/url-manager' +import Layout from '../../layouts/Layout.astro' +import DaoFactory from '../../models/DaoFactory' +import { comparePassword } from '../../libs/AuthUtils' + +const logout = new URLManager(Astro.url).query('logout') + +if (typeof logout === 'string') { + DaoFactory.get('session').removeSession(Astro.response) +} + +// DaoFactory.get('session').removeSession(Astro.response) + +if (Astro.request.method === 'POST') { + const form = await Astro.request.formData() + const email = form.get('email') as string + const password = form.get('password') as string + + const account = await DaoFactory.get('user').findOne({ + email + }) + + if (!account) { + return + } + const valid = await comparePassword(password, account.password) + if (!valid) { + return + } + DaoFactory.get('session').setSession({ + userId: account.id + }, Astro.response) +} + +--- + + +
+
+ + + + +
+
+
diff --git a/src/pages/account/logout.astro b/src/pages/account/logout.astro new file mode 100644 index 0000000..10107f4 --- /dev/null +++ b/src/pages/account/logout.astro @@ -0,0 +1,4 @@ +--- +return Astro.redirect('/account/login?logout') + +--- diff --git a/src/pages/account/register.astro b/src/pages/account/register.astro new file mode 100644 index 0000000..f64ec4f --- /dev/null +++ b/src/pages/account/register.astro @@ -0,0 +1,45 @@ +--- +import Layout from '../../layouts/Layout.astro' +import { hashPassword } from '../../libs/AuthUtils' +import DaoFactory from '../../models/DaoFactory' + +let errorMessage: string | undefined + +if (Astro.request.method === 'POST') { + const form = await Astro.request.formData() + const email = form.get('email') as string + const password = form.get('password') as string + + const user = await DaoFactory.get('user').create({ + email: email, + password: await hashPassword(password) + }) + + if (!user) { + errorMessage = 'User already exists' + return + } + + DaoFactory.get('session').setSession({ + userId: user.id + }, Astro.response) + +} + +--- + + +
+ {errorMessage && ( +
+ {errorMessage} +
+ )} +
+ + + + +
+
+