sintonia_webapp/resources/js/pages/Login.vue
marcoc 03551436dc feat(fe login): finished login
ToDo: add timezone at login
2025-02-28 17:44:17 +01:00

118 lines
No EOL
2.9 KiB
Vue

<script setup lang="ts">
import {reactive} from 'vue';
import axios from "axios";
import {useAuthStore} from "@/stores/auth.store.ts";
import {useRouter} from "vue-router";
const data = reactive({
'username': null,
'password': null,
'loading': false,
});
const router = useRouter();
const onSubmit = () => {
if (!data.username || !data.password) return;
data.loading = true;
axios.post('/login', {
username: data.username,
password: data.password,
// csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content')
}).then((response) => {
if (response.status === 200) {
//const timezone = setTimezone(response.data.user);
const auth = useAuthStore();
const user = {
user: response.data.user,
password: data.password,
//timezone: timezone
}
auth.setUserData(user);
//To check data uncomment the timezone const, the timezone line in user object
// and the console.log below. Then comment router.push('/')
// console.log(auth.userData)
router.push('/');
}
}).catch((error) => {
console.log('Error: '+error);
});
}
const setTimezone = async (user): string => {
return await axios.get("http://localhost:8080/api/v2/preferences", {
auth: {
username: user.login,
password: data.password
}
}).then(res => {
res.data.forEach(item => {
if (item.keystr === 'user_timezone' && item.subjid === user.id) {
return item.valstr;
}
return import.meta.env.VITE_APP_TIMEZONE;
})
}).catch(error => {
console.log("Error: "+error);
return null;
})
}
const required = (v) => {
return !!v || $t('login.errors.required');
}
</script>
<template>
<v-container class="d-flex items-center justify-center">
<v-card class="mx-auto px-6 py-8" min-width="500">
<v-form
v-model="data.form"
@submit.prevent="onSubmit"
>
<v-card-item class="text-center">
<v-card-title>{{ $t('login.title') }}</v-card-title>
<v-card-subtitle>{{ $t('login.subtitle') }}</v-card-subtitle>
</v-card-item>
<v-card-text>
<v-text-field
v-model="data.username"
class="mb-2"
label="Username"
></v-text-field>
<v-text-field
v-model="data.password"
label="Password"
placeholder="Enter your password"
></v-text-field>
</v-card-text>
<v-card-actions>
<v-btn
:disabled="!data.username || !data.password"
:loading="data.loading"
color="primary"
size="large"
type="submit"
variant="elevated"
@click="onSubmit"
block
>
Sign In
</v-btn>
</v-card-actions>
</v-form>
</v-card>
</v-container>
</template>
<style scoped>
.v-container {
height: 100vh;
}
</style>