feat: Scaffold new Nuxt.js application with initial pages, layouts, composables, and middleware.

This commit is contained in:
supalerk-ar66 2026-01-23 09:54:35 +07:00
parent ab3124628c
commit 9bfb852ad0
8 changed files with 113 additions and 48 deletions

View file

@ -1,7 +1,9 @@
// Middleware สำหรับตรวจสอบสิทธิ์การเข้าถึงหน้าเว็บ (Authentication Guard)
export default defineNuxtRouteMiddleware((to) => {
const { isAuthenticated, user } = useAuth()
// Pages that are accessible only when NOT logged in (Auth pages)
// รายชื่อหน้าสำหรับ Guest (ห้าม User ที่ Login แล้วเข้า)
// เช่น หน้า Login, Register
const authPages = [
'/auth/login',
'/auth/register',
@ -9,13 +11,11 @@ export default defineNuxtRouteMiddleware((to) => {
'/auth/reset-password'
]
// Pages that are accessible as public landing
// Note: /courses and /discovery (now in browse/) might be public depending on logic,
// but let's assume browse pages are public or handled separately.
// For now, we list the root.
// รายชื่อหน้าที่เข้าถึงได้โดยไม่ต้อง Login (Public Pages)
const publicPages = ['/', '/courses', '/browse', '/browse/discovery']
// 1. If user is authenticated and tries to access login/register (Keep landing page accessible)
// กรณีที่ 1: ผู้ใช้ Login แล้ว แต่พยายามเข้าหน้า Login/Register
// ระบบจะดีดกลับไปหน้า Dashboard ตาม Role ของผู้ใช้
if (isAuthenticated.value && authPages.includes(to.path)) {
const role = user.value?.role?.code
if (role === 'ADMIN') return navigateTo('/admin', { replace: true })
@ -23,8 +23,8 @@ export default defineNuxtRouteMiddleware((to) => {
return navigateTo('/dashboard', { replace: true })
}
// 2. If user is NOT authenticated and tries to access a page that has this middleware applied
// and is NOT one of the public or auth pages.
// กรณีที่ 2: ผู้ใช้ยังไม่ Login แต่พยายามเข้าหน้าที่ต้อง Login (Protected Pages)
// (หน้าอื่น ๆ ที่ไม่ได้อยู่ใน publicPages และ authPages)
if (!isAuthenticated.value && !authPages.includes(to.path) && !publicPages.includes(to.path)) {
return navigateTo('/auth/login', { replace: true })
}