0
votes

I have followed the tutorial for nenad/yii2-basic-template and the application seems to have setup perfectly. Everything except for the login script. I doubt that there is an issue with the actual code but with my host setup. When the application is first setup i create a superadmin user no problem and user creation after that isnt a problem either, everything checked and correct in the db. when i go through the login process it doesn't seem to be storing the session vlues correctly. if i exit the application from the login function inside the site controller and return the Yii::$app->user->identity the output is what id expect to see, however if i allow the app to render the view and i output the same variable from the view it returns null.

I am running the application on a vagrant box running apache2, php 5.6, mysql 14.4 and other irrelvent packages. i can upload any code or config files if anyone wishes. thanks in advance.

EDIT

fisrt part is the login function from inside _protected/controllers/siteController.php, second part is the start to the view file from _Protected/views/site/index.php

/**
 * Logs in the user if his account is activated,
 * if not, displays appropriate message.
 *
 * @return string|\yii\web\Response
 */
public function actionLogin()
{
    if (!Yii::$app->user->isGuest) 
    {
        return $this->goHome();
    }

    // get setting value for 'Login With Email'
    $lwe = Yii::$app->params['lwe'];

    // if 'lwe' value is 'true' we instantiate LoginForm in 'lwe' scenario
    $model = $lwe ? new LoginForm(['scenario' => 'lwe']) : new LoginForm();

    // now we can try to log in the user
    if ($model->load(Yii::$app->request->post()) && $model->login()) 
    {
        var_dump(Yii::$app->user->identity);
        // var_dump($_SESSION);
        // if (!is_writable(session_save_path())) {
        //     echo 'Session path "'.session_save_path().'" is not writable for PHP!'; 
        // } else {
        //     echo 'Session Path "'.session_save_path().'" IS writable for PHP!';
        // }
        // phpinfo();
        exit;
        return $this->goBack();
    }
    // user couldn't be logged in, because he has not activated his account
    elseif($model->status === User::STATUS_NOT_ACTIVE)
    {
        // if his account is not activated, he will have to activate it first
        Yii::$app->session->setFlash('error', 
            Yii::t('app', 'You have to activate your account first. Please check your email.'));

        return $this->refresh();
    }    
    // account is activated, but some other errors have happened
    else
    {
        return $this->render('login', [
            'model' => $model,
        ]);
    }
}



<?php

/* @var $this yii\web\View */
$this->title = Yii::t('app', Yii::$app->name);
var_dump(Yii::$app->user->identity);
?>
<div class="site-index">

    <div class="jumbotron">
        <h1>Congratulations!</h1>

        <p class="lead">You have successfully installed Yii2 improved application template</p>

        <p><a class="btn btn-lg btn-success" href="http://www.freetuts.org/tutorial/view?id=6">Read our tutorial</a></p>
    </div>

    <div class="body-content">

        <div class="row">
            <div class="col-lg-3">

as you can see i exit the application within the login function and the data in the var dump is what id expect to see, but if i let the application forward to the view the user identity is returned as null.

2
Show some code where you're outputting the variable in the view - Brett Gregson
Show your LoginForm model. - SiZE
Please show your session config in web.php or main.php - Hanafi

2 Answers

-1
votes

If you are using Yii2 on your local host then by default the value of the STATUS_ACTIVE (in User file which can be found in the common folder) is set to 10. for eg:

const STATUS_ACTIVE = 10;

Change this value to const STATUS_ACTIVE = 9;

After this you will be able to perfectly login.

-1
votes

The easiest solution would be to change the status value in the database table.