4
votes

I have a auth problem with laravel sessions. (It was working in same system before)

This is my user controller

public function postLogin()
{
    $username = Input::get("username");
    $password = Input::get("password");
    $credentials = array
    (
        "username"=> $username,
        "password" => $password,
    );
    if (Auth::attempt($credentials)) 
    {
        echo Auth::user()->username;
        exit();

    }
    else
    {
        return Redirect::to("user/login")->with("error_msg","Giriş Başarısız! <br>Lütfen girdiğiniz bilgileri kontrol edip yeniden deneyin.");
    }
}

It returns username.. But when i redirect page session is losing.

public function postLogin()
{
    $username = Input::get("username");
    $password = Input::get("password");
    $credentials = array
    (
        "username"=> $username,
        "password" => $password,
    );
    if (Auth::attempt($credentials)) 
    {
        return Redirect::to('check_login');

    }
    else
    {
        return Redirect::to("user/login")->with("error_msg","Giriş Başarısız! <br>Lütfen girdiğiniz bilgileri kontrol edip yeniden deneyin.");
    }
}

And My Route:

Route::get("check_login",function(){
    echo Auth::user()->username;
});

The result:

ErrorException
Trying to get property of non-object
echo Auth::user()->username;
2
Check your session.php config file. Does the domain and path settings match with your dev environment? - fmgonzalez
I checked the session.php they are the default settings 'path' => '/', 'domain' => null, - KadirCanerErgun
Are you working on a vhost environment? What is the session driver? If you're trying to log in an url like http://localhost/myproject/public/ or something like that it could be the problem. - fmgonzalez
Im not working in a vhost. My session drive is file. Url is a real domain. And when i test it before it was working. - KadirCanerErgun

2 Answers

1
votes

Check your route for postLogin() function. If you are using in UserController, it should be below for post method.

Route::post('login', array('uses' => 'UserController@postLogin'));

Function and route that you provided is working perfectly with me, but if you want to check the user is saved in session or not, try like this.

if (Auth::check())
{
    echo 'My user is logged in';
}

It is better if you can provide all routes you are using for that.

1
votes

use database session, more info: http://laravel.com/docs/5.0/session#database-sessions

OR

I got the same problem. & found problem with my code

in my controller:

public function login(Request $request){
    //assign $request data to session
    Session::put('price',$request->price);
}
public function checkLogin(){
    if(login fail){
    redirect back to login
    }
}

when i redirect to login session override to empty because when I redirect, $request is empty so 'price' is empty

so I check session already got value before put into session

public function login(Request $request){
    //check session already got value
    if(Session::get('price') == ""){

    //assign $request data to session
    Session::put('price',$request->price);
    }
}

problem solved for me.

hope it helps to anyone have same problem