femanager icon indicating copy to clipboard operation
femanager copied to clipboard

Session data lost after autologin

Open nollm opened this issue 5 years ago • 6 comments

I have the same problem like @Skiud.

On my website is a shop where users can add products to the basket without logged in. After registration and autologin the basket is empty.

The problem seems to be in UserUtility->login().

userutility_login

The following line overrides the ses_data where the basket content is stored. $tsfe->fe_user->user = $tsfe->fe_user->fetchUserSession();

Here you see the state before autologin. session_before_login

And here the state after autologin. session_after_login

Do you have a solution how to fix it?

Originally posted by @nollm in https://github.com/in2code-de/femanager/issues/57#issuecomment-607159464

nollm avatar Apr 14 '20 10:04 nollm

@nollm Is there a fix for that? How did you solve the problem? Have the same problem.After autologin the tx cart session data gets lost.

kevinlieser avatar Jul 16 '20 18:07 kevinlieser

@kevinlieser Sorry for the late answer. Usually it is a bad idea but I commented out this line //$tsfe->fe_user->user = $tsfe->fe_user->fetchUserSession();.

nollm avatar Dec 03 '20 10:12 nollm

I fixed that for me with this XClass (hope that was all I have done. It saves the oldSession data and restores it in a foreach loop at the end.

class CustomUserUtility extends In2code\Femanager\Utility\UserUtility
{

    public static function login(User $user, $storagePids = null)
    {
        $tsfe = self::getTypoScriptFrontendController();
        $oldSessionData = $tsfe->fe_user->getAllSessionData();

        $tsfe->fe_user->checkPid = false;
        $info = $tsfe->fe_user->getAuthInfoArray();

        $cleanIntList = implode(',', GeneralUtility::intExplode(',', $storagePids));

        $extraWhere = ' AND uid = ' . (int)$user->getUid();
        if (!empty($storagePids)) {
            $extraWhere = ' AND pid IN (' . $cleanIntList . ')';
        }
        $user = $tsfe->fe_user->fetchUserRecord($info['db_user'], $user->getUsername(), $extraWhere);
        $tsfe->fe_user->createUserSession($user);
        self::loginAlternative($tsfe);
        $tsfe->fe_user->user = $tsfe->fe_user->fetchUserSession();
        $tsfe->fe_user->setAndSaveSessionData('ses', true);
        
        foreach($oldSessionData as $index => $value) {
            $tsfe->fe_user->setAndSaveSessionData($index, $value);
        }

    }
}

kevinlieser avatar Dec 03 '20 10:12 kevinlieser