|
21 | 21 | </template> |
22 | 22 |
|
23 | 23 | <script setup> |
24 | | -import { useRouter } from 'vue-router'; |
| 24 | +import { ref } from 'vue'; |
25 | 25 |
|
26 | | -const router = useRouter(); |
27 | | -</script> |
| 26 | +const username = ref(''); |
| 27 | +const email = ref(''); |
| 28 | +const password = ref(''); |
| 29 | +const passwordConfirm = ref(''); |
28 | 30 |
|
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, |
38 | 34 | }, |
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 | + }); |
45 | 53 |
|
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'); |
69 | 56 | } |
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 | +}; |
72 | 65 | </script> |
73 | 66 |
|
74 | 67 | <style scoped> |
|
0 commit comments