Pedro Gomes 1bc837e606 MAI CALL - auth v0.2
# O que mudou
1 Schema: failedAttempts + lockedUntil em User; migration auth_v0_2_lockout aplicada; crypto.ts com hashSecret/verifySecret (Node scrypt nativo, zero deps)
2 packages/api/src/auth.ts — authenticateCredential com lockout de 5 tentativas
3 Seed reescrito: admin hashed admin1234, operadores hashed 1111/2222/3333
4 Porta das traseiras fechada: AUTH_DEV_AUTOLOGIN ignorado quando NODE_ENV=production, em ambas as apps
5 operator-pwa: Credentials provider usa PIN + allowedRoles:['OPERATOR']; cookies fieldops-op.*
6 Picker em 2 estados: lista → teclado PIN (botões grandes, dots de progresso, mensagem de erro sem dar pistas)
7 admin-web: Auth.js completo (auth.config, auth.ts, route handler, middleware, /login page, AUTH_SECRET no env) com cookies fieldops-admin.*
8 scripts/auth-smoke.ts (11/11 ✓); .env.example e README atualizados
2026-05-30 11:54:38 +01:00

100 lines
3.6 KiB
TypeScript

import NextAuth from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { prisma } from '@repo/db';
import { authenticateCredential } from '@repo/api';
import type { SessionUser } from '@repo/api'; // eslint-disable-line @typescript-eslint/no-unused-vars
import { env } from '../env';
import { authConfig } from './auth.config';
/**
* ============================================================================
* Auth.js v5 — PLACEHOLDER configuration for the scaffold phase.
* ============================================================================
*
* The Credentials provider below accepts ANY email that exists in the User
* table (seeded by `pnpm db:seed`). NO PASSWORD CHECK is performed. This is
* deliberately minimal — just enough to populate the tRPC context with a real
* Auth.js session — and MUST be replaced with real authentication before any
* non-dev deployment.
*
* Auto sign-in
* ------------
* See `resolveUser()` below. When AUTH_DEV_AUTOLOGIN=true, server-side code
* that has no session falls back to the seeded admin user. This is a back
* door and is gated by an explicit env flag whose default in .env.example is
* FALSE.
*
* !!! NEVER set AUTH_DEV_AUTOLOGIN=true in production. !!!
*
* In production with AUTH_DEV_AUTOLOGIN unset/false, requests without a
* signed Auth.js session resolve to user=null, and protectedProcedure throws
* 401.
* ============================================================================
*/
export const { handlers, auth, signIn, signOut } = NextAuth({
...authConfig,
secret: env.AUTH_SECRET,
providers: [
Credentials({
name: 'Operador (PIN)',
credentials: {
email: { label: 'Email', type: 'text' },
pin: { label: 'PIN', type: 'password' },
},
async authorize(credentials) {
const email = credentials?.email;
const pin = credentials?.pin;
if (typeof email !== 'string' || typeof pin !== 'string') return null;
const u = await authenticateCredential({
email,
secret: pin,
allowedRoles: ['OPERATOR'],
});
if (!u) return null;
return {
id: u.id,
email: u.email,
name: u.email,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
role: u.role as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tenantId: u.tenantId as any,
};
},
}),
],
});
/**
* Resolve the current user for server-side code (RSC, route handlers, tRPC).
* Single chokepoint that combines the real Auth.js session with the dev-only
* auto-login fallback. Application code MUST use this and not call `auth()`
* directly when it expects to honour AUTH_DEV_AUTOLOGIN.
*/
export async function resolveUser(): Promise<SessionUser | null> {
const session = await auth();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const u = session?.user as any;
if (u?.id && u?.tenantId) {
return { id: u.id, email: u.email, role: u.role, tenantId: u.tenantId };
}
const autologinAllowed = env.AUTH_DEV_AUTOLOGIN && process.env.NODE_ENV !== 'production';
if (autologinAllowed) {
// Dev back door. Disabled in production even if the env flag is set.
const admin = await prisma.user.findFirst({ where: { email: 'admin@demo.local' } });
if (admin) {
return {
id: admin.id,
email: admin.email,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
role: admin.role as any,
tenantId: admin.tenantId,
};
}
}
return null;
}