Checkpoint: Migration OAuth Manus vers authentification locale (80% complété) - Pages login/register créées, backend JWT implémenté, guides de déploiement ajoutés. Reste à corriger les doublons dans db.ts et finaliser AdminUsers.
This commit is contained in:
43
server/_core/auth.ts
Normal file
43
server/_core/auth.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import bcrypt from "bcrypt";
|
||||
import { sign, verify } from "jsonwebtoken";
|
||||
import { ENV } from "./env";
|
||||
|
||||
const SALT_ROUNDS = 10;
|
||||
|
||||
/**
|
||||
* Hashe un mot de passe avec bcrypt
|
||||
*/
|
||||
export async function hashPassword(password: string): Promise<string> {
|
||||
return bcrypt.hash(password, SALT_ROUNDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie un mot de passe contre son hash
|
||||
*/
|
||||
export async function verifyPassword(
|
||||
password: string,
|
||||
hash: string
|
||||
): Promise<boolean> {
|
||||
return bcrypt.compare(password, hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère un token JWT pour un utilisateur
|
||||
*/
|
||||
export function generateToken(userId: number): string {
|
||||
return sign({ userId }, ENV.jwtSecret, {
|
||||
expiresIn: "30d",
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie et décode un token JWT
|
||||
*/
|
||||
export function verifyToken(token: string): { userId: number } | null {
|
||||
try {
|
||||
const decoded = verify(token, ENV.jwtSecret) as { userId: number };
|
||||
return decoded;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user