Ember.js Ember SimpleStorage中的简单认证持久认证信息不起作用 [英] Ember.js Ember Simple Auth persist authentication information in LocalStorage does not work

查看:77
本文介绍了Ember.js Ember SimpleStorage中的简单认证持久认证信息不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Ember Simple Auth 进行以下设置:
注意:我使用Ember App Kit。



app.js

 code> // init Ember.SimpleAuth 
App.initializer({
name:'authentication',
initialize:function(container,application){
Ember.SimpleAuth .setup(应用程序,{// @todo在Ember-simple-auth的版本0.1.2,添加容器变量
crossOriginWhitelist:['http:// customdomain'],
// store:Ember 。$ s

});

export
默认App;

一个简单的loginController
(主要从 Ember App Kit简单验证

  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,{
authentator:CustomAuthenticator,

actions:{
// display登录失败时的错误
sessionAuthenticationFailed:function(message){
console.log('sessionAuthenticationFailed');
this.set('errorMessage',message);
} ,

// handle login success
sessionAuthenticationSucceeded:function(){
console.log('sessionAuthenticationSucceeded');

this.set('
this.set('identification',);
this.set('password',);
this._super();
}
}
});

export
default LoginController;

到目前为止这么好,我可以认证用户想一个登录表单。但是当我按F5时,我必须重新登录。 LocalStorage适配器为空。所以问题是我需要坚持令牌和会话吗?



注意:我无法更新到ember-simple-auth 0.1.2,bower找不到新的版。似乎github版本的 https://github.com/simplabs/ember-simple-auth - 组件不是最新的。



编辑:
我已经更新了我的代码如下: p>

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(容器,应用程序,{// @todo版本0.1.2的Ember-simple-auth,添加容器
crossOriginWhitelist:['http:// customdomain'],// @todo remove live live
// store:Ember.SimpleAuth.Stores.LocalStorage,
authenticationRoute:'article.login'
} );
}
});

导出默认应用程序;

loginController:

  var LoginController = Ember.Controller.extend(Ember.SimpleAuth.LoginControllerMixin,{
// authenticationator:CustomAuthenticator,//不需要

操作:{
//登录失败时显示错误
sessionAuthenticationFailed:function(message){
this.set('errorMessage',message);
},

//处理登录成功
sessionAuthenticationSucceeded:function(){
this.set('errorMessage',);
this.set('identification' );
this.set('password',);
this._super();
}
}
});

导出默认LoginController;


解决方案

这应该用0.1.2:github修复。 com / simplabs / ember-simple-auth / releases / tag / 0.1.2



我也刚刚更新github.com/simplabs/ember-simple-auth-component


I use Ember Simple Auth with the following settings: Note: I use Ember App Kit.

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)

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. The LocalStorage adapter is empty. 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. Seems that the github version of https://github.com/simplabs/ember-simple-auth-component is not up to date.

Edit: I have updated my code as follows:

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:

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;

解决方案

This should be fixed with 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

这篇关于Ember.js Ember SimpleStorage中的简单认证持久认证信息不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆