Laravel 4 Auth works but does not stay logged in

随声附和 提交于 2019-12-08 04:07:44

问题


In my Laravel 4 App I use the Auth functionality.

I submit my login form to:

$data = array(
'email'     => Input::get('email'),
'password'  => Input::get('password')
);

if (Auth::attempt($data)) {
    echo 'SUCCESS!';
} 

This outputs SUCCESS, hence everything works. But if I next call another method the user is not logged in:

if (Auth::check()) {
echo "logged in";
}
else {
echo "not logged in";
}

This outputs 'not logged in'. I tried to add true to attempt to remember login but it does not make a diference. I have another Laravel App with Auth running that works perfectly.

Any ideas what the reason could be?


回答1:


1.First Authenticate with :

     if (Auth::attempt($data)) {
  return Redirect::to('admin'); 
}

2.Then place this check to the page you have redirected your user after successful authentication In this case say 'admin' .

 if (Auth::check()) {
    echo "process";
    }
    else {
    return Redirect::to('login');
    }



回答2:


I found the solution. Maybe this can help others:

After calling Auth:attempt you must not echo anything. You have do to a redirect. Does not make sense to me but solved the problem.




回答3:


Event::listen('auth.login', function($user)
{
    echo 'user logged in'; 
});

Place this after Auth::attempt($data) and check.



来源:https://stackoverflow.com/questions/24418755/laravel-4-auth-works-but-does-not-stay-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!