Skip to content

Commit 2025fcd

Browse files
authored
Merge pull request #18 from k2so-dev/17-available-data-in-middleware
fetch user before middleware
2 parents 92f8c01 + c7adcad commit 2025fcd

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

nuxt/app/app.vue

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
<script lang="ts" setup>
2-
const auth = useAuthStore();
3-
4-
if (auth.logged) {
5-
await auth.fetchUser();
6-
}
72
</script>
83

94
<template>

nuxt/app/middleware/role-admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default defineNuxtRouteMiddleware((to, from) => {
22
const auth = useAuthStore()
33

4-
if (auth.logged && !auth.hasRole('admin')) {
4+
if (!auth.logged || !auth.hasRole('admin')) {
55
const toast = useToast()
66

77
toast.add({

nuxt/app/middleware/role-user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default defineNuxtRouteMiddleware((to, from) => {
22
const auth = useAuthStore()
33

4-
if (auth.logged && !auth.hasRole('user')) {
4+
if (!auth.logged || !auth.hasRole('user')) {
55
const toast = useToast()
66

77
toast.add({

nuxt/app/middleware/verified.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
export default defineNuxtRouteMiddleware((to, from) => {
1+
import authMiddleware from "./auth";
2+
3+
export default defineNuxtRouteMiddleware(async (to, from) => {
4+
await authMiddleware(to, from);
5+
26
const auth = useAuthStore();
37

4-
if (auth.logged && auth.user.must_verify_email) {
8+
if (auth.user.must_verify_email) {
59
const toast = useToast();
610

711
toast.add({

nuxt/app/plugins/auth.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default defineNuxtPlugin(async (nuxtApp) => {
2+
const auth = useAuthStore();
3+
4+
if (auth.logged) {
5+
await auth.fetchUser();
6+
}
7+
})

0 commit comments

Comments
 (0)