-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from manchenkoff/3-global-app-middleware
feat: implemented global middleware
- Loading branch information
Showing
11 changed files
with
173 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<script setup lang="ts"> | ||
import { useSanctumAuth } from '#imports'; | ||
interface ErrorProps { | ||
error?: { | ||
url: string; | ||
statusCode: number; | ||
statusMessage: string; | ||
message: string; | ||
description: string; | ||
data?: any; | ||
}; | ||
} | ||
const props = defineProps<ErrorProps>(); | ||
const { isAuthenticated, logout } = useSanctumAuth(); | ||
</script> | ||
|
||
<template> | ||
<NuxtLoadingIndicator /> | ||
|
||
<h1>Nuxt - Laravel Sanctum application sample</h1> | ||
|
||
<nav class="menu"> | ||
<NuxtLink to="/">Home</NuxtLink> | ||
<NuxtLink to="/login">Login</NuxtLink> | ||
<NuxtLink to="/logout">Logout</NuxtLink> | ||
<NuxtLink to="/profile">Profile</NuxtLink> | ||
<NuxtLink to="/welcome">Welcome</NuxtLink> | ||
|
||
<button v-if="isAuthenticated" @click="logout">Logout</button> | ||
</nav> | ||
|
||
<hr /> | ||
|
||
<strong>Error</strong> | ||
<p>{{ props.error }}</p> | ||
</template> | ||
|
||
<style scoped> | ||
.menu { | ||
display: flex; | ||
gap: 1rem; | ||
} | ||
</style> |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"extends": "./.nuxt/tsconfig.json", | ||
"exclude": ["../dist"], | ||
} |
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
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
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 |
---|---|---|
@@ -1,10 +1,4 @@ | ||
{ | ||
"extends": "./.nuxt/tsconfig.json", | ||
"exclude": [ | ||
"./node_modules", | ||
"./node_modules/nuxt/node_modules", | ||
"./dist", | ||
"./.output", | ||
"./playground", | ||
], | ||
"exclude": ["./node_modules", "./dist", "./playground"], | ||
} |