Refactor session handling and redirect logic for login flow
This commit enhances the session management and redirect behavior in the login process. It introduces a new function, `fxCompteRedirectAfterLoginConsume`, to streamline the handling of post-login redirects while preventing loops for login-related pages. Additionally, comments are updated for clarity regarding the session's role in protecting access to certain pages. These changes aim to improve user experience and maintainability of the authentication flow.
This commit is contained in:
14
compte.php
14
compte.php
@ -98,16 +98,16 @@ if (!empty($_GET['a']) && $_GET['code'] != 'bib_assignment') {
|
||||
$strCode = $_GET['a'];
|
||||
}
|
||||
|
||||
// empêcher d'accéder à une page où il faut être loggé et qu'on ne l'est pas
|
||||
if (($strCode != "compte_login" && $strCode != "compte_motdepasse" && $strCode != "compte_nouveau" && $strCode != "compte_reset" && !isset($_SESSION['com_id'])) || (($strCode == "compte_login" || $strCode == "compte_motdepasse" || $strCode == "compte_nouveau" || $strCode == "compte_reset") && isset($_SESSION['com_id']))) {
|
||||
|
||||
|
||||
|
||||
/// MSIN-4339 Sauvegarder l'URL demandée
|
||||
// Page protégée sans session : sauver la cible puis formulaire de login (MSIN-4339).
|
||||
if ($strCode != 'compte_login' && $strCode != 'compte_motdepasse' && $strCode != 'compte_nouveau' && $strCode != 'compte_reset' && !isset($_SESSION['com_id'])) {
|
||||
$_SESSION['redirect_after_login'] = $_SERVER['REQUEST_URI'];
|
||||
|
||||
header('Location: ' . $vDomaine . '/' . $strPage);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Déjà connecté sur login / mot de passe / inscription : accueil compte, sans polluer redirect_after_login.
|
||||
if (($strCode == 'compte_login' || $strCode == 'compte_motdepasse' || $strCode == 'compte_nouveau' || $strCode == 'compte_reset') && isset($_SESSION['com_id'])) {
|
||||
header('Location: ' . $vDomaine . '/' . $strPage);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
@ -1162,6 +1162,29 @@ function fxCourrielPerduCompte($strLangue)
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* URL post-login MSIN-4339 — ignore login/mot de passe pour éviter les rebonds en boucle.
|
||||
*/
|
||||
function fxCompteRedirectAfterLoginConsume()
|
||||
{
|
||||
if (empty($_SESSION['redirect_after_login'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$strUrl = $_SESSION['redirect_after_login'];
|
||||
unset($_SESSION['redirect_after_login']);
|
||||
|
||||
$strPath = parse_url($strUrl, PHP_URL_PATH) ?: '';
|
||||
if ($strPath === '/compte' || $strPath === '/account') {
|
||||
return null;
|
||||
}
|
||||
if (preg_match('#/(compte|account)/(login|motdepasse|nouveau|reset)(/|$)#i', $strPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $strUrl;
|
||||
}
|
||||
|
||||
function fxLoginCompte($infoLogin, $strLangue)
|
||||
{
|
||||
global $objDatabase, $vDomaine;
|
||||
@ -1238,17 +1261,13 @@ function fxLoginCompte($infoLogin, $strLangue)
|
||||
$arrRetour['state'] = 'true';
|
||||
|
||||
|
||||
//MSIN-4339
|
||||
if (!empty($_SESSION['redirect_after_login'])) {
|
||||
$url = $_SESSION['redirect_after_login'];
|
||||
|
||||
|
||||
unset($_SESSION['redirect_after_login']);
|
||||
header("Location: " . $url);
|
||||
// MSIN-4339 — page protégée demandée avant login (pas login/mot de passe).
|
||||
$strAfterLogin = fxCompteRedirectAfterLoginConsume();
|
||||
if ($strAfterLogin !== null) {
|
||||
header('Location: ' . $strAfterLogin);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
if ($strLangue == 'fr')
|
||||
$_SESSION['msg'] = 'e112';
|
||||
@ -1274,16 +1293,8 @@ function fxLoginCompte($infoLogin, $strLangue)
|
||||
else
|
||||
$strPage = 'account';
|
||||
|
||||
if (($arrRetour['state'] ?? '') === 'true' && !empty($infoCompte['com_id'])) {
|
||||
require_once dirname(__FILE__) . '/inc_fx_promoteur_hub.php';
|
||||
if (fxPromoteurHubShouldUse($infoCompte['com_id'])) {
|
||||
header('Location: ' . fxPromoteurHubUrl($strLangue));
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
//header("Location: " . $vDomaine . '/' . $strPage);
|
||||
header("Location: " . fxGetReferer());
|
||||
// Un seul atterrissage : /compte (hub v2 ou accueil selon compte.php).
|
||||
header('Location: ' . $vDomaine . '/' . $strPage);
|
||||
}
|
||||
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user