Skip to content

Commit 89b84dd

Browse files
author
Vianpyro
committed
Enhance authentication forms with v-model binding and submit handling for login and registration
1 parent d9590a4 commit 89b84dd

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
<template>
2-
<form id="login-form">
3-
<input type="email" id="login-email" name="email" placeholder="Email" required autofocus>
4-
<input type="password" id="login-password" name="password" placeholder="Password" required>
2+
<form id="login-form" @submit.prevent="login()">
3+
<input type="email" id="login-email" name="email" v-model="email" placeholder="Email" required>
4+
<input type="password" id="login-password" name="password" v-model="password" placeholder="Password" required>
55
<button type="submit">Login</button>
66
</form>
77
</template>
8+
9+
<script>
10+
export default {
11+
data() {
12+
return {
13+
email: '',
14+
password: ''
15+
};
16+
},
17+
methods: {
18+
login() {
19+
fetch('http://localhost:5000/auth/login', {
20+
method: 'GET',
21+
headers: { 'Content-Type': 'application/json' },
22+
body: JSON.stringify({
23+
email: this.email,
24+
password: this.password,
25+
}),
26+
})
27+
.then(response => {
28+
if (response.ok) {
29+
return response.json();
30+
} else {
31+
throw new Error('Login failed');
32+
}
33+
})
34+
.then(data => {
35+
console.log('Login successful:', data);
36+
})
37+
.catch(error => {
38+
console.error('Error:', error);
39+
});
40+
}
41+
},
42+
}
43+
</script>

components/authentication/Register.vue

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
<template>
2-
<form id="register-form">
3-
<input type="text" id="register-username" name="username" placeholder="Username" required>
4-
<input type="email" id="register-email" name="email" placeholder="Email" required>
2+
<form id="register-form" @submit.prevent="register">
3+
<input type="text" id="register-username" name="username" v-model="username" placeholder="Username" required>
4+
<input type="email" id="register-email" name="email" v-model="email" placeholder="Email" required>
55

66
<div>
7-
<input type="password" id="register-password" name="password" placeholder="Password" required>
7+
<input type="password" id="register-password" name="password" v-model="password" placeholder="Password" required>
88
<input class="show-password-check" id="show-password-input" type="checkbox" tabindex="-1">
99
<label class="show-password" for="show-password-input" title="Show Password"></label>
1010
<progress id="password-strength" class="hidden"></progress>
1111
</div>
1212

1313
<div>
14-
<input type="password" id="register-password-confirm" name="password-confirm"
15-
placeholder="Confirm password" required>
14+
<input type="password" id="register-password-confirm" name="password-confirm" v-model="passwordConfirm" placeholder="Confirm password" required>
1615
<input class="show-password-check" id="show-password-confirm-input" type="checkbox" tabindex="-1">
1716
<label class="show-password" for="show-password-confirm-input" title="Show Password"></label>
1817
</div>
@@ -21,6 +20,50 @@
2120
</form>
2221
</template>
2322

23+
<script>
24+
export default {
25+
data() {
26+
return {
27+
username: '',
28+
email: '',
29+
password: '',
30+
passwordConfirm: ''
31+
};
32+
},
33+
methods: {
34+
register() {
35+
if (this.password !== this.passwordConfirm) {
36+
console.error('Passwords do not match');
37+
return;
38+
}
39+
40+
fetch('http://localhost:5000/auth/register', {
41+
method: 'POST',
42+
headers: { 'Content-Type': 'application/json' },
43+
body: JSON.stringify({
44+
username: this.username,
45+
email: this.email,
46+
password: this.password
47+
}),
48+
})
49+
.then(response => {
50+
if (response.ok) {
51+
return response.json();
52+
} else {
53+
throw new Error('Registration failed');
54+
}
55+
})
56+
.then(data => {
57+
console.log('Registration successful:', data);
58+
})
59+
.catch(error => {
60+
console.error('Error:', error);
61+
});
62+
}
63+
},
64+
}
65+
</script>
66+
2467
<style scoped>
2568
#password-strength {
2669
appearance: none;

pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// Import RecipeCard component
1919
import RecipeCard from '@/components/RecipeCard.vue';
2020
21-
// Fetch recipes from your API
21+
// Fetch recipes from the API
2222
const { data: recipes, pending, error } = useFetch('http://localhost:5000/recipe/all', {
2323
params: { language_code: 'fr' },
2424
});

0 commit comments

Comments
 (0)