Skip to content

Commit 05d4681

Browse files
author
Vianpyro
committed
Refactor registration component to redirect to the login page after successful registration
1 parent 213aeac commit 05d4681

File tree

2 files changed

+37
-44
lines changed

2 files changed

+37
-44
lines changed

components/authentication/Register.vue

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,47 @@
2121
</template>
2222

2323
<script setup>
24-
import { useRouter } from 'vue-router';
24+
import { ref } from 'vue';
2525
26-
const router = useRouter();
27-
</script>
26+
const username = ref('');
27+
const email = ref('');
28+
const password = ref('');
29+
const passwordConfirm = ref('');
2830
29-
<script>
30-
export default {
31-
data() {
32-
return {
33-
username: '',
34-
email: '',
35-
password: '',
36-
passwordConfirm: ''
37-
};
31+
const props = defineProps({
32+
goToLogin: {
33+
type: Function,
3834
},
39-
methods: {
40-
register() {
41-
if (this.password !== this.passwordConfirm) {
42-
console.error('Passwords do not match');
43-
return;
44-
}
35+
});
36+
37+
const register = async () => {
38+
if (password.value !== passwordConfirm.value) {
39+
console.error('Passwords do not match');
40+
return;
41+
}
42+
43+
try {
44+
const response = await fetch('http://localhost:5000/auth/register', {
45+
method: 'POST',
46+
headers: { 'Content-Type': 'application/json' },
47+
body: JSON.stringify({
48+
username: username.value,
49+
email: email.value,
50+
password: password.value,
51+
}),
52+
});
4553
46-
fetch('http://localhost:5000/auth/register', {
47-
method: 'POST',
48-
headers: { 'Content-Type': 'application/json' },
49-
body: JSON.stringify({
50-
username: this.username,
51-
email: this.email,
52-
password: this.password
53-
}),
54-
})
55-
.then(response => {
56-
if (response.ok) {
57-
return response.json();
58-
} else {
59-
throw new Error('Registration failed');
60-
}
61-
})
62-
.then(data => {
63-
console.log('Registration successful:', data);
64-
router.push('/');
65-
})
66-
.catch(error => {
67-
console.error('Error:', error);
68-
});
54+
if (!response.ok) {
55+
throw new Error('Registration failed');
6956
}
70-
},
71-
}
57+
58+
const data = await response.json();
59+
console.log('Registration successful:', data);
60+
props.goToLogin();
61+
} catch (error) {
62+
console.error('Error:', error);
63+
}
64+
};
7265
</script>
7366

7467
<style scoped>

pages/login.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<a id="go-to-login" @click="goToRegister">Don't have an account? <span class="link-color">Register</span></a>
66
</section>
77
<section id="register" class="hidden">
8-
<Register />
8+
<Register :goToLogin="goToLogin" />
99
<a id="go-to-register" @click="goToLogin">Already have an account? <span class="link-color">Login</span></a>
1010
</section>
1111
</div>

0 commit comments

Comments
 (0)