Skip to content

A self-hosted media server and file management system

route auth

aminnausin263f75a843e8
diff --git a/resources/js/router/index.js b/resources/js/router/index.js
index e32e5b8e..f739abd0 100644
--- a/resources/js/router/index.js
+++ b/resources/js/router/index.js
@@ -3,7 +3,10 @@ import VideoView from '../views/VideoView.vue'
import HistoryView from '../views/HistoryView.vue'
import ProfileView from '../views/ProfileView.vue'
import LoginView from "../views/LoginView.vue";
-import Layout from "../components/layout/Layout.vue";
+import { useAuthStore } from "../stores/AuthStore";
+import { logout } from "../service/auth";
+import { storeToRefs } from "pinia";
+
const router = createRouter({
history: createWebHistory(),
@@ -22,23 +25,42 @@ const router = createRouter({
name:'register',
component: LoginView
},
+ {
+ path:'/logout',
+ name:'logout',
+ component: {
+ async beforeRouteEnter(to, from, next) {
+ let destination = {
+ path: from.path || "/",
+ query: from.query,
+ params: from.params
+ };
+
+ const {error} = await logout();
+
+ if(error){
+ toastr['error']('Unable to logout');
+ destination = {path: '/'};
+ } else {
+ localStorage.clear('auth-token');
+ }
+ next(destination);
+ }
+ }
+ },
{
path:'/history',
name:'history',
+ meta: {protected: true},
component: HistoryView
},
{
path:'/profile',
name:'profile',
- component: VideoView
- },
- {
- path:'/:category+',
- name:'category',
- component: VideoView
+ component: ProfileView
},
{
- path:'/:category+/:folder',
+ path:'/:category/:folder?',
name:'home',
component: VideoView
},
@@ -46,4 +68,24 @@ const router = createRouter({
]
})
+router.beforeEach((to, from, next) => {
+ const authStore = useAuthStore();
+ const { userData } = storeToRefs(authStore);
+
+ // Check if user is authenticated (has user data)
+ // and not accessing login/register routes
+ if ( to.meta?.protected && !userData && to.path !== '/login' && to.path !== '/register') {
+ next({
+ path: '/login',
+ query: {
+ redirect: to.fullPath,
+ }
+ });
+
+ return;
+ }
+
+ next()
+})
+
export default router;
\ No newline at end of file