Feature/Auth: implement user authentication #3
@@ -8,6 +8,8 @@ import ItemsList from "../pages/ItemsList.vue";
|
|||||||
import ItemForm from "../pages/ItemForm.vue";
|
import ItemForm from "../pages/ItemForm.vue";
|
||||||
import index from "../pages/index.vue";
|
import index from "../pages/index.vue";
|
||||||
|
|
||||||
|
import { authStorage } from "../api/axios.ts"
|
||||||
|
|
||||||
// link path to the page component
|
// link path to the page component
|
||||||
const routes = [
|
const routes = [
|
||||||
{ path: "/", component: index },
|
{ path: "/", component: index },
|
||||||
@@ -26,24 +28,13 @@ const router = createRouter({
|
|||||||
// intercept before routing
|
// intercept before routing
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
|
|
||||||
const token = localStorage.getItem("token");
|
const token: string | null = authStorage.getAccessToken();
|
||||||
if(to.meta.requiresAuth && !token) { // if the page requires use to be signed in, they must have at least a token set
|
if(to.meta.requiresAuth && !token) { // if the page requires use to be signed in, they must have at least a token set
|
||||||
next("/login");
|
next("/login");
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
// TODO: if they have a token, but invalid, it will still send them to the page (the api will catch non-authorized though)
|
|
||||||
// maybe have a "validate token" from the api and refresh it if valid
|
|
||||||
/*
|
|
||||||
} else {
|
|
||||||
bool authorizedUser = authApi.refreshToken(token);
|
|
||||||
if(authorizedUser) {
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
next("/login");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user