Checkpoint: Correction de l'authentification locale : utilisation de sdk.createSessionToken() au lieu de jsonwebtoken pour compatibilité avec le système OAuth

This commit is contained in:
Manus Sandbox
2025-11-23 08:50:35 -05:00
parent ccb1484839
commit 645f6f6b5f
12 changed files with 2984 additions and 12 deletions

233
diagnose.sh Executable file
View File

@@ -0,0 +1,233 @@
#!/bin/bash
###############################################################################
# Script de diagnostic - Gestion des Formations Manager Itinova
# Vérifie la configuration et l'état de l'application
###############################################################################
set -e
# Couleurs
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ Diagnostic - Formation Manager Itinova ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
# Vérifier que nous sommes dans le bon répertoire
if [ ! -f "package.json" ]; then
echo -e "${RED}[ERREUR]${NC} Ce script doit être exécuté depuis la racine du projet"
exit 1
fi
# Charger les variables d'environnement
if [ -f .env ]; then
export $(cat .env | grep -v '^#' | xargs)
echo -e "${GREEN}[OK]${NC} Fichier .env trouvé"
else
echo -e "${RED}[ERREUR]${NC} Fichier .env non trouvé"
exit 1
fi
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo " 1. VÉRIFICATION DES VARIABLES D'ENVIRONNEMENT"
echo "═══════════════════════════════════════════════════════════════"
echo ""
# Vérifier DATABASE_URL
if [ -z "$DATABASE_URL" ]; then
echo -e "${RED}[ERREUR]${NC} DATABASE_URL n'est pas définie"
else
echo -e "${GREEN}[OK]${NC} DATABASE_URL est définie"
# Masquer le mot de passe
MASKED_URL=$(echo $DATABASE_URL | sed 's/:\/\/[^:]*:[^@]*@/:\/\/***:***@/')
echo " $MASKED_URL"
fi
# Vérifier JWT_SECRET
if [ -z "$JWT_SECRET" ]; then
echo -e "${RED}[ERREUR]${NC} JWT_SECRET n'est pas définie"
echo -e "${YELLOW}[INFO]${NC} L'authentification locale ne fonctionnera pas sans JWT_SECRET"
else
echo -e "${GREEN}[OK]${NC} JWT_SECRET est définie"
SECRET_LENGTH=${#JWT_SECRET}
if [ $SECRET_LENGTH -lt 32 ]; then
echo -e "${YELLOW}[WARN]${NC} JWT_SECRET est trop courte ($SECRET_LENGTH caractères, recommandé: 32+)"
else
echo -e "${GREEN}[OK]${NC} JWT_SECRET a une longueur suffisante ($SECRET_LENGTH caractères)"
fi
fi
# Vérifier NODE_ENV
if [ -z "$NODE_ENV" ]; then
echo -e "${YELLOW}[WARN]${NC} NODE_ENV n'est pas définie (défaut: development)"
else
echo -e "${GREEN}[OK]${NC} NODE_ENV = $NODE_ENV"
fi
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo " 2. VÉRIFICATION DE LA BASE DE DONNÉES"
echo "═══════════════════════════════════════════════════════════════"
echo ""
# Extraire les informations de connexion
DB_USER=$(echo $DATABASE_URL | sed -n 's/.*:\/\/\([^:]*\):.*/\1/p')
DB_PASS=$(echo $DATABASE_URL | sed -n 's/.*:\/\/[^:]*:\([^@]*\)@.*/\1/p')
DB_HOST=$(echo $DATABASE_URL | sed -n 's/.*@\([^:]*\):.*/\1/p')
DB_PORT=$(echo $DATABASE_URL | sed -n 's/.*:\([0-9]*\)\/.*/\1/p')
DB_NAME=$(echo $DATABASE_URL | sed -n 's/.*\/\([^?]*\).*/\1/p')
# Tester la connexion MySQL
if command -v mysql &> /dev/null; then
if mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASS -e "USE $DB_NAME;" 2>/dev/null; then
echo -e "${GREEN}[OK]${NC} Connexion à la base de données réussie"
# Vérifier les tables
TABLES=$(mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASS -D $DB_NAME -e "SHOW TABLES;" 2>/dev/null | tail -n +2)
if [ -z "$TABLES" ]; then
echo -e "${YELLOW}[WARN]${NC} Aucune table trouvée dans la base de données"
echo -e "${YELLOW}[INFO]${NC} Exécutez 'pnpm db:push' pour créer les tables"
else
echo -e "${GREEN}[OK]${NC} Tables trouvées:"
echo "$TABLES" | while read table; do
echo " - $table"
done
# Vérifier la table users
if echo "$TABLES" | grep -q "users"; then
USER_COUNT=$(mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASS -D $DB_NAME -e "SELECT COUNT(*) FROM users;" 2>/dev/null | tail -n 1)
echo ""
echo -e "${GREEN}[OK]${NC} Table 'users' existe"
echo " Nombre d'utilisateurs: $USER_COUNT"
# Vérifier si adminServFormation existe
ADMIN_EXISTS=$(mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASS -D $DB_NAME -e "SELECT COUNT(*) FROM users WHERE username='adminServFormation';" 2>/dev/null | tail -n 1)
if [ "$ADMIN_EXISTS" = "1" ]; then
echo -e "${GREEN}[OK]${NC} Utilisateur 'adminServFormation' existe"
# Vérifier le mot de passe
PASSWORD_HASH=$(mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASS -D $DB_NAME -e "SELECT password FROM users WHERE username='adminServFormation';" 2>/dev/null | tail -n 1)
if [ -z "$PASSWORD_HASH" ] || [ "$PASSWORD_HASH" = "NULL" ]; then
echo -e "${RED}[ERREUR]${NC} Le mot de passe de 'adminServFormation' est vide ou NULL"
else
echo -e "${GREEN}[OK]${NC} Le mot de passe est défini (hash: ${PASSWORD_HASH:0:20}...)"
fi
else
echo -e "${RED}[ERREUR]${NC} Utilisateur 'adminServFormation' n'existe pas"
echo -e "${YELLOW}[INFO]${NC} Utilisez le script 'reset-admin.sh' pour le créer"
fi
else
echo -e "${RED}[ERREUR]${NC} Table 'users' n'existe pas"
echo -e "${YELLOW}[INFO]${NC} Exécutez 'pnpm db:push' pour créer les tables"
fi
fi
else
echo -e "${RED}[ERREUR]${NC} Impossible de se connecter à la base de données"
echo -e "${YELLOW}[INFO]${NC} Vérifiez les paramètres de connexion dans .env"
fi
else
echo -e "${YELLOW}[WARN]${NC} MySQL client non installé, impossible de vérifier la base de données"
fi
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo " 3. VÉRIFICATION DES DÉPENDANCES"
echo "═══════════════════════════════════════════════════════════════"
echo ""
# Vérifier node_modules
if [ -d "node_modules" ]; then
echo -e "${GREEN}[OK]${NC} Dossier node_modules existe"
# Vérifier bcryptjs
if [ -d "node_modules/bcryptjs" ]; then
echo -e "${GREEN}[OK]${NC} bcryptjs est installé"
else
echo -e "${RED}[ERREUR]${NC} bcryptjs n'est pas installé"
echo -e "${YELLOW}[INFO]${NC} Exécutez 'pnpm install' pour installer les dépendances"
fi
# Vérifier jsonwebtoken
if [ -d "node_modules/jsonwebtoken" ]; then
echo -e "${GREEN}[OK]${NC} jsonwebtoken est installé"
else
echo -e "${RED}[ERREUR]${NC} jsonwebtoken n'est pas installé"
echo -e "${YELLOW}[INFO]${NC} Exécutez 'pnpm install' pour installer les dépendances"
fi
else
echo -e "${RED}[ERREUR]${NC} Dossier node_modules n'existe pas"
echo -e "${YELLOW}[INFO]${NC} Exécutez 'pnpm install' pour installer les dépendances"
fi
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo " 4. VÉRIFICATION DU SERVICE"
echo "═══════════════════════════════════════════════════════════════"
echo ""
# Vérifier si le service systemd existe
if command -v systemctl &> /dev/null; then
if systemctl list-unit-files | grep -q "formation-manager"; then
echo -e "${GREEN}[OK]${NC} Service systemd 'formation-manager' existe"
if systemctl is-active --quiet formation-manager; then
echo -e "${GREEN}[OK]${NC} Service est actif"
else
echo -e "${YELLOW}[WARN]${NC} Service n'est pas actif"
fi
if systemctl is-enabled --quiet formation-manager; then
echo -e "${GREEN}[OK]${NC} Service est activé au démarrage"
else
echo -e "${YELLOW}[WARN]${NC} Service n'est pas activé au démarrage"
fi
else
echo -e "${YELLOW}[INFO]${NC} Service systemd 'formation-manager' n'existe pas"
fi
fi
# Vérifier si l'application répond
echo ""
if command -v curl &> /dev/null; then
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000 2>/dev/null || echo "000")
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "302" ]; then
echo -e "${GREEN}[OK]${NC} Application répond (HTTP $HTTP_CODE)"
else
echo -e "${YELLOW}[WARN]${NC} Application ne répond pas correctement (HTTP $HTTP_CODE)"
fi
fi
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo " RÉSUMÉ DU DIAGNOSTIC"
echo "═══════════════════════════════════════════════════════════════"
echo ""
if [ -z "$JWT_SECRET" ]; then
echo -e "${RED}[ACTION REQUISE]${NC} Définir JWT_SECRET dans .env"
echo " Générer avec: openssl rand -base64 32"
fi
if [ "$ADMIN_EXISTS" != "1" ]; then
echo -e "${RED}[ACTION REQUISE]${NC} Créer l'utilisateur adminServFormation"
echo " Exécuter: ./reset-admin.sh"
fi
if [ -z "$TABLES" ]; then
echo -e "${RED}[ACTION REQUISE]${NC} Initialiser la base de données"
echo " Exécuter: pnpm db:push"
fi
echo ""
echo "Diagnostic terminé."