BUT Informatique S4 - Module R401
Implémenter Google OAuth 2.0 dans une application Vue.js + Express déjà fonctionnelle avec authentification JWT classique (email/password).
⏱️ Temps estimé : 30 minutes
✅ Authentification email/password complète ✅ Génération et vérification JWT ✅ MongoDB avec modèle User (supporte googleId et picture) ✅ CORS configuré ✅ Frontend Vue.js complet (Login, Register, Home, AuthCallback) ✅ Bouton "Se connecter avec Google" déjà stylisé ✅ Middleware d'authentification JWT ✅ Routes classiques fonctionnelles
Fichier : `backend/config/passport.js`
Implémenter la stratégie Google OAuth 2.0 avec :
- clientID, clientSecret, callbackURL depuis .env
- Option passReqToCallback: true
- Callback async qui cherche/crée l'utilisateur
Fichier : `backend/routes/auth.js`
Créer 2 routes :
- GET /google : Initie l'authentification Google
- GET /google/callback : Reçoit le callback, génère JWT, redirige
```bash cd backend npm install cp .env.example .env
```
```bash cd frontend npm install cp .env.example .env ```
- Créer un projet sur Google Cloud Console
- API et services → Écran de consentement OAuth → Type: Externe
- Ajouter votre email dans Utilisateurs test
- Créer des identifiants OAuth 2.0:
- Origines JavaScript: http://localhost:3000, http://localhost:5173
- URI de redirection: http://localhost:3000/auth/google/callback
- Activer l'API People
- Copier Client ID et Secret dans backend/.env
```bash
Lancer MongoDB
cd backend && npm install && npm run dev
cd frontend && npm install && npm run dev ```
Frontend: http://localhost:5173 Backend: http://localhost:3000
- Créer un compte classique → OK si ça fonctionne
- Cliquer sur "Se connecter avec Google"
- Autoriser l'accès
- Vérifier redirection vers /home avec profil Google
- Passport Google OAuth
- Slides du cours: slides/oauth-google-nodejs-jwt.html (Slides 21, 23, 18)
| Erreur | Solution |
|---|---|
| redirect_uri_mismatch | Vérifier URL callback dans Google Cloud Console |
| Access blocked | Ajouter email dans Utilisateurs test |
| Cannot find module | npm install dans backend |
Bon courage ! 🚀