Skip to content

Commit

Permalink
Better login/logout actions
Browse files Browse the repository at this point in the history
  • Loading branch information
tonioo committed Apr 15, 2024
1 parent 64dce8b commit 452ee13
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

services:
redis:
image: redis:5-alpine
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
<router-view />
</template>

<script setup></script>
<script setup>
import { useAuthStore } from '@/stores'
const authStore = useAuthStore()
authStore.initialize()
</script>
1 change: 0 additions & 1 deletion frontend/src/layouts/dashboard/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ function displayMenuItem(item) {
async function logout() {
getActivePinia()._s.forEach(async (store) => await store.$reset())
router.push({ name: 'Login' })
}
onMounted(() => {
Expand Down
26 changes: 9 additions & 17 deletions frontend/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
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) {
authStore.validateAccess()
if (to.meta.allowedRoles !== undefined) {
if (to.meta.allowedRoles.indexOf(authStore.authUser.role) === -1) {
next({ name: 'Dashboard' })
return
}
next()
})
} else {
next()
}
if (to.meta.requiresMailbox && !authStore.authUser.mailbox) {
next({ name: 'Dashboard' })
}
}
next()
})

export default router
10 changes: 5 additions & 5 deletions frontend/src/stores/auth.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test_project/test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"ckeditor",
"ckeditor_uploader",
"oauth2_provider",
"corsheaders",
"rest_framework",
"rest_framework.authtoken",
"drf_spectacular",
Expand Down Expand Up @@ -115,6 +116,7 @@
"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",
Expand Down Expand Up @@ -168,6 +170,8 @@

WSGI_APPLICATION = "test_project.wsgi.application"

CORS_ORIGIN_ALLOW_ALL = True

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

Expand Down

0 comments on commit 452ee13

Please sign in to comment.