会话保持跨页面重载骨干JS [英] Keep Session Across Page Reload In Backbone JS
问题描述
我耗费返回登录时没有Cookie的API,但只有一个的auth_token作为JSON响应的一部分。
I am consuming an API which returns no cookie on login,but only an auth_token as part of json response.
基URL提供了backbonejs应用。 http://www.webiyo.com/ ,并通过默认的登录页面。
The base URL delivers the backbonejs application. say http://www.webiyo.com/ and by default login page is displayed.
我要保留用户会话,即使用户刷新backbonejs应用程序的页面。
I want to keep the user session even if the user refreshes the page in backbonejs app.
问题是,我的API不返回会话cookie又名AUTH_TOKEN饼干(它只是返回所需要的查询字符串的所有后续调用传递的响应JSON的AUTH_TOKEN)
Problem is that my API doesn't return session cookie aka auth_token cookie ( it just returns an auth_token in a response json which is needed to be passed on all the subsequent calls in the query string )
因此,在这种情况下,我可以设置在我自己的一个javascript使用的cookie在backbonejs浏览器端来跟踪用户会话?如果是的话怎么样?
So in such situation could I set a javascript cookie on my own to track user session at browser side using backbonejs ? if yes how ?
我已经提到
的http://what$c$ccraves.com/articles/2012/01/11/backbonejs-sessions-and-authentication但它假定的HTTP cookie的auth_token直接从服务器发送到浏览器上登录成功,不像我情况下的AUTH_TOKEN是LOGN成功JSON响应的一部分。
http://whatcodecraves.com/articles/2012/01/11/backbonejs-sessions-and-authentication but it assumes the http cookie "auth_token" is sent directly from the server to browser on login success unlike my case where the the "auth_token" is the part of the json response on logn success.
推荐答案
根据你所描述的东西,存储的auth_token
在浏览器会话cookie似乎是方式去了。
Based on what you've described, storing the auth_token
in browser's session cookie seems to be the way to go.
Backbone.js的不支持饼干操纵。但是,您可以使用jQuery插件/写自己的cookie手处理这个问题。
Backbone.js doesn't support cookie manipulation. However, you can use a jQuery plugin/write your own cookie manipulator to handle this.
假设你使用 jQuery的cookie的
( https://github.com/carhartl/jquery-cookie ),这里是你如何能做到这一点(这是对他们的维基太为例):
Assuming you're using jquery-cookie
(https://github.com/carhartl/jquery-cookie), here's an example on how you can achieve this (It's on their Wiki too!):
$.cookie('auth_token', authTokenValue);
有关您与之交互的API,取决于他们如何接受的auth_token
,您可能需要创建Backbone.Model之上的BaseModel处理使用的auth_token
自动
For the API you interact with, depends on how they accept the auth_token
, you may need to create a BaseModel on top of Backbone.Model to handle the use of auth_token
automatically.
例如,如果API希望你通过的auth_token
作为查询字符串的一部分,你需要重写 Backbone.Model。网址()
功能:
For example, if the API expects you to pass the auth_token
as a part of QueryString, you'll need to override Backbone.Model.url()
function:
var BaseModel = Backbone.Model.extend({
url: function() {
var base = _.result(this, 'urlRoot') || _.result(this.collection, 'url') || urlError();
if (this.isNew()) return base + '?' + $.cookie('auth_token');
return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.id) + '?' + $.cookie('auth_token');
}
});
var EntityModel = BaseModel.extend({ ... });
那么,如何覆盖Backbone.Model依赖于API端点所期待的。
So, how you override the Backbone.Model depends on what the API endpoint expects.
如果以下这是你的最终目标是什么,有几件事情可以做:
If following this is what your ultimate goal is, there are couple things you can do:
App.Models.Session = Backbone.Model.extend
defaults:
access_token: null,
user_id: null
initialize: ->
@load()
authenticated: ->
Boolean(@get("auth_token"))
login: (email, password, options)->
# make an AJAX call to the authentication API
# once returned, call @save(...) with auth_token that you got back.
# options is there to facilitate that, if you want to pass in an onAuthencated or onNotAuthenticated callbacks, you can.
# Saves session information to cookie
save: (auth_token)->
$.cookie('auth_token', auth_token)
# Loads session information from cookie
load: ->
@set
access_token: $.cookie('auth_token')
App.start = ->
@session = new App.Models.Session()
if @session.authenticated()
# redirect to user page
else
# launch a login form
# call @session.login(email, password)
作为一个方面说明,而不是使用@login(电子邮件,密码,期权)的选项
参数,还可以触发一个事件,如 @trigger('验证')
从模型,让查看/应用程序知道用户现在通过身份验证。
As a side note, instead of using the option
param in @login(email, password, options), you can also trigger an event, like @trigger('authenticated')
from the model, letting the View/App knows that the user is now authenticated.
这篇关于会话保持跨页面重载骨干JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!