feat: nuxt basics
This commit is contained in:
2
front-nuxt/app/app.config.ts
Normal file
2
front-nuxt/app/app.config.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export default defineAppConfig({
|
||||
})
|
||||
@@ -1,6 +1,5 @@
|
||||
<template>
|
||||
<div>
|
||||
<NuxtRouteAnnouncer />
|
||||
<NuxtWelcome />
|
||||
</div>
|
||||
<NuxtLayout>
|
||||
<NuxtPage />
|
||||
</NuxtLayout>
|
||||
</template>
|
||||
|
||||
14
front-nuxt/app/error.vue
Normal file
14
front-nuxt/app/error.vue
Normal file
@@ -0,0 +1,14 @@
|
||||
<script setup lang="ts">
|
||||
import type { NuxtError } from "#app";
|
||||
|
||||
const { error } = defineProps({
|
||||
error: Object as () => NuxtError,
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<h1>{{ error?.statusCode }}</h1>
|
||||
<NuxtLink to="/">Go back home</NuxtLink>
|
||||
</div>
|
||||
</template>
|
||||
6
front-nuxt/app/layouts/auth.vue
Normal file
6
front-nuxt/app/layouts/auth.vue
Normal file
@@ -0,0 +1,6 @@
|
||||
<template>
|
||||
<header>
|
||||
Loging header actions
|
||||
</header>
|
||||
<slot/>
|
||||
</template>
|
||||
7
front-nuxt/app/layouts/default.vue
Normal file
7
front-nuxt/app/layouts/default.vue
Normal file
@@ -0,0 +1,7 @@
|
||||
<template>
|
||||
<main>
|
||||
<header>Header</header>
|
||||
<slot />
|
||||
<footer>Footer</footer>
|
||||
</main>
|
||||
</template>
|
||||
8
front-nuxt/app/middleware/auth.global.ts
Normal file
8
front-nuxt/app/middleware/auth.global.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
const userStore = useUserStore();
|
||||
console.log(
|
||||
">> Global Middleware from ",
|
||||
from.query,
|
||||
userStore.profile,
|
||||
);
|
||||
});
|
||||
3
front-nuxt/app/middleware/login.ts
Normal file
3
front-nuxt/app/middleware/login.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
console.log(">> Middleware from ", from.query);
|
||||
})
|
||||
5
front-nuxt/app/pages/about.vue
Normal file
5
front-nuxt/app/pages/about.vue
Normal file
@@ -0,0 +1,5 @@
|
||||
<template>
|
||||
<div>
|
||||
About
|
||||
</div>
|
||||
</template>
|
||||
3
front-nuxt/app/pages/index.vue
Normal file
3
front-nuxt/app/pages/index.vue
Normal file
@@ -0,0 +1,3 @@
|
||||
<template>
|
||||
<div>Main page</div>
|
||||
</template>
|
||||
26
front-nuxt/app/pages/login.vue
Normal file
26
front-nuxt/app/pages/login.vue
Normal file
@@ -0,0 +1,26 @@
|
||||
<script setup lang="ts">
|
||||
definePageMeta({
|
||||
layout: "auth",
|
||||
middleware: "login",
|
||||
});
|
||||
|
||||
const userStore = useUserStore();
|
||||
const setUpUser = () => {
|
||||
userStore.update({
|
||||
id: "1",
|
||||
name: "My name",
|
||||
roles: ["user", "manager"],
|
||||
});
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
Login form
|
||||
<div>
|
||||
User profile:
|
||||
{{ userStore.profile }}
|
||||
</div>
|
||||
<button @click="setUpUser">Login</button>
|
||||
</div>
|
||||
</template>
|
||||
10
front-nuxt/app/plugins/log-errors.client.ts
Normal file
10
front-nuxt/app/plugins/log-errors.client.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
nuxtApp.vueApp.config.errorHandler = (error, instance, info) => {
|
||||
// handle error, e.g. report to a service
|
||||
}
|
||||
|
||||
// Also possible
|
||||
nuxtApp.hook('vue:error', (error, instance, info) => {
|
||||
// handle error, e.g. report to a service
|
||||
})
|
||||
})
|
||||
46
front-nuxt/app/stores/user.ts
Normal file
46
front-nuxt/app/stores/user.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
type Role = "user" | "manager" | "admin";
|
||||
|
||||
interface ApiSession {
|
||||
exp?: number;
|
||||
accessToken: string;
|
||||
refreshToken?: string;
|
||||
}
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
roles: Role[];
|
||||
image?: string;
|
||||
name?: string;
|
||||
apiSession?: ApiSession;
|
||||
}
|
||||
|
||||
export const useUserStore = defineStore("user", {
|
||||
state: (): User => {
|
||||
const user = useCookie("userSession", {
|
||||
default: (): User => ({ id: "-1", roles: [] }),
|
||||
watch: false,
|
||||
});
|
||||
return user.value;
|
||||
},
|
||||
getters: {
|
||||
profile: (state) => ({
|
||||
id: state.id,
|
||||
name: state.name,
|
||||
image: state.image,
|
||||
roles: state.roles,
|
||||
}),
|
||||
},
|
||||
actions: {
|
||||
persist() {
|
||||
const user = useCookie("userSession", {
|
||||
default: (): User => ({ id: "-1", roles: [] }),
|
||||
watch: false,
|
||||
});
|
||||
user.value ||= this.profile;
|
||||
},
|
||||
update(user: User) {
|
||||
this.$state = user;
|
||||
this.persist();
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user