Skip to content

Commit

Permalink
Internal: Vue: Redirect to login form on PHP session expiration - ref…
Browse files Browse the repository at this point in the history
…s BT#21825
  • Loading branch information
christianbeeznest committed Jun 30, 2024
1 parent df69804 commit 8bfa73a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
29 changes: 14 additions & 15 deletions assets/vue/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import assignments from "./assignments"
import links from "./links"
import glossary from "./glossary"
import { useSecurityStore } from "../store/securityStore"
import securityService from "../services/securityService"
import MyCourseList from "../views/user/courses/List.vue"
import MySessionList from "../views/user/sessions/SessionsCurrent.vue"
import MySessionListPast from "../views/user/sessions/SessionsPast.vue"
Expand Down Expand Up @@ -167,23 +168,21 @@ const router = createRouter({
],
})

router.beforeEach((to, from, next) => {
if (to.matched.some((record) => record.meta.requiresAuth)) {
const securityStore = useSecurityStore()

//console.log('requiresAuth');
// this route requires auth, check if logged in
// if it is not, redirect to login page.
if (securityStore.isAuthenticated) {
next()
} else {
next({
path: "/login",
query: { redirect: to.fullPath },
})
router.beforeEach(async (to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
try {
const response = await securityService.checkSession()
const isAuthenticated = response.isAuthenticated
if (isAuthenticated) {
next()
} else {
next({ name: 'Login', query: { redirect: to.fullPath } })
}
} catch (error) {
console.error('Error checking session:', error)
next({ name: 'Login', query: { redirect: to.fullPath } })
}
} else {
//console.log('next');
next() // make sure to always call next()!
}
})
Expand Down
13 changes: 11 additions & 2 deletions assets/vue/services/securityService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import baseService from "./baseService"
import baseService from "./baseService";

/**
* @param {string} login
Expand All @@ -11,9 +11,18 @@ async function login({ login, password, _remember_me }) {
username: login,
password,
_remember_me,
})
});
}

/**
* Checks the status of the user's session.
* @returns {Promise<Object>}
*/
async function checkSession() {
return await baseService.get('/check-session')
}

export default {
login,
checkSession,
}
10 changes: 10 additions & 0 deletions src/CoreBundle/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@ public function loginJson(Request $request, EntityManager $entityManager, Settin

return new JsonResponse($data, Response::HTTP_OK, [], true);
}

#[Route('/check-session', name: 'check_session', methods: ['GET'])]
public function checkSession(): JsonResponse
{
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
return new JsonResponse(['isAuthenticated' => true]);
}

return new JsonResponse(['isAuthenticated' => false]);
}
}

0 comments on commit 8bfa73a

Please sign in to comment.