Skip to content

Commit

Permalink
🐛 Fix auto login
Browse files Browse the repository at this point in the history
  • Loading branch information
MaloLebrin committed Feb 29, 2024
1 parent 84b2b4c commit c19ce14
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composables/authHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export default function authHook() {
}

function getCookie() {
return useCookie('userToken', { secure: true, sameSite: true, path: '', maxAge: 604800 })
const cookie = useCookie('userToken', { secure: true, sameSite: true, path: '', maxAge: 604800 })
return cookie.value
}

return {
Expand Down
7 changes: 6 additions & 1 deletion layouts/auth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ import EventModal from '~/components/Event/EventModal.vue'
import HeaderDashboard from '~/components/Header/HeaderDashboard.vue'
import MenuDrawer from '~/components/Menu/MenuDrawer.vue'
import UserMissingInfoModal from '~~/components/User/MissingInfoModal.vue'
import { useEventStore, useUiStore } from '~~/store'
import { useAuthStore, useEventStore, useUiStore } from '~~/store'
import { ModalNameEnum } from '~~/types'
const uiStore = useUiStore()
const authStore = useAuthStore()
const { resetUiModalState } = uiStore
const eventStore = useEventStore()
const { logWithToken } = authHook()
const isModalActive = (modalName: ModalNameEnum) => computed(() =>
uiStore.getUiModalState.isActive
Expand All @@ -84,6 +86,9 @@ function CloseResetModalState() {
}
onMounted(async () => {
if (authStore.getToken) {
await logWithToken(authStore.getToken)
}
const { startSEE } = notificationSSEHook()
startSEE()
})
Expand Down
19 changes: 14 additions & 5 deletions middleware/guardsMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { useJwt } from '@vueuse/integrations/useJwt'
import { useAuthStore } from '~~/store'

export default defineNuxtRouteMiddleware(async to => {
const { $toast } = useNuxtApp()
const authStore = useAuthStore()
const { logWithToken, getCookie } = authHook()
const { setToken, setJWTasUser } = authStore
const { getCookie } = authHook()

const cookieToken = getCookie()

if (authStore.getIsLoggedIn) {
return
}

if (to.meta.isAuth && !authStore.getIsLoggedIn) {
if (cookieToken.value) {
const jwt = await logWithToken(cookieToken.value)
if (jwt) {
return
if (cookieToken) {
setToken(cookieToken)
const { payload } = useJwt(cookieToken)

if (payload.value) {
setJWTasUser(payload.value)
}
return
}

$toast?.denied('Vous n\'êtes pas connecté')
Expand Down

0 comments on commit c19ce14

Please sign in to comment.