Laravel Auth delay when redirecting pages

I have an auth problem with laravel sessions. (He worked on the 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 the username .. But when I redirect the page session, it gets lost.

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;
});

Result:

ErrorException
Trying to get property of non-object
echo Auth::user()->username;
+4
source share
2 answers

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

OR

I have the same problem. and found a 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
    }
}

, , , $request ""

,

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

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

.

, - ​​ .

+2

postLogin(). UserController, post.

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

, , , , , .

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

, , .

+1

Source: https://habr.com/ru/post/1543353/


All Articles