From ad59cc3e54a75819a0d5c9456bebf5674a9e6736 Mon Sep 17 00:00:00 2001 From: philwing100 Date: Fri, 1 Nov 2024 10:27:51 -0500 Subject: [PATCH] Working login and logout with google --- public/index.html | 1 - src/components/Login.vue | 2 +- src/store.js | 29 ++++++++++++----------------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/public/index.html b/public/index.html index 34339fc..378bdd8 100644 --- a/public/index.html +++ b/public/index.html @@ -4,7 +4,6 @@ Sus - diff --git a/src/components/Login.vue b/src/components/Login.vue index 1ef1a99..2939f9d 100644 --- a/src/components/Login.vue +++ b/src/components/Login.vue @@ -24,7 +24,7 @@ export default { methods: { redirectToGoogleSignIn() { // Redirecting to the Google login URL using the imported base URL - window.location.href = `${baseURL}/login/google`; + window.location.href = `${baseURL}/auth/google`; }, async logout() { try { diff --git a/src/store.js b/src/store.js index 52010da..4c6dbbb 100644 --- a/src/store.js +++ b/src/store.js @@ -1,43 +1,38 @@ import { createStore } from 'vuex'; -import axios from './axios'; // Make sure to import your axios instance +import { instance as axios } from './axios'; // Import the axios instance const store = createStore({ state: { - user: null, // Store user data - isAuthenticated: false, // Authentication status + user: null, + isAuthenticated: false, }, mutations: { SET_USER(state, user) { + console.log('User authenticated:', user); state.user = user; state.isAuthenticated = !!user; }, LOGOUT(state) { + console.log('User logged out'); state.user = null; state.isAuthenticated = false; }, }, actions: { async checkAuth({ commit }) { - console.log("checking auth"); try { - const response = await axios.get('/check-auth', { withCredentials: true }); - commit('SET_USER', response.data.user); // Set user data from response + console.log('Checking authentication with axios instance:', axios); + const response = await axios.get('/auth/check-auth'); // Axios will use the baseURL from axios.js + commit('SET_USER', response.data.user); } catch (error) { - commit('LOGOUT'); // If error occurs, user is not authenticated - } - }, - async login({ commit }, credentials) { - try { - await axios.post('/login', credentials, { withCredentials: true }); - await this.dispatch('checkAuth'); // Re-check auth status after login - } catch (error) { - console.error('Login error:', error); + console.error('Not authenticated:', error); + commit('LOGOUT'); } }, async logout({ commit }) { try { - await axios.post('/logout', {}, { withCredentials: true }); - commit('LOGOUT'); // Commit mutation to log the user out + await axios.post('/auth/logout'); + commit('LOGOUT'); } catch (error) { console.error('Logout error:', error); }