How to create and login a frontend user (feuser) in typo3 manually/programmatically?

1.3k views Asked by At

I want to create and login a frontend user in typo3 and I should be able to see user logged-in in login form. FYI Users are getting added to fe_user table in database but are not getting logged in.

$GLOBALS['TSFE']->fe_user->forceSetCookie = TRUE;
                    $GLOBALS['TSFE']->fe_user->start();
                    $GLOBALS['TSFE']->fe_user->createUserSession($user);
                    $GLOBALS['TSFE']->fe_user->user = $user;
                    $GLOBALS['TSFE']->fe_user->setKey('user', 'fe_typo_user', $user);
                    $GLOBALS['TSFE']->fe_user->user = $GLOBALS['TSFE']->fe_user->fetchUserSession();
                    $GLOBALS['TSFE']->fe_user->setAndSaveSessionData('user', TRUE);
                    $this->ses_id = $GLOBALS['TSFE']->fe_user->fetchUserSession();
                    $reflection = new \ReflectionClass($GLOBALS['TSFE']->fe_user);
                    $setSessionCookieMethod = $reflection->getMethod('setSessionCookie');
                    $setSessionCookieMethod->setAccessible(TRUE);
                    $setSessionCookieMethod->invoke($GLOBALS['TSFE']->fe_user);
                    $GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth']['setup']['FE_alwaysFetchUser'] = true;
                    $GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth']['setup']['FE_alwaysAuthUser'] = true;
                    $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['login_confirmed'] = true;
                    $GLOBALS['TSFE']->fe_user->storeSessionData();
                    $GLOBALS['TSFE']->fe_user->loginUser = 1;

expected this : [https://i.stack.imgur.com/lBBkn.png right now getting this https://i.stack.imgur.com/fjBlo.png

2

There are 2 answers

5
owned139 On

This way works for me in 7.6-9.5:

$GLOBALS['TSFE']->fe_user->checkPid = 0;
$GLOBALS['TSFE']->fe_user->dontSetCookie = FALSE;
$GLOBALS['TSFE']->fe_user->user = $GLOBALS['TSFE']->fe_user->fetchUserSession();
$GLOBALS['TSFE']->loginUser = 1;
$GLOBALS['TSFE']->fe_user->start();
$GLOBALS['TSFE']->fe_user->createUserSession(['uid' => $userUid]);
$GLOBALS['TSFE']->initUserGroups();
$GLOBALS['TSFE']->fe_user->loginSessionStarted = TRUE;
$GLOBALS['TSFE']->storeSessionData();
0
Vikram On

You can use Typo3 Authentication services to implement your own user login functionality https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Authentication/Index.html