#!/bin/bash ############################################################################### # Script de vérification de la configuration de connexion # Vérifie que tout est en place pour que la connexion fonctionne ############################################################################### set -e # Couleurs RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' echo "╔════════════════════════════════════════════════════════════════╗" echo "║ Vérification de la configuration de connexion ║" echo "╚════════════════════════════════════════════════════════════════╝" echo "" # Charger les variables d'environnement if [ -f ".env" ]; then export $(cat .env | grep -v '^#' | xargs 2>/dev/null) else echo -e "${RED}[ERREUR]${NC} Fichier .env non trouvé" exit 1 fi # 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') echo "═══════════════════════════════════════════════════════════════" echo " 1. VÉRIFICATION JWT_SECRET" echo "═══════════════════════════════════════════════════════════════" echo "" if [ -z "$JWT_SECRET" ]; then echo -e "${RED}[ERREUR]${NC} JWT_SECRET n'est pas définie dans .env" echo -e "${YELLOW}[ACTION]${NC} Ajoutez JWT_SECRET dans .env:" echo " JWT_SECRET=$(openssl rand -base64 32)" JWT_OK=false else echo -e "${GREEN}[OK]${NC} JWT_SECRET est définie" SECRET_LENGTH=${#JWT_SECRET} echo " Longueur: $SECRET_LENGTH caractères" if [ $SECRET_LENGTH -lt 32 ]; then echo -e "${YELLOW}[WARN]${NC} JWT_SECRET est trop courte (recommandé: 32+ caractères)" JWT_OK=false else JWT_OK=true fi fi echo "" echo "═══════════════════════════════════════════════════════════════" echo " 2. VÉRIFICATION DE L'UTILISATEUR DANS LA BASE DE DONNÉES" echo "═══════════════════════════════════════════════════════════════" echo "" # Vérifier l'utilisateur USER_INFO=$(mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASS -D $DB_NAME -se "SELECT username, email, role, isActive, loginMethod, LENGTH(password) as pwd_len FROM users WHERE username='adminServFormation';" 2>/dev/null) if [ -z "$USER_INFO" ]; then echo -e "${RED}[ERREUR]${NC} Utilisateur 'adminServFormation' n'existe pas" echo -e "${YELLOW}[ACTION]${NC} Exécutez: ./create-admin.sh" USER_OK=false else echo -e "${GREEN}[OK]${NC} Utilisateur 'adminServFormation' existe" echo "" echo "Détails de l'utilisateur:" # Parser les informations IFS=$'\t' read -r username email role isActive loginMethod pwd_len <<< "$USER_INFO" echo " Username: $username" echo " Email: $email" echo " Role: $role" echo " Active: $isActive" echo " Login Method: $loginMethod" echo " Password length: $pwd_len caractères" # Vérifications if [ "$isActive" != "1" ]; then echo -e "${RED}[ERREUR]${NC} L'utilisateur est désactivé (isActive = 0)" USER_OK=false elif [ -z "$pwd_len" ] || [ "$pwd_len" = "0" ]; then echo -e "${RED}[ERREUR]${NC} Le mot de passe est vide" USER_OK=false elif [ "$role" != "admin" ]; then echo -e "${YELLOW}[WARN]${NC} L'utilisateur n'a pas le rôle admin" USER_OK=true else USER_OK=true fi fi echo "" echo "═══════════════════════════════════════════════════════════════" echo " 3. VÉRIFICATION DU SERVICE" echo "═══════════════════════════════════════════════════════════════" echo "" SERVICE_OK=false if command -v systemctl &> /dev/null; then if systemctl list-unit-files | grep -q "formation-manager"; then if systemctl is-active --quiet formation-manager; then echo -e "${GREEN}[OK]${NC} Service formation-manager est actif" SERVICE_OK=true # Vérifier les logs récents echo "" echo "Dernières lignes des logs:" echo "---" sudo journalctl -u formation-manager -n 5 --no-pager 2>/dev/null | tail -5 echo "---" else echo -e "${RED}[ERREUR]${NC} Service formation-manager n'est pas actif" echo -e "${YELLOW}[ACTION]${NC} Démarrez le service: sudo systemctl start formation-manager" fi else echo -e "${YELLOW}[INFO]${NC} Service systemd 'formation-manager' n'existe pas" echo "L'application doit être démarrée manuellement" fi fi echo "" echo "═══════════════════════════════════════════════════════════════" echo " 4. VÉRIFICATION DE L'APPLICATION" echo "═══════════════════════════════════════════════════════════════" 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)" APP_OK=true else echo -e "${RED}[ERREUR]${NC} Application ne répond pas (HTTP $HTTP_CODE)" echo -e "${YELLOW}[ACTION]${NC} Vérifiez que l'application est démarrée" APP_OK=false fi fi echo "" echo "═══════════════════════════════════════════════════════════════" echo " 5. TEST DE CONNEXION (SIMULATION)" echo "═══════════════════════════════════════════════════════════════" echo "" # Vérifier bcryptjs if [ -d "node_modules/bcryptjs" ]; then echo -e "${GREEN}[OK]${NC} bcryptjs est installé" # Tester le hash du mot de passe echo "" echo "Test de vérification du mot de passe..." STORED_HASH=$(mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASS -D $DB_NAME -se "SELECT password FROM users WHERE username='adminServFormation';" 2>/dev/null) if [ -n "$STORED_HASH" ]; then # Utiliser Node.js pour vérifier le hash HASH_VALID=$(node -e " const bcrypt = require('bcryptjs'); const hash = '$STORED_HASH'; const password = 'Itinova69!'; bcrypt.compare(password, hash, (err, result) => { if (err) { console.log('ERROR'); } else { console.log(result ? 'VALID' : 'INVALID'); } }); " 2>/dev/null) if [ "$HASH_VALID" = "VALID" ]; then echo -e "${GREEN}[OK]${NC} Le hash du mot de passe est valide pour 'Itinova69!'" HASH_OK=true elif [ "$HASH_VALID" = "INVALID" ]; then echo -e "${RED}[ERREUR]${NC} Le hash du mot de passe ne correspond pas à 'Itinova69!'" echo -e "${YELLOW}[ACTION]${NC} Réexécutez: ./create-admin.sh" HASH_OK=false else echo -e "${YELLOW}[WARN]${NC} Impossible de vérifier le hash (erreur Node.js)" HASH_OK=false fi fi else echo -e "${RED}[ERREUR]${NC} bcryptjs n'est pas installé" echo -e "${YELLOW}[ACTION]${NC} Exécutez: pnpm install" fi echo "" echo "═══════════════════════════════════════════════════════════════" echo " RÉSUMÉ" echo "═══════════════════════════════════════════════════════════════" echo "" ALL_OK=true if [ "$JWT_OK" = true ]; then echo -e "${GREEN}✓${NC} JWT_SECRET configuré correctement" else echo -e "${RED}✗${NC} JWT_SECRET manquant ou invalide" ALL_OK=false fi if [ "$USER_OK" = true ]; then echo -e "${GREEN}✓${NC} Utilisateur adminServFormation configuré correctement" else echo -e "${RED}✗${NC} Problème avec l'utilisateur adminServFormation" ALL_OK=false fi if [ "$HASH_OK" = true ]; then echo -e "${GREEN}✓${NC} Hash du mot de passe valide" else echo -e "${RED}✗${NC} Problème avec le hash du mot de passe" ALL_OK=false fi if [ "$SERVICE_OK" = true ] || [ "$APP_OK" = true ]; then echo -e "${GREEN}✓${NC} Application en cours d'exécution" else echo -e "${RED}✗${NC} Application non démarrée" ALL_OK=false fi echo "" if [ "$ALL_OK" = true ]; then echo "╔════════════════════════════════════════════════════════════════╗" echo "║ Tout est configuré correctement! ✅ ║" echo "╚════════════════════════════════════════════════════════════════╝" echo "" echo "🔑 Vous devriez pouvoir vous connecter avec:" echo " Identifiant: adminServFormation" echo " Mot de passe: Itinova69!" echo "" echo "Si la connexion échoue toujours, vérifiez les logs:" echo " sudo journalctl -u formation-manager -f" else echo "╔════════════════════════════════════════════════════════════════╗" echo "║ Des problèmes ont été détectés ⚠️ ║" echo "╚════════════════════════════════════════════════════════════════╝" echo "" echo "Actions recommandées:" echo "" if [ "$JWT_OK" != true ]; then echo "1. Ajouter JWT_SECRET dans .env:" echo " echo \"JWT_SECRET=\$(openssl rand -base64 32)\" >> .env" echo "" fi if [ "$USER_OK" != true ] || [ "$HASH_OK" != true ]; then echo "2. Réinitialiser l'utilisateur admin:" echo " ./create-admin.sh" echo "" fi if [ "$SERVICE_OK" != true ] && [ "$APP_OK" != true ]; then echo "3. Redémarrer l'application:" echo " sudo systemctl restart formation-manager" echo "" fi fi