简体   繁体   中英

typo3 7 + extbase: manually login frontend user

I'm trying to manually login a frontend user (and do some other basic operations while I'm at it) in an ajaxAction and it doesn't seem to work as I intend. What I do when the loginform is submitted:

$loginData = array(
    'username' => $username,
    'uident_text' => $password,
    'status' => 'login',
);

$GLOBALS['TSFE']->fe_user->checkPid = 0;
$info = $GLOBALS['TSFE']->fe_user->getAuthInfoArray();
$user = $GLOBALS['TSFE']->fe_user->fetchUserRecord($info['db_user'], $loginData['username']);

$GLOBALS['TSFE']->loginUser = 1;
$GLOBALS['TSFE']->fe_user->createUserSession($user);
$GLOBALS['TSFE']->fe_user->setAndSaveSessionData('user', TRUE);

return json_encode(['login' => 'true']);

when trying to print out $GLOBALS['TSFE']->fe_user before the return, the data are set and $GLOBALS['TSFE']->loginUser = 1 , so everything looks file. When I reload the login form afterwards though, the fe_user data are still there, but the fluid security.ifAuthenticated does not work.

The Form view looks like this:

<f:security.ifAuthenticated>
    <f:then>
        user is authenticated!
    </f:then>
    <f:else>
        <f:render partial="LoginForm.html"/>
    </f:else>
</f:security.ifAuthenticated>

Username: {user.firstName}

And it corretly outputs the Firstname of the logged in user, but also still shows the loginform. security.ifAuthenticated always enters the ELSE. And when I looked at the viewHelper it is because $GLOBALS['TSFE']->loginUser is not set.

Any hints/ideas as to why this is happening?

From what I experimented, you need to set a cookie for it to work.

$reflection = new \ReflectionClass($GLOBALS['TSFE']->fe_user);
$setSessionCookieMethod = $reflection->getMethod('setSessionCookie');
$setSessionCookieMethod->setAccessible(TRUE);
$setSessionCookieMethod->invoke($GLOBALS['TSFE']->fe_user);

If I played only with $GLOBALS['TSFE']->fe_users, it didn't work even if the fe_session was created in the database.

Turns out my inexperience with typo3 and the fe_login was at fault. What I did not know is that $GLOBALS['TSFE']->loginUser is an indicator of fe_groups. The problem was, that my frontend user was not assigned to a group, the column "usergroup" was empty in the database. Therefore Typo3 sets loginUser back to false and that's why ifAuthenticated does not work.

I wrote about the solution here , but basically the only thing I did was assign the user to a group and afterwards the login as I tried it worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM