SÉCURITÉ 8→9: - lib/rate-limit.ts: Redis rate limiter (incr + expire) - chat/route: 15 req/min max - 6 routes IA: 20 req/min max (tags, title, labels, markdown, overview, summary) - link-preview: cache Redis 5min par URL+userId - auth-providers: rate limit login 5 req/5min UX 7.5→8.5: - search-modal: focus trap (Tab cycle + Shift+Tab) - search-modal: ref modal pour querySelector focusable - layout: skip-link 'Skip to content' (sr-only focus:not-sr-only) - note-actions: boutons h-8 w-8 → h-9 w-9 (36px → touch target) 0 erreur TS, 199/199 tests
81 lines
2.0 KiB
TypeScript
81 lines
2.0 KiB
TypeScript
import Google from 'next-auth/providers/google';
|
|
import Credentials from 'next-auth/providers/credentials';
|
|
import { z } from 'zod';
|
|
import bcrypt from 'bcryptjs';
|
|
import prisma from '@/lib/prisma';
|
|
import { rateLimit } from '@/lib/rate-limit';
|
|
|
|
export function buildAuthProviders() {
|
|
const providers = [];
|
|
|
|
if (process.env.AUTH_GOOGLE_ID && process.env.AUTH_GOOGLE_SECRET) {
|
|
const googlePrompt =
|
|
process.env.AUTH_GOOGLE_PROMPT === 'login' ? 'login' : 'select_account';
|
|
|
|
providers.push(
|
|
Google({
|
|
clientId: process.env.AUTH_GOOGLE_ID,
|
|
clientSecret: process.env.AUTH_GOOGLE_SECRET,
|
|
authorization: {
|
|
params: {
|
|
prompt: googlePrompt,
|
|
access_type: 'online',
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
|
|
providers.push(
|
|
Credentials({
|
|
async authorize(credentials) {
|
|
try {
|
|
const parsedCredentials = z
|
|
.object({ email: z.string().email(), password: z.string().min(6) })
|
|
.safeParse(credentials);
|
|
|
|
if (!parsedCredentials.success) {
|
|
return null;
|
|
}
|
|
|
|
const { email, password } = parsedCredentials.data;
|
|
|
|
const { allowed } = await rateLimit(`login:${email.toLowerCase()}`, 'login', 5, 300);
|
|
if (!allowed) {
|
|
return null;
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: email.toLowerCase() },
|
|
});
|
|
|
|
if (!user || !user.password) {
|
|
return null;
|
|
}
|
|
|
|
const passwordsMatch = await bcrypt.compare(password, user.password);
|
|
|
|
if (passwordsMatch) {
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
role: user.role,
|
|
};
|
|
}
|
|
|
|
return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
},
|
|
}),
|
|
);
|
|
|
|
return providers;
|
|
}
|
|
|
|
export function isGoogleAuthEnabled() {
|
|
return Boolean(process.env.AUTH_GOOGLE_ID && process.env.AUTH_GOOGLE_SECRET);
|
|
}
|