homebox/frontend/pages/index.vue

259 lines
7.8 KiB
Vue
Raw Normal View History

2022-09-01 22:32:03 +00:00
<script setup lang="ts">
2024-04-28 16:42:37 +00:00
import { useRouteHash } from "@vueuse/router";
import MdiAccount from "~icons/mdi/account";
import MdiAccountPlus from "~icons/mdi/account-plus";
import MdiLogin from "~icons/mdi/login";
import MdiArrowRight from "~icons/mdi/arrow-right";
import MdiLock from "~icons/mdi/lock";
2024-04-28 16:42:37 +00:00
enum PageForms {
Register = "register",
Login = "login",
ForgotPassword = "forgot-password",
}
2022-09-01 22:32:03 +00:00
useHead({
title: "Homebox | Organize and Tag Your Stuff",
2022-09-01 22:32:03 +00:00
});
definePageMeta({
layout: "empty",
middleware: [
() => {
const ctx = useAuthContext();
if (ctx.isAuthorized()) {
return "/home";
}
},
],
2022-09-01 22:32:03 +00:00
});
const ctx = useAuthContext();
const api = usePublicApi();
const toast = useNotifier();
2024-04-28 16:42:37 +00:00
const pageForm = useRouteHash(PageForms.Login);
const pageFormStr = computed(() => (pageForm.value[0] === "#" ? pageForm.value.slice(1) : pageForm.value));
const route = useRoute();
const router = useRouter();
const username = ref("");
const email = ref("");
const password = ref("");
const canRegister = ref(false);
const remember = ref(false);
2022-09-01 22:32:03 +00:00
const groupToken = computed<string>({
get() {
const params = route.query.token;
if (typeof params === "string") {
return params;
}
return "";
},
set(v) {
router.push({
query: {
token: v,
},
});
},
});
2022-09-01 22:32:03 +00:00
async function registerUser() {
loading.value = true;
2022-09-04 06:19:13 +00:00
const { error } = await api.register({
name: username.value,
email: email.value,
password: password.value,
token: groupToken.value,
2022-09-01 22:32:03 +00:00
});
if (error) {
toast.error("Problem registering user");
2022-09-04 06:19:13 +00:00
return;
2022-09-01 22:32:03 +00:00
}
toast.success("User registered");
2022-09-04 06:19:13 +00:00
2022-09-01 22:32:03 +00:00
loading.value = false;
2024-04-28 16:42:37 +00:00
pageForm.value = PageForms.Login;
2022-09-01 22:32:03 +00:00
}
onMounted(() => {
if (groupToken.value !== "") {
2024-04-28 16:42:37 +00:00
pageForm.value = PageForms.Register;
}
});
2022-09-01 22:32:03 +00:00
const loading = ref(false);
const loginPassword = ref("");
2022-09-01 22:32:03 +00:00
async function login() {
loading.value = true;
const { error } = await ctx.login(api, email.value, loginPassword.value, remember.value);
2022-09-01 22:32:03 +00:00
if (error) {
toast.error("Invalid email or password");
loading.value = false;
return;
}
2022-09-01 22:32:03 +00:00
toast.success("Logged in successfully");
2022-09-01 22:32:03 +00:00
navigateTo("/home");
2022-09-01 22:32:03 +00:00
loading.value = false;
}
2024-04-28 16:42:37 +00:00
async function resetPassword() {
if (email.value === "") {
toast.error("Email is required");
return;
}
const resp = await api.resetPasseord(email.value);
if (resp.error) {
toast.error("Problem resetting password");
return;
}
toast.success("Password reset link sent to your email");
return await Promise.resolve();
}
2022-09-01 22:32:03 +00:00
</script>
<template>
2024-04-28 17:05:17 +00:00
<NuxtLayout v-slot="{ status }" name="center-card">
2022-09-05 00:37:37 +00:00
<div>
2024-04-28 17:05:17 +00:00
<Transition name="slide-fade">
<form v-if="pageFormStr === PageForms.Register" @submit.prevent="registerUser">
<div class="card w-max-[500px] md:w-[500px] bg-base-100 shadow-xl">
<div class="card-body">
<h2 class="card-title text-2xl align-center">
<MdiAccount class="mr-1 w-7 h-7" />
Register
</h2>
<FormTextField v-model="email" label="Set your email?" />
<FormTextField v-model="username" label="What's your name?" />
<div v-if="!(groupToken == '')" class="pt-4 pb-1 text-center">
<p>You're Joining an Existing Group!</p>
<button type="button" class="text-xs underline" @click="groupToken = ''">
Don't Want To Join a Group?
</button>
2022-09-01 22:32:03 +00:00
</div>
2024-04-28 17:05:17 +00:00
<FormPassword v-model="password" label="Set your password" />
<PasswordScore v-model:valid="canRegister" :password="password" />
<div class="card-actions justify-end">
<button
type="submit"
class="btn btn-primary mt-2"
:class="loading ? 'loading' : ''"
:disabled="loading || !canRegister"
>
Register
</button>
2024-04-28 16:42:37 +00:00
</div>
2024-04-28 17:05:17 +00:00
</div>
</div>
</form>
<form v-else-if="pageFormStr === PageForms.ForgotPassword" @submit.prevent="resetPassword">
<div class="card w-max-[500px] md:w-[500px] bg-base-100 shadow-xl">
<div class="card-body">
<h2 class="card-title text-2xl align-center">
<MdiAccount class="mr-1 w-7 h-7" />
Reset Password
</h2>
<FormTextField v-model="email" label="Email" />
<p class="text-sm text-base-content/50">
If you have an account with us, we will send you a password reset link.
</p>
<div class="card-actions justify-end mt-4">
<button
type="submit"
class="btn btn-primary btn-block"
:class="loading ? 'loading' : ''"
:disabled="loading"
>
Reset Password
</button>
2022-09-01 22:32:03 +00:00
</div>
2024-04-28 17:05:17 +00:00
</div>
</div>
</form>
<form v-else @submit.prevent="login">
<div class="card w-max-[500px] md:w-[500px] bg-base-100 shadow-xl">
<div class="card-body">
<h2 class="card-title text-2xl align-center">
<MdiAccount class="mr-1 w-7 h-7" />
Login
</h2>
<template v-if="status && status.demo">
<p class="text-xs italic text-center">This is a demo instance</p>
<p class="text-xs text-center"><b>Email</b> demo@example.com</p>
<p class="text-xs text-center"><b>Password</b> demo</p>
</template>
2024-04-28 17:05:17 +00:00
<FormTextField v-model="email" label="Email" />
<FormPassword v-model="loginPassword" label="Password" />
<div class="max-w-[140px]">
<FormCheckbox v-model="remember" label="Remember Me" />
</div>
<div class="card-actions justify-end">
<button
type="submit"
class="btn btn-primary btn-block"
:class="loading ? 'loading' : ''"
:disabled="loading"
>
Login
</button>
</div>
</div>
2022-09-05 00:37:37 +00:00
</div>
2024-04-28 17:05:17 +00:00
</form>
</Transition>
<div class="text-center mt-6">
<BaseButton
v-if="status && status.allowRegistration"
class="btn-primary btn-wide"
:to="pageFormStr === PageForms.Register ? `#${PageForms.Login}` : `#${PageForms.Register}`"
>
<template #icon>
<MdiAccountPlus v-if="pageFormStr === PageForms.Register" class="w-5 h-5 swap-off" />
<MdiLogin v-else class="w-5 h-5 swap-off" />
<MdiArrowRight class="w-5 h-5 swap-on" />
</template>
{{ pageFormStr === PageForms.Register ? "Login" : "Register" }}
</BaseButton>
<p v-else class="text-base-content italic text-sm inline-flex items-center gap-2">
<MdiLock class="w-4 h-4 inline-block" />
Registration Disabled
</p>
<NuxtLink :to="`#${PageForms.ForgotPassword}`">
<p class="text-xs text-base-content/50 mt-2">Forgot your password?</p>
</NuxtLink>
2022-09-01 22:32:03 +00:00
</div>
</div>
2024-04-28 17:05:17 +00:00
</NuxtLayout>
2022-09-01 22:32:03 +00:00
</template>
<style lang="css" scoped>
.slide-fade-enter-active {
transition: all 0.2s ease-out;
}
.slide-fade-enter-from,
.slide-fade-leave-to {
position: absolute;
transform: translateX(20px);
opacity: 0;
}
progress[value]::-webkit-progress-value {
transition: width 0.5s;
}
2022-09-01 22:32:03 +00:00
</style>