REFACTOR(repo): simplify project structure
- Move services/nextjs to nextjs/ - Move deploy/docker/Dockerfile.prod to Dockerfile - Add GitHub Actions workflows (ci.yml, build.yml) - Remove deploy/, services/, scripts/ folders
This commit is contained in:
71
nextjs/middleware.ts
Normal file
71
nextjs/middleware.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { verifyToken } from './src/shared/lib/auth'
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl
|
||||
|
||||
// Public paths that don't require authentication
|
||||
const publicPaths = [
|
||||
'/',
|
||||
'/home',
|
||||
'/home/signIn',
|
||||
'/home/signUp',
|
||||
'/home/share',
|
||||
'/api/auth/login',
|
||||
'/api/auth/register',
|
||||
'/api/health',
|
||||
'/api/metadata',
|
||||
]
|
||||
|
||||
// Check if the current path is public
|
||||
const isPublicPath = publicPaths.some(path => pathname.startsWith(path))
|
||||
|
||||
// Allow public paths
|
||||
if (isPublicPath) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
// Check for authentication on protected paths
|
||||
if (pathname.startsWith('/documents')) {
|
||||
// Get token from cookie
|
||||
const token = request.cookies.get('auth-token')?.value
|
||||
|
||||
if (!token) {
|
||||
// Redirect to home page if no token
|
||||
const url = new URL('/home', request.url)
|
||||
return NextResponse.redirect(url)
|
||||
}
|
||||
|
||||
// Verify token
|
||||
const payload = verifyToken(token)
|
||||
|
||||
if (!payload) {
|
||||
// Invalid token, clear cookie and redirect
|
||||
const url = new URL('/home', request.url)
|
||||
const response = NextResponse.redirect(url)
|
||||
response.cookies.delete('auth-token')
|
||||
return response
|
||||
}
|
||||
|
||||
// Token is valid, continue to the requested page
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
// Allow all other paths by default
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
// Configure which paths this middleware should run on
|
||||
export const config = {
|
||||
matcher: [
|
||||
/*
|
||||
* Match all request paths except for the ones starting with:
|
||||
* - _next/static (static files)
|
||||
* - _next/image (image optimization files)
|
||||
* - favicon.ico (favicon file)
|
||||
* - public folder
|
||||
*/
|
||||
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user