Better login/logout actions

This commit is contained in:
Antoine Nguyen
2024-06-22 19:44:52 +02:00
committed by Spitfireap
parent 794512b69e
commit ece58ddb93
7 changed files with 26 additions and 26 deletions
-2
View File
@@ -1,5 +1,3 @@
version: '3'
services:
redis:
image: redis:5-alpine
+7 -1
View File
@@ -2,4 +2,10 @@
<router-view />
</template>
<script setup></script>
<script setup>
import { useAuthStore } from '@/stores'
const authStore = useAuthStore()
authStore.initialize()
</script>
@@ -313,7 +313,6 @@ function displayMenuItem(item) {
async function logout() {
getActivePinia()._s.forEach(async (store) => await store.$reset())
router.push({ name: 'Login' })
}
onMounted(() => {
+9 -17
View File
@@ -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
+5 -5
View File
@@ -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) {
+1
View File
@@ -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
+4
View File
@@ -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/