|
1 | 1 | <template> |
2 | 2 | <div> |
3 | | - <div v-if="!isAuthenticated" class="bruh"> |
| 3 | + <div v-if="!isAuthenticated" class="login-container"> |
4 | 4 | <h1>Login</h1> |
5 | | - <div> |
6 | | - <form @submit.prevent="login"> |
7 | | - <label for="username">Email</label> |
8 | | - <input v-model="username" name="username" placeholder="username" type="text" /> |
9 | | - <label for="password">Password</label> |
10 | | - <input v-model="password" name="password" type="password" /> |
11 | | - <button>Login</button> |
12 | | - </form> |
13 | | - |
14 | | - <button @click="redirectToSignUp()">Sign up</button> |
15 | | - </div> |
| 5 | + <button class="button google" @click="redirectToGoogleSignIn">Sign in with Google</button> |
16 | 6 | </div> |
17 | 7 |
|
18 | | - <div v-if="isAuthenticated"> |
19 | | - <h1>You are currently logged in</h1> |
20 | | - <button @click="logout()">Logout</button> |
| 8 | + <div v-if="isAuthenticated" class="logged-in-container"> |
| 9 | + <h1>Welcome, {{ user.email }}</h1> |
| 10 | + <button @click="logout">Logout</button> |
21 | 11 | </div> |
22 | 12 | </div> |
23 | 13 | </template> |
24 | 14 |
|
25 | 15 | <script> |
26 | | -import { mapState } from 'vuex'; |
27 | | -import axios from 'axios'; |
| 16 | +import { mapState } from 'vuex'; // Importing Vuex to access state |
| 17 | +import { baseURL } from '../axios'; // Importing the base URL from axios |
28 | 18 |
|
29 | 19 | export default { |
30 | 20 | name: 'LoginComponent', |
31 | 21 | computed: { |
32 | | - ...mapState(['isAuthenticated', 'user']), |
33 | | - }, |
34 | | - data() { |
35 | | - return { |
36 | | - username: '', |
37 | | - password: '', |
38 | | - }; |
| 22 | + ...mapState(['isAuthenticated', 'user']), // Accessing authentication state and user data from Vuex store |
39 | 23 | }, |
40 | 24 | methods: { |
41 | | - async login() { |
42 | | - try { |
43 | | - await this.$store.dispatch('login', { username: this.username, password: this.password }); |
44 | | - this.$router.push('/'); |
45 | | - } catch (error) { |
46 | | - console.error('Login error:', error); |
47 | | - } |
48 | | - }, |
49 | | - redirectToSignUp() { |
50 | | - this.$router.push('/signup'); |
| 25 | + redirectToGoogleSignIn() { |
| 26 | + // Redirecting to the Google login URL using the imported base URL |
| 27 | + window.location.href = `${baseURL}/login/google`; |
51 | 28 | }, |
52 | 29 | async logout() { |
53 | 30 | try { |
54 | | - await this.$store.dispatch('logout'); |
55 | | - } catch (error) { |
56 | | - console.error('Logout error:', error); |
57 | | - } |
58 | | - }, |
59 | | - async testAPI() { |
60 | | - try { |
61 | | - const response = await axios.get('http://localhost:3000/api/test'); |
62 | | - console.log(response.data); |
| 31 | + await this.$store.dispatch('logout'); // Dispatching the logout action to Vuex |
| 32 | + // Optionally, redirect to login page after logging out |
| 33 | + this.$router.push('/login'); |
63 | 34 | } catch (error) { |
64 | | - console.error('Error calling test API:', error); |
| 35 | + console.error('Logout error:', error); // Logging any errors that occur during logout |
65 | 36 | } |
66 | 37 | }, |
67 | 38 | }, |
68 | | - created() { |
| 39 | + mounted() { |
| 40 | + // Check if the user is authenticated when the component is mounted |
69 | 41 | this.$store.dispatch('checkAuth'); |
70 | | - this.testAPI(); |
71 | 42 | }, |
72 | 43 | }; |
73 | 44 | </script> |
74 | 45 |
|
75 | | -<style> |
| 46 | +<style scoped> |
| 47 | +.login-container, .logged-in-container { |
| 48 | + text-align: center; |
| 49 | + margin-top: 20px; |
| 50 | +} |
76 | 51 | </style> |
0 commit comments