通过AJAX Laravel 4 AUTH,可能会发行 [英] Laravel 4 AUTH via AJAX, possible session issue
问题描述
我在使用哨兵2 previous项目做到了,但我不能让它一起工作内置的身份验证功能。
I've done it in previous projects using Sentry 2, but I can't get it to work with the built-in Auth functionality.
用户存储方法:
public function store()
{
$validation = new Services\Validators\User;
if ($validation->passesForStore()) {
// Create a new user
$user = new User;
$user->name = Input::get('name');
$user->password = Input::get('password');
$user->email = Input::get('email');
$user->save();
// Authenticate the user
Auth::loginUsingId($user->id);
return JSend::success([
'redirect' => URL::action('VaultController@index')
]);
}
// Provide a custom message for an e-mailaddress that's already in use
$message = '';
if ($validation->hasFailed('email', 'unique')) {
$message = 'This e-mailaddress has already been taken';
}
return JSend::fail([
'failed' => $validation->failedFields(),
'message' => $message
]);
}
Ajax响应处理程序:
The AJAX response handler:
// Redirect to given URL
if (typeof response.data.redirect != 'undefined') {
window.location = response.data.redirect;
}
用户创建成功,将一个验证::检查();
直接 loginUsingId
的结果后, 真
,但只要在网页是由 window.location的
,重定向验证::检查()
结果虚假和用户没有登录,即使是在手动导航到不同的页面。
The user is successfully created, placing an Auth::check();
directly after the loginUsingId
results in true
but as soon as the page is redirected by window.location
, Auth::check()
results in false and the user is not logged in, not even when manually navigating to a different page.
App.session设置:
App.session settings:
'lifetime' => 120,
'expire_on_close' => true,
裸记住登录方法的工作方式类似,但也不起作用! (它的工作,而无需使用AJAX的)
Bare in mind that the login method works in a similar way but doesn't work either! (it does work without the use of AJAX)
===更新===
生活刚刚有点怪异。 <击>我现在可以登录使用自己的登录和注册方法。 是什么改变了?我添加了一个虚拟主机别名我以前的本地主机项目,并记录在似乎 做工精细。 (我听起来像一个环境的问题?)。击>
Life just got a little weirder. I can now login using my login and register methods.
What changed? I added a vhost alias for my former localhost project and logging in seemed
to work fine. (Sounds to me like an environment issue?).
不过有什么不工作时退出。我创建了以下路线为测试目的:
What doesn't work however is logging out. I created the following route for test purposes:
Route::get('logout', function(){
Auth::logout();
Session::flush();
dd(Auth::check());
});
结果,一如预期,假
。然而!如果我再转到我的登录页面,我重定向。访问下面的结果我等调试路线真
这使得绝对
毫无意义的。没有登录使用'记住'标志的用户。
The result is, as expected, false
. However! If I then go to my login page I get redirected. Visiting my other debug route below results in true
which makes absolutely
no sense at all. The user was not logged in using the 'remember' flag.
Route::get('authenticated', function(){
dd(Auth::check());
});
任何输入高度AP preciated在这一点上:)
Any input is highly appreciated at this point :)
===更新2 ===
看来previous成功的尝试只是一个侥幸。登录或注销后我手动登录自己在不工作。任何方法来调试这样的黑魔法?
It seems the previous successful attempt was just a fluke. Logging in, or logging out after I manually logged myself in doesn't work.. Any way to debug this kind of black magic?
===更新3 ===
今天,我想我的应用程序一次,是psented与无法写入会议文件XXXX错误$ P $。我检查了我的存储/会话文件夹,发现没有这样的文件存在。
Today I tried my app once more and was presented with an can't write to session file xxxx error. I checked my storage/sessions folder and found that no such file existed.
我清空存储/会话文件夹,清空所有的缓存,并怪足够的登录名和 注销我以前都没有了问题。
I emptied the storage/sessions folder, cleared all cache, and weird enough the login and logout problems I had before are gone.
尽管面临的首要问题是解决了(现在)我还是想找出什么摆在首位导致此问题
Even though the primary issue is solved (for now) I would still like to find out what caused this problem in the first place
推荐答案
在回答这个问题可以用类似的帖子中找到,谢谢@Razor!
The answer to this question can be found in a similar post, thanks @Razor!
<一个href="http://stackoverflow.com/questions/23536877/laravel-4-persisting-data-ongoing-jquery-ajax-submit">laravel 4持久化数据进行中 - jQuery的AJAX提交
这篇关于通过AJAX Laravel 4 AUTH,可能会发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!