NT Diesel
Layout doesn't change when using navigateTo in the nuxt middleware
this is my /auth/login page
this is my auth layout
<script setup>
definePageMeta({
middleware: ["require-unauth"],
layout: "auth",
});
</script>
<script setup>
definePageMeta({
middleware: ["require-unauth"],
layout: "auth",
});
</script>
<template>
<div class="relative min-h-[100dvh]">
<!-- start of bg of the auth pages -->
<div class="absolute h-full w-full">
<img src="/imgs/auth-bg.jpg" class="w-full h-full object-cover" />
<div class="w-full h-full absolute bg-FrameOne/30 inset-0"></div>
<img
src="/imgs/logo.png"
class="absolute top-2 left-2 w-[15%] object-cover aspect-square"
/>
</div>
<!-- end of bg of the auth pages -->
<slot />
</div>
</template>
<template>
<div class="relative min-h-[100dvh]">
<!-- start of bg of the auth pages -->
<div class="absolute h-full w-full">
<img src="/imgs/auth-bg.jpg" class="w-full h-full object-cover" />
<div class="w-full h-full absolute bg-FrameOne/30 inset-0"></div>
<img
src="/imgs/logo.png"
class="absolute top-2 left-2 w-[15%] object-cover aspect-square"
/>
</div>
<!-- end of bg of the auth pages -->
<slot />
</div>
</template>
54 replies
nav guard and redirection
have problem with my nuxt3 app i am making middle ware like this
and i have the default layout like this
and i have the index page like this
import { useMainStore } from "~/stores/main";
export default defineNuxtRouteMiddleware(async (to, from) => {
if (process.server) return;
const MainStore = useMainStore();
if (!MainStore.AccessToken) {
return navigateTo("/auth/login");
}
const config = useRuntimeConfig();
try {
const resp = await $fetch(`${config.public.serverApi}/login`, {
method: "post",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: {
fromRefresh: true,
},
});
MainStore.Login(resp.username, resp.accessToken);
return;
} catch (e) {
console.log(e.data.message);
return navigateTo("/auth/login");
}
});
import { useMainStore } from "~/stores/main";
export default defineNuxtRouteMiddleware(async (to, from) => {
if (process.server) return;
const MainStore = useMainStore();
if (!MainStore.AccessToken) {
return navigateTo("/auth/login");
}
const config = useRuntimeConfig();
try {
const resp = await $fetch(`${config.public.serverApi}/login`, {
method: "post",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: {
fromRefresh: true,
},
});
MainStore.Login(resp.username, resp.accessToken);
return;
} catch (e) {
console.log(e.data.message);
return navigateTo("/auth/login");
}
});
<script setup></script>
<template>
<div class="relative min-h-[100dvh]">
<!-- start of bg of the auth pages -->
<div class="absolute h-full w-full">
<img src="/imgs/auth-bg.jpg" class="w-full h-full object-cover" />
<div class="w-full h-full absolute bg-primary/30 inset-0"></div>
<img
src="/imgs/logo.png"
class="absolute top-2 left-2 w-[15%] object-cover aspect-square"
/>
</div>
<!-- end of bg of the auth pages -->
<slot />
</div>
</template>
<style scoped></style>
<script setup></script>
<template>
<div class="relative min-h-[100dvh]">
<!-- start of bg of the auth pages -->
<div class="absolute h-full w-full">
<img src="/imgs/auth-bg.jpg" class="w-full h-full object-cover" />
<div class="w-full h-full absolute bg-primary/30 inset-0"></div>
<img
src="/imgs/logo.png"
class="absolute top-2 left-2 w-[15%] object-cover aspect-square"
/>
</div>
<!-- end of bg of the auth pages -->
<slot />
</div>
</template>
<style scoped></style>
<script setup>
definePageMeta({
middleware: ["require-auth"],
// or middleware: 'auth'
});
</script>
<template>
<div></div>
</template>
<script setup>
definePageMeta({
middleware: ["require-auth"],
// or middleware: 'auth'
});
</script>
<template>
<div></div>
</template>
8 replies