Skip to content

Commit 307f0e6

Browse files
committed
Updates to login and store.js auth
1 parent 11a2f4a commit 307f0e6

File tree

12 files changed

+140
-132
lines changed

12 files changed

+140
-132
lines changed

src/axios.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// axios.js
2+
import axios from 'axios';
3+
4+
const baseURL = 'http://localhost:3000/api'; // Define the base URL here
5+
6+
const instance = axios.create({
7+
baseURL, // Use the base URL here
8+
withCredentials: true, // Include credentials with requests
9+
});
10+
11+
export { instance, baseURL }; // Export both the instance and base URL

src/components/CalendarComponents/DailyCalendar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export default {
174174
text-overflow: ellipsis;
175175
}
176176
177-
.event:hover {
177+
.event:hover {
178178
background-color: #388e3c;
179179
}
180180

src/components/Login.vue

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,51 @@
11
<template>
22
<div>
3-
<div v-if="!isAuthenticated" class="bruh">
3+
<div v-if="!isAuthenticated" class="login-container">
44
<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>
166
</div>
177

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>
2111
</div>
2212
</div>
2313
</template>
2414

2515
<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
2818
2919
export default {
3020
name: 'LoginComponent',
3121
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
3923
},
4024
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`;
5128
},
5229
async logout() {
5330
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');
6334
} catch (error) {
64-
console.error('Error calling test API:', error);
35+
console.error('Logout error:', error); // Logging any errors that occur during logout
6536
}
6637
},
6738
},
68-
created() {
39+
mounted() {
40+
// Check if the user is authenticated when the component is mounted
6941
this.$store.dispatch('checkAuth');
70-
this.testAPI();
7142
},
7243
};
7344
</script>
7445

75-
<style>
46+
<style scoped>
47+
.login-container, .logged-in-container {
48+
text-align: center;
49+
margin-top: 20px;
50+
}
7651
</style>

src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { createApp } from 'vue';
22
import App from './App.vue';
33
import store from './store'; // Ensure the correct path
44
import router from './router'; // Assuming you have a router
5+
import axios from 'axios';
6+
7+
axios.defaults.withCredentials = true;
58

69
const app = createApp(App);
710

src/router/accountRoutes.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// src/router/routes/accountRoutes.js
2+
import Login from '@/views/Login.vue';
3+
import SignUp from '@/views/SignUp.vue';
4+
5+
export default [
6+
{
7+
path: '/login',
8+
name: 'Login',
9+
component: Login,
10+
},
11+
{
12+
path: '/signup',
13+
name: 'SignUp',
14+
component: SignUp,
15+
},
16+
];

src/router/authRoutes.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Login from '../views/Login.vue';
2+
import SignUp from '../views/SignUp.vue';
3+
4+
export default [
5+
{
6+
path: '/login',
7+
name: 'Login',
8+
component: Login,
9+
},
10+
{
11+
path: '/signup',
12+
name: 'SignUp',
13+
component: SignUp,
14+
},
15+
];

src/router/dashboardRoutes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import DashboardWorld from '../views/Dashboard.vue';
2+
3+
export default [
4+
{
5+
path: '/',
6+
name: 'dashboard',
7+
component: DashboardWorld,
8+
},
9+
];

src/router/generalRoutes.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// src/router/routes/generalRoutes.js
2+
import AboutMe from '@/views/AboutMe.vue';
3+
import Learn from '@/views/Learn.vue';
4+
import Type from '@/views/Type.vue';
5+
6+
export default [
7+
{
8+
path: '/about-me',
9+
name: 'AboutMe',
10+
component: AboutMe
11+
},
12+
{
13+
path: '/learn',
14+
name: 'Learn',
15+
component: Learn
16+
},
17+
{
18+
path: '/type',
19+
name: 'Type',
20+
component: Type
21+
}
22+
];

src/router/index.js

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,38 @@
1-
import { createRouter, createWebHistory } from 'vue-router'
2-
import DashboardWorld from '../views/Dashboard.vue'
3-
import ListsWorld from '../views/Lists.vue'
4-
import SettingsWorld from '../views/Settings.vue'
5-
import AboutMe from '../views/AboutMe.vue'
6-
import Learn from '../views/Learn.vue'
7-
import Type from '../views/Type.vue'
8-
import Login from '../views/Login.vue'
9-
import SignUp from '../views/SignUp.vue'
1+
// src/router/index.js
2+
import { createRouter, createWebHistory } from 'vue-router';
3+
import store from '@/store'; // Vuex store for authentication state
104

5+
// Import route groups
6+
import dashboardRoutes from './dashboardRoutes';
7+
import accountRoutes from './accountRoutes';
8+
import generalRoutes from './generalRoutes';
9+
10+
// Combine routes
1111
const routes = [
12-
{
13-
path: '/',
14-
name: 'dashboard',
15-
component: DashboardWorld
16-
},
17-
{
18-
path: '/Lists',
19-
name: 'Lists',
20-
component: ListsWorld
21-
},
22-
{
23-
path: '/Settings',
24-
name: 'Settings',
25-
component: SettingsWorld
26-
},
27-
{
28-
path: '/About-me',
29-
name: 'About Me',
30-
component: AboutMe
31-
},
32-
{
33-
path: '/Learn',
34-
name: 'Learn',
35-
component: Learn
36-
},
37-
{
38-
path: '/Type',
39-
name: 'Type',
40-
component: Type
41-
},
42-
{
43-
path: '/Login',
44-
name: 'Login',
45-
component: Login
46-
},
47-
{
48-
path: '/SignUp',
49-
name: 'SignUp',
50-
component: SignUp
51-
},
52-
]
12+
...dashboardRoutes,
13+
...accountRoutes,
14+
...generalRoutes
15+
];
5316

5417
const router = createRouter({
5518
history: createWebHistory(process.env.BASE_URL),
56-
routes
57-
})
19+
routes,
20+
});
21+
22+
// Navigation guard for protected routes
23+
router.beforeEach((to, from, next) => {
24+
const isAuthenticated = store.state.isAuthenticated; // Get auth status from Vuex
25+
26+
if (to.matched.some(record => record.meta.requiresAuth)) {
27+
// Route requires auth and user is not authenticated
28+
if (!isAuthenticated) {
29+
next({ name: 'Login' }); // Redirect to login page
30+
} else {
31+
next(); // Proceed to route
32+
}
33+
} else {
34+
next(); // Proceed to route
35+
}
36+
});
5837

59-
export default router
38+
export default router;

src/store.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createStore } from 'vuex';
2-
import axios from 'axios';
2+
import axios from './axios'; // Make sure to import your axios instance
33

44
const store = createStore({
55
state: {
@@ -18,25 +18,26 @@ const store = createStore({
1818
},
1919
actions: {
2020
async checkAuth({ commit }) {
21+
console.log("checking auth");
2122
try {
22-
const response = await axios.get('http://localhost:3000/api/check-auth');
23-
commit('SET_USER', response.data.user);
23+
const response = await axios.get('/check-auth', { withCredentials: true });
24+
commit('SET_USER', response.data.user); // Set user data from response
2425
} catch (error) {
25-
commit('LOGOUT');
26+
commit('LOGOUT'); // If error occurs, user is not authenticated
2627
}
2728
},
28-
async login({ commit }, credentials) {//!1Abcdefgh
29+
async login({ commit }, credentials) {
2930
try {
30-
await axios.post('http://localhost:3000/api/login', credentials);
31+
await axios.post('/login', credentials, { withCredentials: true });
3132
await this.dispatch('checkAuth'); // Re-check auth status after login
3233
} catch (error) {
3334
console.error('Login error:', error);
3435
}
3536
},
3637
async logout({ commit }) {
3738
try {
38-
await axios.post('http://localhost:3000/api/logout');
39-
commit('LOGOUT');
39+
await axios.post('/logout', {}, { withCredentials: true });
40+
commit('LOGOUT'); // Commit mutation to log the user out
4041
} catch (error) {
4142
console.error('Logout error:', error);
4243
}

src/views/Login.vue

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,16 @@
11
<template>
2-
<div class="bruh">
32
<LoginComponent />
4-
</div>
5-
<h1>Sign in</h1>
6-
<a class="button google" href="/login/federated/google">Sign in with Google</a>
73
</template>
84

95
<script>
106
import LoginComponent from '@/components/Login.vue';
11-
//Needs to also display the sign up with google
12-
137
148
export default {
159
name: 'LoginWorld',
1610
components: {
1711
LoginComponent
1812
},
1913
methods: {
20-
async login() {
21-
try {
22-
const response = await axios.post('http://localhost:3000/api/signup', {
23-
email: this.email,
24-
password: this.password
25-
});
26-
this.message = 'Signup successful! Please log in.';
27-
// Optionally, you can redirect the user to the login page
28-
setTimeout(() => {
29-
this.$router.push('/login');
30-
}, 2000); // Redirect after 2 seconds
31-
} catch (error) {
32-
console.log(error.response.data.errors);
33-
this.message = 'Error signing up: ' + (error.response.data.errors || error.message);
34-
}
35-
},
3614
}
3715
}
3816
</script>

vue.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ module.exports = defineConfig({
1212
},
1313
});
1414

15-
//For some reason this stuff doesn't work and I don't know why but its very goofy. All api calls need to be to the exact link
1615

1716

0 commit comments

Comments
 (0)