mirror of
https://github.com/R0m1k3/noteflow.git
synced 2026-10-11 17:29:37 +02:00
Merge pull request #66 from R0m1k3/claude/fix-stack-redeploy-error-011CV4ma2o8R5TFqh2v7L5hK
Fix: Détection automatique URL pour OAuth Google Calendar
This commit is contained in:
1 file changed
+28
-13
+28
-13
@@ -29,8 +29,9 @@ async function getAuthType() {
|
||||
/**
|
||||
* Créer un client OAuth2
|
||||
* @param {string} [customRedirectUri] - URI de redirection personnalisée (optionnel)
|
||||
* @param {Object} [req] - Objet requête Express pour détecter l'URL
|
||||
*/
|
||||
async function getOAuth2Client(customRedirectUri = null) {
|
||||
async function getOAuth2Client(customRedirectUri = null, req = null) {
|
||||
const clientId = await getOne("SELECT value FROM settings WHERE key = 'google_client_id'");
|
||||
const clientSecret = await getOne("SELECT value FROM settings WHERE key = 'google_client_secret'");
|
||||
const appUrl = await getOne("SELECT value FROM settings WHERE key = 'app_url'");
|
||||
@@ -39,12 +40,23 @@ async function getOAuth2Client(customRedirectUri = null) {
|
||||
throw new Error('Client ID et Client Secret non configurés');
|
||||
}
|
||||
|
||||
// Utiliser l'URI personnalisée si fournie, sinon utiliser l'URL du site configurée
|
||||
// Détecter l'URL depuis la requête si possible
|
||||
let detectedUrl = null;
|
||||
if (req) {
|
||||
const protocol = req.headers['x-forwarded-proto'] || req.protocol || 'https';
|
||||
const host = req.headers['x-forwarded-host'] || req.headers.host;
|
||||
if (host) {
|
||||
detectedUrl = `${protocol}://${host}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Utiliser l'URI personnalisée si fournie, sinon l'URL configurée, sinon l'URL détectée
|
||||
const redirectUri = customRedirectUri ||
|
||||
(appUrl?.value ? `${appUrl.value}/api/calendar/oauth-callback` : null);
|
||||
(appUrl?.value ? `${appUrl.value}/api/calendar/oauth-callback` : null) ||
|
||||
(detectedUrl ? `${detectedUrl}/api/calendar/oauth-callback` : null);
|
||||
|
||||
if (!redirectUri) {
|
||||
throw new Error('URL du site non configurée. Veuillez configurer l\'URL dans les paramètres.');
|
||||
throw new Error('URL du site non configurée. Veuillez configurer l\'URL dans les paramètres (Paramètres > Google Calendar > URL du site).');
|
||||
}
|
||||
|
||||
return new google.auth.OAuth2(
|
||||
@@ -95,8 +107,9 @@ async function getApiKeyClient() {
|
||||
/**
|
||||
* Créer un client Google Calendar selon la méthode d'authentification
|
||||
* @param {number} userId - ID de l'utilisateur (pour OAuth2)
|
||||
* @param {Object} [req] - Objet requête Express pour détecter l'URL
|
||||
*/
|
||||
async function getCalendarClient(userId = null) {
|
||||
async function getCalendarClient(userId = null, req = null) {
|
||||
const authType = await getAuthType();
|
||||
|
||||
if (authType === AUTH_TYPES.SERVICE_ACCOUNT) {
|
||||
@@ -110,7 +123,7 @@ async function getCalendarClient(userId = null) {
|
||||
if (!userId) {
|
||||
throw new Error('User ID requis pour OAuth2');
|
||||
}
|
||||
const oauth2Client = await getValidTokens(userId);
|
||||
const oauth2Client = await getValidTokens(userId, req);
|
||||
return google.calendar({ version: 'v3', auth: oauth2Client });
|
||||
}
|
||||
}
|
||||
@@ -139,7 +152,7 @@ router.get('/auth-url', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
// Permettre de passer une URL de redirection personnalisée via query param
|
||||
const customRedirectUri = req.query.redirect_uri;
|
||||
const oauth2Client = await getOAuth2Client(customRedirectUri);
|
||||
const oauth2Client = await getOAuth2Client(customRedirectUri, req);
|
||||
|
||||
const authUrl = oauth2Client.generateAuthUrl({
|
||||
access_type: 'offline',
|
||||
@@ -175,7 +188,7 @@ router.get('/oauth-callback', async (req, res) => {
|
||||
return res.status(400).send('État utilisateur invalide');
|
||||
}
|
||||
|
||||
const oauth2Client = await getOAuth2Client();
|
||||
const oauth2Client = await getOAuth2Client(null, req);
|
||||
|
||||
// Échanger le code contre des tokens
|
||||
const { tokens } = await oauth2Client.getToken(code);
|
||||
@@ -298,8 +311,10 @@ router.get('/auth-status', authenticateToken, async (req, res) => {
|
||||
|
||||
/**
|
||||
* Récupérer et rafraîchir les tokens OAuth si nécessaire
|
||||
* @param {number} userId - ID de l'utilisateur
|
||||
* @param {Object} [req] - Objet requête Express pour détecter l'URL
|
||||
*/
|
||||
async function getValidTokens(userId) {
|
||||
async function getValidTokens(userId, req = null) {
|
||||
const tokenData = await getOne(
|
||||
'SELECT * FROM google_oauth_tokens WHERE user_id = ?',
|
||||
[userId]
|
||||
@@ -309,7 +324,7 @@ async function getValidTokens(userId) {
|
||||
throw new Error('Tokens OAuth non trouvés. Veuillez vous authentifier.');
|
||||
}
|
||||
|
||||
const oauth2Client = await getOAuth2Client();
|
||||
const oauth2Client = await getOAuth2Client(null, req);
|
||||
oauth2Client.setCredentials({
|
||||
access_token: tokenData.access_token,
|
||||
refresh_token: tokenData.refresh_token,
|
||||
@@ -394,7 +409,7 @@ router.post('/sync', authenticateToken, async (req, res) => {
|
||||
}
|
||||
|
||||
// Créer un client Google Calendar selon la méthode d'authentification
|
||||
const calendar = await getCalendarClient(req.user.id);
|
||||
const calendar = await getCalendarClient(req.user.id, req);
|
||||
|
||||
// Récupérer les événements des 30 prochains jours
|
||||
const now = new Date();
|
||||
@@ -528,7 +543,7 @@ router.post('/events', authenticateToken, async (req, res) => {
|
||||
return res.status(400).json({ error: 'Titre, date de début et date de fin requis' });
|
||||
}
|
||||
|
||||
const calendar = await getCalendarClient(req.user.id);
|
||||
const calendar = await getCalendarClient(req.user.id, req);
|
||||
|
||||
// Préparer l'événement
|
||||
const event = {
|
||||
@@ -648,7 +663,7 @@ router.put('/events/:id', authenticateToken, async (req, res) => {
|
||||
return res.status(400).json({ error: 'Titre, date de début et date de fin requis' });
|
||||
}
|
||||
|
||||
const calendar = await getCalendarClient(req.user.id);
|
||||
const calendar = await getCalendarClient(req.user.id, req);
|
||||
|
||||
// Préparer l'événement mis à jour
|
||||
const event = {
|
||||
|
||||
Reference in new issue
Block a user