Files
warren/frontend/pages/login.vue
2025-08-09 00:31:35 +02:00

153 lines
4.1 KiB
Vue

<script setup lang="ts">
import {
Card,
CardHeader,
CardTitle,
CardDescription,
CardContent,
CardFooter,
} from '@/components/ui/card';
import { toTypedSchema } from '@vee-validate/yup';
import { useForm } from 'vee-validate';
import { loginUser } from '~/lib/api/auth/login';
import { getRedirectUrl, oidcLoginUser } from '~/lib/api/auth/oidc';
import { loginSchema } from '~/lib/schemas/auth';
definePageMeta({
layout: 'auth',
middleware: ['not-authenticated'],
});
useHead({
title: 'Login - Warren',
});
const route = useRoute();
// TODO: Get this from the backend
const OPEN_ID = true;
const loggingIn = ref(false);
if (
route.query.code &&
typeof route.query.code === 'string' &&
route.query.state &&
typeof route.query.state === 'string'
) {
console.log('SEND');
loggingIn.value = true;
const { success } = await oidcLoginUser(
route.query.code,
route.query.state
);
loggingIn.value = false;
if (success) {
await navigateTo({ path: '/' });
}
}
const form = useForm({
validationSchema: toTypedSchema(loginSchema),
});
const onSubmit = form.handleSubmit(async (values) => {
if (loggingIn.value) {
return;
}
loggingIn.value = true;
const { success } = await loginUser(values.email, values.password);
if (success) {
await navigateTo({ path: '/' });
}
loggingIn.value = false;
});
async function oidcClicked() {
if (loggingIn.value) {
return;
}
loggingIn.value = true;
const result = await getRedirectUrl();
if (result.success) {
await navigateTo(result.url, {
external: true,
});
} else {
loggingIn.value = false;
}
}
</script>
<template>
<Card class="w-full max-w-sm border-0 md:border">
<CardHeader>
<CardTitle class="text-2xl">Login</CardTitle>
<CardDescription>
Enter your email and password to log in to your account.
</CardDescription>
</CardHeader>
<CardContent>
<form id="login-form" class="grid gap-4" @submit.prevent="onSubmit">
<FormField v-slot="{ componentField }" name="email">
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
v-bind="componentField"
id="email"
type="email"
placeholder="name@example.com"
autocomplete="off"
/>
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="password">
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input
v-bind="componentField"
id="password"
type="password"
autocomplete="off"
/>
</FormControl>
<FormMessage />
</FormItem>
</FormField>
</form>
</CardContent>
<CardFooter class="flex-col gap-2">
<Button
type="submit"
class="w-full"
form="login-form"
:disabled="loggingIn"
>Log in</Button
>
<Button
class="w-full"
variant="outline"
:disabled="!OPEN_ID || loggingIn"
@click="oidcClicked"
>OpenID Connect</Button
>
<NuxtLink to="/register" class="w-full">
<Button class="w-full" variant="ghost">Register instead</Button>
</NuxtLink>
</CardFooter>
</Card>
</template>