Laravel Auth session losing when redirect pages
Solution 1
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.
Solution 2
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
KadirCanerErgun
Updated on June 04, 2022Comments
-
KadirCanerErgun almost 2 years
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;