-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11a2f4a
commit 307f0e6
Showing
12 changed files
with
140 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// axios.js | ||
import axios from 'axios'; | ||
|
||
const baseURL = 'http://localhost:3000/api'; // Define the base URL here | ||
|
||
const instance = axios.create({ | ||
baseURL, // Use the base URL here | ||
withCredentials: true, // Include credentials with requests | ||
}); | ||
|
||
export { instance, baseURL }; // Export both the instance and base URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,51 @@ | ||
<template> | ||
<div> | ||
<div v-if="!isAuthenticated" class="bruh"> | ||
<div v-if="!isAuthenticated" class="login-container"> | ||
<h1>Login</h1> | ||
<div> | ||
<form @submit.prevent="login"> | ||
<label for="username">Email</label> | ||
<input v-model="username" name="username" placeholder="username" type="text" /> | ||
<label for="password">Password</label> | ||
<input v-model="password" name="password" type="password" /> | ||
<button>Login</button> | ||
</form> | ||
|
||
<button @click="redirectToSignUp()">Sign up</button> | ||
</div> | ||
<button class="button google" @click="redirectToGoogleSignIn">Sign in with Google</button> | ||
</div> | ||
|
||
<div v-if="isAuthenticated"> | ||
<h1>You are currently logged in</h1> | ||
<button @click="logout()">Logout</button> | ||
<div v-if="isAuthenticated" class="logged-in-container"> | ||
<h1>Welcome, {{ user.email }}</h1> | ||
<button @click="logout">Logout</button> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { mapState } from 'vuex'; | ||
import axios from 'axios'; | ||
import { mapState } from 'vuex'; // Importing Vuex to access state | ||
import { baseURL } from '../axios'; // Importing the base URL from axios | ||
export default { | ||
name: 'LoginComponent', | ||
computed: { | ||
...mapState(['isAuthenticated', 'user']), | ||
}, | ||
data() { | ||
return { | ||
username: '', | ||
password: '', | ||
}; | ||
...mapState(['isAuthenticated', 'user']), // Accessing authentication state and user data from Vuex store | ||
}, | ||
methods: { | ||
async login() { | ||
try { | ||
await this.$store.dispatch('login', { username: this.username, password: this.password }); | ||
this.$router.push('/'); | ||
} catch (error) { | ||
console.error('Login error:', error); | ||
} | ||
}, | ||
redirectToSignUp() { | ||
this.$router.push('/signup'); | ||
redirectToGoogleSignIn() { | ||
// Redirecting to the Google login URL using the imported base URL | ||
window.location.href = `${baseURL}/login/google`; | ||
}, | ||
async logout() { | ||
try { | ||
await this.$store.dispatch('logout'); | ||
} catch (error) { | ||
console.error('Logout error:', error); | ||
} | ||
}, | ||
async testAPI() { | ||
try { | ||
const response = await axios.get('http://localhost:3000/api/test'); | ||
console.log(response.data); | ||
await this.$store.dispatch('logout'); // Dispatching the logout action to Vuex | ||
// Optionally, redirect to login page after logging out | ||
this.$router.push('/login'); | ||
} catch (error) { | ||
console.error('Error calling test API:', error); | ||
console.error('Logout error:', error); // Logging any errors that occur during logout | ||
} | ||
}, | ||
}, | ||
created() { | ||
mounted() { | ||
// Check if the user is authenticated when the component is mounted | ||
this.$store.dispatch('checkAuth'); | ||
this.testAPI(); | ||
}, | ||
}; | ||
</script> | ||
|
||
<style> | ||
<style scoped> | ||
.login-container, .logged-in-container { | ||
text-align: center; | ||
margin-top: 20px; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// src/router/routes/accountRoutes.js | ||
import Login from '@/views/Login.vue'; | ||
import SignUp from '@/views/SignUp.vue'; | ||
|
||
export default [ | ||
{ | ||
path: '/login', | ||
name: 'Login', | ||
component: Login, | ||
}, | ||
{ | ||
path: '/signup', | ||
name: 'SignUp', | ||
component: SignUp, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Login from '../views/Login.vue'; | ||
import SignUp from '../views/SignUp.vue'; | ||
|
||
export default [ | ||
{ | ||
path: '/login', | ||
name: 'Login', | ||
component: Login, | ||
}, | ||
{ | ||
path: '/signup', | ||
name: 'SignUp', | ||
component: SignUp, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import DashboardWorld from '../views/Dashboard.vue'; | ||
|
||
export default [ | ||
{ | ||
path: '/', | ||
name: 'dashboard', | ||
component: DashboardWorld, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// src/router/routes/generalRoutes.js | ||
import AboutMe from '@/views/AboutMe.vue'; | ||
import Learn from '@/views/Learn.vue'; | ||
import Type from '@/views/Type.vue'; | ||
|
||
export default [ | ||
{ | ||
path: '/about-me', | ||
name: 'AboutMe', | ||
component: AboutMe | ||
}, | ||
{ | ||
path: '/learn', | ||
name: 'Learn', | ||
component: Learn | ||
}, | ||
{ | ||
path: '/type', | ||
name: 'Type', | ||
component: Type | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,38 @@ | ||
import { createRouter, createWebHistory } from 'vue-router' | ||
import DashboardWorld from '../views/Dashboard.vue' | ||
import ListsWorld from '../views/Lists.vue' | ||
import SettingsWorld from '../views/Settings.vue' | ||
import AboutMe from '../views/AboutMe.vue' | ||
import Learn from '../views/Learn.vue' | ||
import Type from '../views/Type.vue' | ||
import Login from '../views/Login.vue' | ||
import SignUp from '../views/SignUp.vue' | ||
// src/router/index.js | ||
import { createRouter, createWebHistory } from 'vue-router'; | ||
import store from '@/store'; // Vuex store for authentication state | ||
|
||
// Import route groups | ||
import dashboardRoutes from './dashboardRoutes'; | ||
import accountRoutes from './accountRoutes'; | ||
import generalRoutes from './generalRoutes'; | ||
|
||
// Combine routes | ||
const routes = [ | ||
{ | ||
path: '/', | ||
name: 'dashboard', | ||
component: DashboardWorld | ||
}, | ||
{ | ||
path: '/Lists', | ||
name: 'Lists', | ||
component: ListsWorld | ||
}, | ||
{ | ||
path: '/Settings', | ||
name: 'Settings', | ||
component: SettingsWorld | ||
}, | ||
{ | ||
path: '/About-me', | ||
name: 'About Me', | ||
component: AboutMe | ||
}, | ||
{ | ||
path: '/Learn', | ||
name: 'Learn', | ||
component: Learn | ||
}, | ||
{ | ||
path: '/Type', | ||
name: 'Type', | ||
component: Type | ||
}, | ||
{ | ||
path: '/Login', | ||
name: 'Login', | ||
component: Login | ||
}, | ||
{ | ||
path: '/SignUp', | ||
name: 'SignUp', | ||
component: SignUp | ||
}, | ||
] | ||
...dashboardRoutes, | ||
...accountRoutes, | ||
...generalRoutes | ||
]; | ||
|
||
const router = createRouter({ | ||
history: createWebHistory(process.env.BASE_URL), | ||
routes | ||
}) | ||
routes, | ||
}); | ||
|
||
// Navigation guard for protected routes | ||
router.beforeEach((to, from, next) => { | ||
const isAuthenticated = store.state.isAuthenticated; // Get auth status from Vuex | ||
|
||
if (to.matched.some(record => record.meta.requiresAuth)) { | ||
// Route requires auth and user is not authenticated | ||
if (!isAuthenticated) { | ||
next({ name: 'Login' }); // Redirect to login page | ||
} else { | ||
next(); // Proceed to route | ||
} | ||
} else { | ||
next(); // Proceed to route | ||
} | ||
}); | ||
|
||
export default router | ||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters