✨ feat (auth): implement global authentication middleware
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
const { $pinia } = useNuxtApp()
|
||||
|
||||
if (import.meta.client) {
|
||||
const userStore = useUserStore($pinia)
|
||||
|
||||
console.log('currRole', userStore.userRole)
|
||||
console.log('isAuth', userStore.isAuthenticated)
|
||||
if (!userStore.isAuthenticated) {
|
||||
return navigateTo('/auth/login')
|
||||
}
|
||||
|
||||
const allowedRoles = to.meta.roles as string[] | undefined
|
||||
if (allowedRoles && !allowedRoles.includes(userStore.userRole)) {
|
||||
return navigateTo('/unauthorized')
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -1,7 +0,0 @@
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
// TODO: change this to actual api
|
||||
const user = true
|
||||
if (!user) {
|
||||
return navigateTo('/auth/login')
|
||||
}
|
||||
})
|
||||
@@ -1,3 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
definePageMeta({
|
||||
roles: ['sys', 'doc'],
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>detail pasien</div>
|
||||
</template>
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
definePageMeta({
|
||||
roles: ['sys', 'doc'],
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>edit pasien</div>
|
||||
</template>
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
definePageMeta({
|
||||
roles: ['sys', 'doc'],
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<FlowPatientAdd />
|
||||
</template>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
definePageMeta({
|
||||
roles: ['system', 'doctor'],
|
||||
roles: ['sys', 'doc'],
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
// app/types/index.d.ts
|
||||
import type { Pinia } from 'pinia'
|
||||
|
||||
declare module '#app' {
|
||||
interface NuxtApp {
|
||||
$pinia: Pinia
|
||||
}
|
||||
}
|
||||
|
||||
declare module '@vue/runtime-core' {
|
||||
interface ComponentCustomProperties {
|
||||
$pinia: Pinia
|
||||
}
|
||||
}
|
||||
|
||||
export {}
|
||||
Reference in New Issue
Block a user