2023-09-01 03:10:33 +07:00
|
|
|
<template>
|
|
|
|
|
|
|
|
<v-app>
|
|
|
|
<top-bar :show-back-icon="true" :page-title="pageTitle" />
|
|
|
|
<v-main>
|
|
|
|
<v-container>
|
|
|
|
<v-form name="login-form">
|
|
|
|
<div class="mb-3">
|
|
|
|
<label for="username">Username: </label>
|
|
|
|
<input type="text" id="username" v-model="input.username" />
|
|
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
|
|
<label for="password">Password: </label>
|
|
|
|
<input type="password" id="password" v-model="input.password" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</v-form>
|
|
|
|
</v-container>
|
|
|
|
|
|
|
|
<v-btn @click.prevent="login">ลงชื่อเข้าใช้</v-btn>
|
2023-09-01 03:35:39 +07:00
|
|
|
<v-btn :to="{name: 'register'}">ฉันต้องการสมัครสมาชิก</v-btn>
|
2023-09-01 03:10:33 +07:00
|
|
|
|
|
|
|
</v-main>
|
|
|
|
</v-app>
|
|
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-09-01 03:35:39 +07:00
|
|
|
import {RouterLink} from 'vue-router';
|
2023-09-01 03:10:33 +07:00
|
|
|
import TopBar from '@/components/TopBar.vue';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
TopBar
|
|
|
|
},
|
|
|
|
name: 'Login',
|
|
|
|
data() {
|
|
|
|
return {
|
2023-09-01 03:35:39 +07:00
|
|
|
pageTitle: 'ลงชื่อเข้าใช้',
|
2023-09-01 03:10:33 +07:00
|
|
|
input: {
|
|
|
|
username: '',
|
|
|
|
password: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
login() {
|
2023-09-24 18:35:25 +07:00
|
|
|
if (this.input.username !== '' && this.input.password !== '') {
|
2023-09-01 03:10:33 +07:00
|
|
|
console.log('Authenticated: Checking with Backend');
|
2023-09-27 00:47:59 +07:00
|
|
|
fetch(`http://localhost:5000/api/users/login`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
username: this.input.username,
|
|
|
|
password: this.input.password,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (res.ok) {
|
|
|
|
return res.json();
|
|
|
|
} else {
|
|
|
|
throw Error(`Login failed (${res.status})`);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then((data) => {
|
2023-09-27 00:58:43 +07:00
|
|
|
console.log(data.success);
|
|
|
|
sessionStorage.setItem('current_user', data.user.username);
|
2023-09-27 00:47:59 +07:00
|
|
|
this.$router.push({ name: 'home' });
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
// Handle the error, e.g., display an error message to the user
|
|
|
|
});
|
2023-09-01 03:10:33 +07:00
|
|
|
} else {
|
|
|
|
console.log('Username and Password cannot be empty');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
</script>
|