简体   繁体   English

断言用户通过AJAX从前端登录

[英]Asserting the user is logged in from the front-end via AJAX

My problem is as follows: 我的问题如下:

I have a rails (3.2.8) application that uses devise for authentication. 我有一个使用devise进行身份验证的Rails(3.2.8)应用程序。 Rails is just a RESTful api and the front-end is a backbone one-page app. Rails只是一个RESTful api,前端是骨干的一页应用程序。

I manage to register and sign-in users, but how can the front-end get that information from the back-end with AJAX ? 我设法注册并登录用户,但是前端如何使用AJAX从后端获取该信息? Basically I want to be able to tell from the front end that this user is logged in and has a session, or tell that the user has not logged in. 基本上,我希望能够从前端告知该用户已登录并具有会话,或者告诉该用户尚未登录。

Currently I my controllers and models are completely rails-created, I have not touched them. 目前,我的控制器和模型完全是用Rails创建的,我还没有碰过。

Googling around produced many tutorials that use the token_authenticatable module, but they had varying implementations and did not offer a explicit solution. 在Google上搜索了许多使用token_authenticatable模块的教程,但是它们具有不同的实现方式,并且没有提供明确的解决方案。

A simple way to do it is let rails handle your sessions. 一种简单的方法是让Rails处理您的会话。 You do this by: 您可以这样操作:

rails generate session_migration
rake db:migrate

Then, store a session value in rails for example: 然后,将会话值存储在rails中,例如:

session[:user_id] = user.id

(where user is an object of your User class) (其中user是您的User类的对象)

And then you can constantly ping your rails backend via ajax to check whether the session[:user_id] variable is not null, if it isn't null then a session exists. 然后,您可以通过ajax不断对rails后端执行ping操作,以检查session [:user_id]变量是否不为null,如果不为null,则表明存在会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM