[英]Ember.js Ember Simple Auth persist authentication information in LocalStorage does not work
I use Ember Simple Auth with the following settings: Note: I use Ember App Kit. 我将Ember Simple Auth用于以下设置:注意:我使用Ember App Kit。
app.js app.js
// init Ember.SimpleAuth
App.initializer({
name: 'authentication',
initialize: function(container, application) {
Ember.SimpleAuth.setup(application, { // @todo at version 0.1.2 of Ember-simple-auth, add container variable
crossOriginWhitelist: ['http://customdomain'],
// store: Ember.SimpleAuth.Stores.LocalStorage, // default now
authenticationRoute: 'article.login'
});
}
});
export
default App;
a simple loginController (took it mostly from Ember App Kit Simple Auth ) 一个简单的loginController (主要是从Ember App Kit的Simple Auth获取 )
var CustomAuthenticator = Ember.SimpleAuth.Authenticators.OAuth2.extend({
serverTokenEndpoint: 'http://customdomain/access_token/',
makeRequest: function(data) {
return Ember.$.ajax({
url: this.serverTokenEndpoint,
type: 'POST',
data: {
grant_type: 'password',
username: data.username,
password: data.password
},
dataType: 'json',
contentType: 'application/x-www-form-urlencoded'
});
}
});
var LoginController = Ember.Controller.extend(Ember.SimpleAuth.LoginControllerMixin, {
authenticator: CustomAuthenticator,
actions: {
// display an error when logging in fails
sessionAuthenticationFailed: function(message) {
console.log('sessionAuthenticationFailed');
this.set('errorMessage', message);
},
// handle login success
sessionAuthenticationSucceeded: function() {
console.log('sessionAuthenticationSucceeded');
this.set('errorMessage', "");
this.set('identification', "");
this.set('password', "");
this._super();
}
}
});
export
default LoginController;
So far so good, I can authenticate a user thought a login form. 到目前为止,我可以通过登录表单对用户进行身份验证。 However when I press F5, I have to login again.
但是,当我按F5键时,我必须再次登录。 The LocalStorage adapter is empty.
LocalStorage适配器为空。 So the question is what do I need to persist the token and session?
所以问题是我需要什么来持久保存令牌和会话?
Note: I cannot update to ember-simple-auth 0.1.2, bower cannot find the new version. 注意:我无法更新到ember-simple-auth 0.1.2,bower无法找到新版本。 Seems that the github version of https://github.com/simplabs/ember-simple-auth-component is not up to date.
似乎https://github.com/simplabs/ember-simple-auth-component的github版本不是最新的。
Edit: I have updated my code as follows: 编辑:我已更新我的代码,如下所示:
app.js app.js
// init Ember.SimpleAuth
App.initializer({
name: 'authentication',
initialize: function(container, application) {
Ember.SimpleAuth.Authenticators.OAuth2.reopen({
serverTokenEndpoint: 'http://customdomain/access_token'
});
Ember.SimpleAuth.setup(container, application, { // @todo at version 0.1.2 of Ember-simple-auth, add container
crossOriginWhitelist: ['http://customdomain'], // @todo remove when live
// store: Ember.SimpleAuth.Stores.LocalStorage,
authenticationRoute: 'article.login'
});
}
});
export default App;
loginController: 的LoginController:
var LoginController = Ember.Controller.extend(Ember.SimpleAuth.LoginControllerMixin, {
// authenticator: CustomAuthenticator, // not needed anymore
actions: {
// display an error when logging in fails
sessionAuthenticationFailed: function(message) {
this.set('errorMessage', message);
},
// handle login success
sessionAuthenticationSucceeded: function() {
this.set('errorMessage', "");
this.set('identification', "");
this.set('password', "");
this._super();
}
}
});
export default LoginController;
I haven't used the oauth2 authenticator before (just a custom one for my backend that I wrote) but I think the same concepts should apply. 我以前没有使用过oauth2身份验证器(只是为我编写的后端定制了一个身份验证器),但我认为应该应用相同的概念。
When you refresh the page ember-simple-auth makes a call to the restore
method of the oauth2 authenticator that you are using. 刷新页面时,ember-simple-auth调用您所使用的oauth2身份验证器的
restore
方法。 The restore
method is looking for a property called 'access_token' to confirm that the user has already authenticated with your server. restore
方法正在寻找一个名为“ access_token”的属性,以确认用户已通过您的服务器进行身份验证。 Does your REST API return a property called access_token
when you authenticate with the endpoint at http://customdomain/access_token/
? 当您通过位于
http://customdomain/access_token/
的端点进行身份验证时,您的REST API是否会返回名为access_token
的属性? If not, you want to make sure this is happening or you will encounter the refresh issue you're having. 如果没有,则要确保这种情况正在发生,否则您将遇到刷新问题。 Here's the restore method in the oauth2 authenticator provided with ember-simple auth:
这是ember-simple auth随附的oauth2身份验证器中的restore方法:
restore: function(properties) {
var _this = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
// It looks for the 'access_token' property here which should have been set
// by the authenticate method if you returned it from your REST API
if (!Ember.isEmpty(properties.access_token)) {
_this.scheduleAccessTokenRefresh(properties.expires_in,
properties.expires_at,
properties.refresh_token);
resolve(properties);
} else {
reject();
}
});
}
Additionally, I think in your sessionAuthenticationSucceeded action you need to return true
. 另外,我认为在sessionAuthenticationSucceeded操作中,您需要
return true
。 Otherwise the action won't propagate up to the ember-simple-auth ApplicationRouteMixin (unless you are not using that mixin or don't depend on its sessionAuthenticationSucceeded method in which case it doesn't matter). 否则,该操作将不会传播到ember-simple-auth ApplicationRouteMixin(除非您不使用该mixin或不依赖于它的sessionAuthenticationSucceeded方法,在这种情况下都没有关系)。
This should be fixed with 0.1.2: github.com/simplabs/ember-simple-auth/releases/tag/0.1.2 这应该用0.1.2修复:github.com/simplabs/ember-simple-auth/releases/tag/0.1.2
I also just updated github.com/simplabs/ember-simple-auth-component 我也刚刚更新了github.com/simplabs/ember-simple-auth-component
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.