|
1 | 1 | <template> |
2 | | - <form id="login-form" @submit.prevent="login()"> |
| 2 | + <form id="login-form" @submit.prevent="login"> |
3 | 3 | <input type="email" id="login-email" name="email" v-model="email" placeholder="Email" required> |
4 | 4 | <input type="password" id="login-password" name="password" v-model="password" placeholder="Password" required> |
5 | 5 | <button type="submit">Login</button> |
6 | 6 | </form> |
7 | 7 | </template> |
8 | 8 |
|
9 | 9 | <script setup> |
| 10 | +import { ref } from 'vue'; |
10 | 11 | import { useRouter } from 'vue-router'; |
| 12 | +import { useCookie } from '#app'; |
11 | 13 |
|
12 | 14 | const router = useRouter(); |
13 | | -</script> |
14 | 15 |
|
15 | | -<script> |
16 | | -export default { |
17 | | - data() { |
18 | | - return { |
19 | | - email: '', |
20 | | - password: '' |
21 | | - }; |
22 | | - }, |
23 | | - methods: { |
24 | | - login() { |
25 | | - fetch('http://localhost:5000/auth/login', { |
26 | | - method: 'POST', |
27 | | - headers: { 'Content-Type': 'application/json' }, |
28 | | - body: JSON.stringify({ |
29 | | - email: this.email, |
30 | | - password: this.password, |
31 | | - }), |
32 | | - }) |
| 16 | +const email = ref(''); |
| 17 | +const password = ref(''); |
| 18 | +
|
| 19 | +const login = async () => { |
| 20 | + try { |
| 21 | + const data = await fetch('http://localhost:5000/auth/login', { |
| 22 | + method: 'POST', |
| 23 | + headers: { 'Content-Type': 'application/json' }, |
| 24 | + body: JSON.stringify({ |
| 25 | + email: email.value, // Correct usage of ref |
| 26 | + password: password.value, // Correct usage of ref |
| 27 | + }), |
| 28 | + }) |
33 | 29 | .then(response => { |
34 | | - if (response.ok) { |
35 | | - return response.json(); |
36 | | - } else { |
| 30 | + if (!response.ok) { |
37 | 31 | throw new Error('Login failed'); |
38 | 32 | } |
| 33 | + return response.json(); // Parse JSON only if response is OK |
39 | 34 | }) |
40 | | - .then(data => { |
41 | | - console.log('Login successful:', data); |
| 35 | + .then(data => data.data); |
42 | 36 |
|
43 | | - // Create or update session cookies |
44 | | - const accessToken = useCookie('access_token'); |
45 | | - const refreshToken = useCookie('refresh_token'); |
46 | | - accessToken.value = data.access_token; |
47 | | - refreshToken.value = data.refresh_token; |
| 37 | + // Use cookies to store tokens |
| 38 | + const accessToken = useCookie('access_token', { |
| 39 | + maxAge: 60 * 60 * 1, // 1 hour |
| 40 | + path: '/', |
| 41 | + }); |
48 | 42 |
|
49 | | - router.push('/'); |
50 | | - }) |
51 | | - .catch(error => { |
52 | | - console.error('Error:', error); |
53 | | - }); |
54 | | - } |
55 | | - }, |
56 | | -} |
| 43 | + const refreshToken = useCookie('refresh_token', { |
| 44 | + maxAge: 60 * 60 * 24 * 30, // 30 days |
| 45 | + path: '/', |
| 46 | + }); |
| 47 | +
|
| 48 | + accessToken.value = data.access_token; |
| 49 | + refreshToken.value = data.refresh_token; |
| 50 | +
|
| 51 | + // Navigate to home page |
| 52 | + router.push('/'); |
| 53 | + } catch (error) { |
| 54 | + console.error('Error:', error); |
| 55 | + } |
| 56 | +}; |
57 | 57 | </script> |
0 commit comments