Change Audiens

This commit is contained in:
Randa Firman Putra
2025-06-28 06:15:50 +07:00
parent baf9965d64
commit 37d083ec31
18 changed files with 906 additions and 318 deletions

View File

@@ -7,7 +7,7 @@ export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Define public paths that don't require authentication
const publicPaths = ['/', '/login', '/register'];
const publicPaths = ['/'];
const isPublicPath = publicPaths.includes(pathname);
// Check if the path is an API route or static file
@@ -19,13 +19,35 @@ export async function middleware(request: NextRequest) {
return NextResponse.next();
}
// If trying to access public route with token
// If trying to access public route with valid token, redirect to dashboard
if (token && isPublicPath) {
return NextResponse.next();
try {
await jwtVerify(
token,
new TextEncoder().encode(process.env.JWT_SECRET || 'your-secret-key')
);
return NextResponse.redirect(new URL('/dashboard', request.url));
} catch (error) {
// If token is invalid, clear it and stay on public page
const response = NextResponse.next();
response.cookies.set('token', '', {
expires: new Date(0),
path: '/',
httpOnly: true,
secure: false,
sameSite: 'lax'
});
return response;
}
}
// If the path is protected (dashboard routes) and user is not logged in, redirect to home
if (pathname.startsWith('/dashboard') && !token) {
return NextResponse.redirect(new URL('/', request.url));
}
// If the path is protected and user is logged in, verify token
if (!isPublicPath && token) {
if (pathname.startsWith('/dashboard') && token) {
try {
// Verify the token
await jwtVerify(