Passo 8 completo. Tudo verde. Sumário do que foi feito:
Novas páginas:
app/select-operator/page.tsx — Server Component; redireciona automaticamente se já há sessão; lista operadores via prisma direto (funciona mesmo sem sessão ativa)
app/select-operator/operator-picker.tsx — Client Component; tap → signIn('credentials', { email, redirect: false }) → redireciona para /
app/sign-out-button.tsx — botão "Trocar" que chama signOut → volta ao picker
middleware.ts atualizado — redireciona para /select-operator quando não há sessão e AUTH_DEV_AUTOLOGIN=false; skip automático se já logado; o picker não faz redirect se não há sessão (deixa carregar)
app/page.tsx atualizado — mostra chip com o email do utilizador atual + botão "Trocar" (necessário para o AC "header mostra op1@demo.local")
Correções de infraestrutura descobertas:
NODE_ENV="development" removido do .env — estava a forçar o runtime de dev no next build, quebrando a geração estática
pages/_error.tsx adicionado — override mínimo que previne o erro <Html> outside _document
@repo/storage adicionado a transpilePackages e AWS SDK marcado como serverExternalPackages
app/not-found.tsx + app/error.tsx adicionados para App Router
AC verificado: build de produção passa limpo em Next.js 15.3.9 com todas as rotas correctas. O fluxo demo (/ → picker → login → / mostra email) funciona via dev server.
39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
import NextAuth from 'next-auth';
|
|
import { authConfig } from './lib/auth.config';
|
|
|
|
// Edge-runtime middleware. Uses the edge-safe authConfig (no Credentials
|
|
// provider, no Prisma) — it only validates and refreshes the JWT cookie. The
|
|
// full auth config with the Credentials provider lives in lib/auth.ts and
|
|
// runs in the Node.js runtime via the route handlers.
|
|
const { auth } = NextAuth(authConfig);
|
|
|
|
export default auth((req) => {
|
|
const isLoggedIn = !!req.auth?.user;
|
|
// AUTH_DEV_AUTOLOGIN bypasses the picker redirect — resolveUser() handles
|
|
// the autologin fallback server-side; the middleware just stays out of the way.
|
|
const isAutologin = process.env['AUTH_DEV_AUTOLOGIN'] === 'true';
|
|
const { pathname } = req.nextUrl;
|
|
|
|
// On the picker itself: skip if already logged in.
|
|
if (pathname === '/select-operator') {
|
|
if (isLoggedIn) {
|
|
return Response.redirect(new URL('/', req.url));
|
|
}
|
|
return; // allow through
|
|
}
|
|
|
|
// Any other matched route: redirect to picker if unauthenticated and no autologin.
|
|
if (!isLoggedIn && !isAutologin) {
|
|
return Response.redirect(new URL('/select-operator', req.url));
|
|
}
|
|
});
|
|
|
|
export const config = {
|
|
matcher: [
|
|
// Run on every path except static assets, image optimization, and the
|
|
// PWA manifest. The Auth.js / tRPC API routes are excluded explicitly
|
|
// because they handle session resolution themselves.
|
|
'/((?!api/auth|api/trpc|_next/static|_next/image|favicon.ico|manifest.webmanifest|icon-.*\\.svg).*)',
|
|
],
|
|
};
|