Added comprehensive improvements to the login and authentication flow: **UI Improvements:** - Add password visibility toggle (Eye/EyeOff icons) for password fields - Improve error message consistency with Dutch translations - Fix syntax error in login page try-catch block **Error Handling:** - Add user-friendly error messages for invalid credentials - Add specific error codes: invalid_credentials, email_not_confirmed - Improve error message for non-existent accounts during login - Handle duplicate email detection during signup with auto-switch to login mode **Middleware:** - Add /reset-password and /update-password to public routes - Fix redirect issue preventing access to password reset flow **Password Reset Flow:** - Verify password reset works for magic link users (can set initial password) - Ensure existing password users can reset their password - Both flows use the same update mechanism via Supabase **Documentation:** - Add comprehensive release notes in docs/RELEASE_AUTH_IMPROVEMENTS.md All changes maintain backward compatibility with existing magic link users. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
99 lines
2.5 KiB
TypeScript
99 lines
2.5 KiB
TypeScript
/**
|
|
* Next.js Middleware
|
|
*
|
|
* Handles authentication and route protection
|
|
*/
|
|
|
|
import { createServerClient } from '@supabase/ssr'
|
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
|
|
export async function middleware(request: NextRequest) {
|
|
let supabaseResponse = NextResponse.next({
|
|
request,
|
|
})
|
|
|
|
const supabase = createServerClient(
|
|
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
|
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
|
{
|
|
cookies: {
|
|
getAll() {
|
|
return request.cookies.getAll()
|
|
},
|
|
setAll(cookiesToSet) {
|
|
cookiesToSet.forEach(({ name, value }) =>
|
|
request.cookies.set(name, value)
|
|
)
|
|
supabaseResponse = NextResponse.next({
|
|
request,
|
|
})
|
|
cookiesToSet.forEach(({ name, value, options }) =>
|
|
supabaseResponse.cookies.set(name, value, options)
|
|
)
|
|
},
|
|
},
|
|
}
|
|
)
|
|
|
|
// Refresh session if expired - required for Server Components
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser()
|
|
|
|
const { pathname } = request.nextUrl
|
|
|
|
// Public routes that don't require authentication
|
|
const publicRoutes = [
|
|
'/',
|
|
'/login',
|
|
'/reset-password',
|
|
'/update-password',
|
|
'/auth/callback',
|
|
'/contact',
|
|
'/api/leads',
|
|
'/robots.txt',
|
|
'/sitemap.xml',
|
|
]
|
|
|
|
// Check if current path is public
|
|
const isPublicRoute = publicRoutes.some(route =>
|
|
pathname === route || pathname.startsWith(`${route}/`)
|
|
)
|
|
|
|
// Static files and Next.js internals
|
|
if (
|
|
pathname.startsWith('/_next') ||
|
|
pathname.startsWith('/static') ||
|
|
pathname.includes('.')
|
|
) {
|
|
return supabaseResponse
|
|
}
|
|
|
|
// Redirect to login if not authenticated and trying to access protected route
|
|
if (!user && !isPublicRoute) {
|
|
const redirectUrl = new URL('/login', request.url)
|
|
redirectUrl.searchParams.set('redirect', pathname)
|
|
return NextResponse.redirect(redirectUrl)
|
|
}
|
|
|
|
// Redirect to /epd/clients if authenticated and trying to access login
|
|
if (user && pathname === '/login') {
|
|
return NextResponse.redirect(new URL('/epd/clients', request.url))
|
|
}
|
|
|
|
return supabaseResponse
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
/*
|
|
* Match all request paths except:
|
|
* - _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)$).*)',
|
|
],
|
|
}
|