Skip to content

Commit

Permalink
Working login and logout with google
Browse files Browse the repository at this point in the history
  • Loading branch information
philwing100 committed Nov 1, 2024
1 parent 307f0e6 commit ad59cc3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
1 change: 0 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="utf-8">
<title>Sus</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap">
<link rel="manifest" href="/manifest.json">
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 12 additions & 17 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down

0 comments on commit ad59cc3

Please sign in to comment.