init repo

This commit is contained in:
LeoMortari
2025-08-28 20:22:13 -03:00
commit 54fc9f400b
22 changed files with 913 additions and 0 deletions

100
src/auth/router.js Normal file
View File

@@ -0,0 +1,100 @@
import Cookies from "js-cookie";
import { createWebHistory, createRouter } from "vue-router";
import roles from "@/auth/roles";
import Videos from "@/routes/videos";
import Login from "@/routes/auth/Login.vue";
const getUserRoles = () => {
const rolesFromCookie = Cookies.get("user_roles"); // TODO: Tirar as permissões do usuário
return rolesFromCookie ? JSON.parse(rolesFromCookie) : [];
};
export const routes = [
{
path: "/login",
name: "Login",
component: Login,
meta: {
guest: true,
title: "Login",
},
},
{
path: "/videos",
component: Videos,
name: "Videos",
meta: {
requiresAuth: true,
title: "Vídeos",
permissions: [roles.VIDEOS_LIST],
},
},
{
path: "/:pathMatch(.*)*",
redirect: (to) => {
return Cookies.get("token") ? "/videos" : "/login";
},
},
{
path: "/",
redirect: (to) => {
console.log(Cookies);
return Cookies.get("token") ? "/videos" : "/login";
},
},
];
export const router = createRouter({
history: createWebHistory(),
routes,
});
export const isAuthenticated = () => {
return !!Cookies.get("token");
};
const hasPermission = (requiredPermissions = []) => {
if (!requiredPermissions.length) return true;
const userRoles = getUserRoles();
return requiredPermissions.some((permission) =>
userRoles.includes(permission)
);
};
router.beforeEach((to, from, next) => {
if (to.meta.title) {
document.title = `${to.meta.title} | ClipperIA`;
}
if (to.matched.some((record) => record.meta.requiresAuth)) {
if (!isAuthenticated()) {
next({
path: "/login",
query: { redirect: to.fullPath },
});
return;
}
const requiredPermissions = to.meta.permissions || [];
if (requiredPermissions.length > 0 && !hasPermission(requiredPermissions)) {
next({ path: "/unauthorized" });
return;
}
}
if (to.matched.some((record) => record.meta.guest) && isAuthenticated()) {
next({ path: "/" });
return;
}
next();
});