update: separate login logic to loginview
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, watch } from 'vue';
|
import { ref, watch } from 'vue';
|
||||||
import { login } from '../../lib/api';
|
|
||||||
import { useRouter } from 'vue-router';
|
import { useRouter } from 'vue-router';
|
||||||
import { useAuthStore } from '../../stores/auth';
|
|
||||||
|
const emit = defineEmits(['login-submit']);
|
||||||
|
|
||||||
const username = ref('');
|
const username = ref('');
|
||||||
const password = ref('');
|
const password = ref('');
|
||||||
@@ -11,8 +11,6 @@ const router = useRouter();
|
|||||||
const usernameError = ref('');
|
const usernameError = ref('');
|
||||||
const passwordError = ref('');
|
const passwordError = ref('');
|
||||||
|
|
||||||
const authStore = useAuthStore();
|
|
||||||
|
|
||||||
function validateUsername() {
|
function validateUsername() {
|
||||||
if (!username.value) {
|
if (!username.value) {
|
||||||
usernameError.value = '使用者名稱為必填。';
|
usernameError.value = '使用者名稱為必填。';
|
||||||
@@ -47,7 +45,7 @@ watch(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const onSubmit = async () => {
|
const submit = () => {
|
||||||
validateUsername();
|
validateUsername();
|
||||||
validatePassword();
|
validatePassword();
|
||||||
|
|
||||||
@@ -55,20 +53,12 @@ const onSubmit = async () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
emit('login-submit', { username: username.value, password: password.value });
|
||||||
const response = await login(username.value, password.value);
|
}
|
||||||
const { jwt } = response;
|
|
||||||
authStore.setJwt(jwt);
|
|
||||||
alert('Login successful!');
|
|
||||||
router.push('/profile');
|
|
||||||
} catch (error) {
|
|
||||||
alert("Login failed: " + error.message);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<form @submit.prevent="onSubmit">
|
<form @submit.prevent="submit">
|
||||||
<div class="ts-box ts-content">
|
<div class="ts-box ts-content">
|
||||||
<div class="ts-header is-large is-center-aligned has-bottom-spaced">登入</div>
|
<div class="ts-header is-large is-center-aligned has-bottom-spaced">登入</div>
|
||||||
<div class="ts-wrap is-vertical">
|
<div class="ts-wrap is-vertical">
|
||||||
|
|||||||
+20
-3
@@ -1,10 +1,27 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import LoginForm from '../components/Login/LoginForm.vue';
|
import LoginForm from '../components/Login/LoginForm.vue';
|
||||||
|
import { useRouter } from 'vue-router';
|
||||||
|
import { useAuthStore } from '../stores/auth';
|
||||||
|
import { login } from '../lib/api';
|
||||||
|
|
||||||
|
const router = useRouter();
|
||||||
|
const authStore = useAuthStore();
|
||||||
|
|
||||||
|
const onSubmit = async ({ username, password }) => {
|
||||||
|
try {
|
||||||
|
const response = await login(username, password);
|
||||||
|
const { jwt } = response;
|
||||||
|
authStore.setJwt(jwt);
|
||||||
|
alert('Login successful!');
|
||||||
|
router.push('/profile');
|
||||||
|
} catch (error) {
|
||||||
|
alert("Login failed: " + error.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="ts-app-center">
|
<div class="ts-app-center">
|
||||||
<LoginForm />
|
<LoginForm @login-submit="onSubmit" />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user