From ece58ddb9385c2df75093673f692b998c50d095e Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Mon, 15 Apr 2024 15:56:02 +0200 Subject: [PATCH] Better login/logout actions --- docker-compose.yml | 2 -- frontend/src/App.vue | 8 ++++++- frontend/src/layouts/dashboard/NavBar.vue | 1 - frontend/src/router/index.js | 26 ++++++++--------------- frontend/src/stores/auth.store.js | 10 ++++----- requirements.txt | 1 + test_project/test_project/settings.py | 4 ++++ 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 90fa8da7a..ca6c9b327 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3' - services: redis: image: redis:5-alpine diff --git a/frontend/src/App.vue b/frontend/src/App.vue index fd34c44f9..461abade6 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -2,4 +2,10 @@ - + diff --git a/frontend/src/layouts/dashboard/NavBar.vue b/frontend/src/layouts/dashboard/NavBar.vue index 9f88ca714..1d5e865c0 100644 --- a/frontend/src/layouts/dashboard/NavBar.vue +++ b/frontend/src/layouts/dashboard/NavBar.vue @@ -313,7 +313,6 @@ function displayMenuItem(item) { async function logout() { getActivePinia()._s.forEach(async (store) => await store.$reset()) - router.push({ name: 'Login' }) } onMounted(() => { diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index 0013cb7e5..7efefe625 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -299,26 +299,18 @@ router.beforeEach((to, from, next) => { const previousPage = window.location.href sessionStorage.setItem('previousPage', previousPage) const authStore = useAuthStore() - authStore.initialize().then((res) => { - if (res === null) { - next({name: 'Login'}) + authStore.validateAccess() + if (to.meta.allowedRoles !== undefined) { + if (to.meta.allowedRoles.indexOf(authStore.authUser.role) === -1) { + next({ name: 'Dashboard' }) return } - authStore.validateAccess() - if (to.meta.allowedRoles !== undefined) { - if (to.meta.allowedRoles.indexOf(authStore.authUser.role) === -1) { - next({ name: 'Dashboard' }) - return - } - } - if (to.meta.requiresMailbox && !authStore.authUser.mailbox) { - next({ name: 'Dashboard' }) - } - next() - }) - } else { - next() + } + if (to.meta.requiresMailbox && !authStore.authUser.mailbox) { + next({ name: 'Dashboard' }) + } } + next() }) export default router diff --git a/frontend/src/stores/auth.store.js b/frontend/src/stores/auth.store.js index 530a898ea..3618e7026 100644 --- a/frontend/src/stores/auth.store.js +++ b/frontend/src/stores/auth.store.js @@ -9,7 +9,6 @@ import { UserManager } from 'oidc-client-ts' import repository from '@/api/repository' import accountApi from '@/api/account' import accountsApi from '@/api/accounts' -// import authApi from '@/api/auth' export const useAuthStore = defineStore('auth', () => { const authUser = ref({}) @@ -61,7 +60,7 @@ export const useAuthStore = defineStore('auth', () => { async function login() { try { - await manager.signinRedirect(); + await manager.signinRedirect() } catch (error) { console.error('Error logging in:', error) } @@ -71,13 +70,13 @@ export const useAuthStore = defineStore('auth', () => { try { const user = await manager.signinRedirectCallback() isAuthenticated.value = true - const previousPage = sessionStorage.getItem('previousPage'); + const previousPage = sessionStorage.getItem('previousPage') // Redirect the user to the previous page if available if (previousPage) { - window.location.href = previousPage; + window.location.href = previousPage } else { // Redirect to a default page if the previous page is not available - router.push({name : 'Dashboard'}); + router.push({ name: 'Dashboard' }) } return user } catch (error) { @@ -91,6 +90,7 @@ export const useAuthStore = defineStore('auth', () => { authUser.value = {} isAuthenticated.value = false //TODO: Call the logout callback of OIDC and log out from the IdP + manager.signoutRedirect() } async function updateAccount(data) { diff --git a/requirements.txt b/requirements.txt index 41e113b60..6b11a48aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,6 +14,7 @@ drf-spectacular uritemplate djangorestframework-simplejwt==5.3.1 django-oauth-toolkit +django-cors-headers passlib~=1.7.4 bcrypt # Requires libffi-dev and python-dev diff --git a/test_project/test_project/settings.py b/test_project/test_project/settings.py index 1b1751506..6bbf33ab2 100644 --- a/test_project/test_project/settings.py +++ b/test_project/test_project/settings.py @@ -67,6 +67,7 @@ INSTALLED_APPS = ( "ckeditor", "ckeditor_uploader", "oauth2_provider", + "corsheaders", "rest_framework", "rest_framework.authtoken", "drf_spectacular", @@ -115,6 +116,7 @@ MIDDLEWARE = ( "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.locale.LocaleMiddleware", "x_forwarded_for.middleware.XForwardedForMiddleware", + "corsheaders.middleware.CorsMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", @@ -168,6 +170,8 @@ ROOT_URLCONF = "test_project.urls" WSGI_APPLICATION = "test_project.wsgi.application" +CORS_ORIGIN_ALLOW_ALL = True + # Internationalization # https://docs.djangoproject.com/en/2.2/topics/i18n/