diff --git a/src/views/Login.vue b/src/views/Login.vue
index dc7363e..c2cfabd 100644
--- a/src/views/Login.vue
+++ b/src/views/Login.vue
@@ -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');
}
diff --git a/src/views/Register.vue b/src/views/Register.vue
index 9ce0491..563711a 100644
--- a/src/views/Register.vue
+++ b/src/views/Register.vue
@@ -21,7 +21,7 @@
- สมัครสมาชิค
+ สมัครสมาชิค
@@ -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');
}
}
}
};
-
\ No newline at end of file