This commit is contained in:
SRP-mango 2023-09-30 11:18:42 +07:00
commit c59eaff193
2 changed files with 58 additions and 5 deletions

View file

@ -45,8 +45,34 @@ export default {
},
methods: {
login() {
if (this.input.username !== '' || this.input.password !== '') {
if (this.input.username !== '' && this.input.password !== '') {
console.log('Authenticated: Checking with Backend');
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) => {
console.log(data.success);
sessionStorage.setItem('current_user', data.user.username);
this.$router.push({ name: 'home' });
})
.catch((err) => {
console.log(err);
// Handle the error, e.g., display an error message to the user
});
} else {
console.log('Username and Password cannot be empty');
}

View file

@ -21,7 +21,7 @@
</v-form>
</v-container>
<v-btn @click.prevent="login">สมครสมาช</v-btn>
<v-btn @click.prevent="register">สมครสมาช</v-btn>
</v-main>
</v-app>
@ -47,15 +47,42 @@
};
},
methods: {
login() {
if (this.input.username !== '' || this.input.password !== '') {
register() {
if (this.input.username !== '' && ((this.input.password !='') && (this.input.password == this.input.passwordConfirm))) {
console.log('Authenticated: Checking with Backend');
fetch("http://localhost:5000/api/users/create", {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.input.username,
password: this.input.password,
email: "asdf",
isGoogleAccount: false
})
})
.then((res) => {
if(res.ok){
return res.json()
}
else{
return res.json().then(data => {throw Error(`${data.registerStatus}`) });
}
})
.then((data) => {
console.log(data.registerStatus)
this.$router.push({name : 'login'})
})
.catch((err) =>{
console.log(err)
})
console.log("fisnished fetch");
} else {
console.log('Username and Password cannot be empty');
}
}
}
};
</script>