Yii2 login not working when debug module is not loaded

735 Views Asked by At

We have a working Yii2 app that was Frankensteinized using Yii1's old version as a base then updated to Yii2. Of course there were a lot of problems and I managed to fix most, however there is a problem that keeps evading me.

As soon as we switch the environment to anything else than DEV, this code isn't executed anymore:

$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
    'class' => 'yii\debug\Module',
    // uncomment the following to add your IP if you are not connecting from localhost.
    //'allowedIPs' => ['127.0.0.1', '::1'],
];

When this isn't executed, we can't log in anymore. I isolated that issue by leaving the DEV environment on and just commenting that bit. I have no idea why it seems like the session or Identity is not saving the user id and just keeps showing back the login page.

I know the login, User and IdentityInterface changed a lot with Yii2 and my dodgy user component/Interface might be one of the causes, but I can't figure out the problem and was wondering if someone would have a strike of "déjà-vu" and recognize why the debug module made it work.

I fixed it temporarily by adding this, in the init() method:

class UserIdentity extends \yii\web\User implements \yii\web\IdentityInterface
{
    function init()
    {
        parent::init();
        $this->id = Yii::$app->session->get('id');
    }
}

This way, Yii::$app->user->id will always have the $_SESSION value in it
.
.
.
.
.
As requested, my complete web.php config code:

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'name' => 'NAME WIL BE HERE',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm' => '@vendor/npm-asset',
    ],
    'layout' => 'column1',
    'components' => [
        'request' => [
            'class' => 'app\components\NTDI_HttpRequest',
            'cookieValidationKey' => 'XXXXXXXXXXXXXXXXXXXX',
        ],
        'session' => [
            'class' => 'yii\web\DbSession',
            'name'=>'sess',
            'timeout'=>3600,
            'db'=>'db',
            'sessionTable'=>'session',
        ],
        'user' => [
            'identityClass' => 'app\components\UserIdentity',
            'class' => 'app\components\UserIdentity',
            'loginUrl' => array('/site/login'),
            'returnUrl' => array('/site/index'),
            'enableAutoLogin' => false,
        ],
        'errorHandler'=>array(
            'errorAction'=>'site/error',
        ),
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        'language' => 'fr_CA',
        'sourceLanguage' => 'en_US',
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => false,
            'showScriptName' => true,
            'rules' => [
                '' => 'site/index',
                'membre' => 'site/login',
                'nip' => 'member/nip',
                'verif' => 'member/verif',
                'vote' => 'member/vote',
                'merci' => 'member/logout',
            ],
        ],
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'fileMap' => [
                        'general' => 'general.php',
                        'landing' => 'landing.php',
                        'vote' => 'vote.php',
                        'app/error' => 'error.php',
                    ],
                ],
            ],
        ],
    ],
    'params' => $params,
];


// #BUG# If this is not set, we can't login anymore and the system keeps asking for a username numnber.. why??
//*
if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}
//*/

if (YII_ENV_DEV) {
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        'allowedIPs' => ['127.0.0.1', '::1', '192.222.216.153', '24.37.138.46'],
    ];
}

return $config;

As requested by @rob006, my UserIdentity code. (sorry it's messy, most of that code was already there and I didn't want to change it too much

0

There are 0 best solutions below