-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c186b9
commit 6322fd3
Showing
5 changed files
with
111 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { defineNuxtRouteMiddleware, navigateTo } from '#app'; | ||
import type { RouteLocationRaw } from 'vue-router'; | ||
import { useSanctumConfig } from '../composables/useSanctumConfig'; | ||
import { useSanctumAuth } from '../composables/useSanctumAuth'; | ||
|
||
export default defineNuxtRouteMiddleware((to) => { | ||
const options = useSanctumConfig(); | ||
const { isAuthenticated } = useSanctumAuth(); | ||
|
||
const [homePage, loginPage] = [ | ||
options.redirect.onGuestOnly, | ||
options.redirect.onAuthOnly, | ||
]; | ||
|
||
if (homePage === false) { | ||
throw new Error( | ||
'You must define onGuestOnly route when using global middleware.' | ||
); | ||
} | ||
|
||
if (loginPage === false) { | ||
throw new Error( | ||
'You must define onAuthOnly route when using global middleware.' | ||
); | ||
} | ||
|
||
if (isAuthenticated.value === true) { | ||
if (to.path === loginPage) { | ||
return navigateTo(homePage, { replace: true }); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (to.path === loginPage || to.meta.excludeFromSanctum === true) { | ||
return; | ||
} | ||
|
||
if ( | ||
options.globalMiddleware.allow404WithoutAuth && | ||
to.matched.length === 0 | ||
) { | ||
return; | ||
} | ||
|
||
const redirect: RouteLocationRaw = { path: loginPage }; | ||
|
||
if (options.redirect.keepRequestedRoute) { | ||
redirect.query = { redirect: to.fullPath }; | ||
} | ||
|
||
return navigateTo(redirect, { replace: true }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters