Javascript 成功注册后登录Ember Simple Auth
我已为注册后的用户设置了自动身份验证: app/libs/auto-authenticate.jsJavascript 成功注册后登录Ember Simple Auth,javascript,authentication,ember.js,ember-app-kit,Javascript,Authentication,Ember.js,Ember App Kit,我已为注册后的用户设置了自动身份验证: app/libs/auto-authenticate.js export default Ember.SimpleAuth.Authenticators.OAuth2.extend({ authenticate: function(credentials) { if(!Ember.isEmpty
export default Ember.SimpleAuth.Authenticators.OAuth2.extend({
authenticate: function(credentials) {
if(!Ember.isEmpty(credentials.access_token)) {
return Ember.RSVP.resolve(credentials);
} else {
return this._super(credentials);
}
}
});
app/app.js
import AutoAuthenticate from 'appkit/libs/auto-authenticate';
App.initializer({
name: 'auto',
initialize: function(container, application) {
container.register('app:authenticators:custom', AutoAuthenticator);
Ember.SimpleAuth.setup(container, application);
}
});
app/controllers/register.js
export default Ember.ObjectController.extend({
firstname: '',
lastname: '',
username: '',
password: '',
actions: {
registerUser: function(){
var self = this;
var user = this.store.createRecord('user', {
first_name: this.get('firstname'),
last_name: this.get('lastname'),
username: this.get('username')
});
user.set('typedPass', this.get('password'));
user.save().then(function(){
//How can I login this user using ember-simple-auth ??
});
}
}
});
我有单独的用户登录,将提供他们的用户名和密码
我想做的是,当一个新用户在网站上注册时,我希望该用户直接登录,而不必进入登录页面并提供其用户名/密码?由于我从注册过程中获取用户名和密码,我不希望用户转到其他路径然后登录。如何调用自定义验证器以使用当前注册用户的登录凭据对其进行身份验证???最好的解决方案可能是重用注册控制器中已有的用户名和密码属性:
export default Ember.ObjectController.extend({
firstname: '',
lastname: '',
username: '',
password: '',
actions: {
registerUser: function(){
var self = this;
var user = this.store.createRecord('user', {
first_name: this.get('firstname'),
last_name: this.get('lastname'),
username: this.get('username')
});
user.set('typedPass', this.get('password'));
user.save().then(function() {
//this is basically what happens when you trigger the LoginControllerMixin's "authenticate" action
this.get('session').authenticate('app:authenticators:custom', {
identification: this.get('username'),
password: this.get('password')
});
});
}
}
})) 我也遇到了同样的问题,我无法从“this”或“self”获取会话/用户名/密码。所以我所做的是:
App.session = this.get('session');
App.credentials = {
identification: data.username,
password: data.password
};
然后我在承诺中使用了它们(使用默认验证器):
我们使用Desive,也有同样的问题。我对这个解决方案不太满意,但我们在对
user.save()
的响应中返回了designe auth令牌,并直接调用了session\setup
,如下所示:
user.save().then(function(user) {
var secure = self.session.get('secure');
secure.token = user.get('deviseAuthToken');
secure.email = user.get('email');
self.session.setup('simple-auth-authenticator:devise', secure, true);
}
使用新的ember simple auth 1.0插件,我们将此代码移动到自定义验证器中:
user.save().then(function() {
App.session.authenticate('ember-simple-auth:authenticators:oauth2', App.credentials);
}, function(err) {
console.log('error ..')
});
//app/authenticators/registration.js
import Ember from 'ember';
import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
const { set } = Ember;
export default DeviseAuthenticator.extend({
authenticate: function(registration) {
const { tokenAttributeName, identificationAttributeName } = this.getProperties('tokenAttributeName', 'identificationAttributeName');
const data = {};
return registration.save().then(function(response) {
set(data, tokenAttributeName, response.get('authToken'));
set(data, identificationAttributeName, response.get('email'));
set(data, 'registration', response.toJSON());
return data;
});
}
});
并在我们的注册表上的提交操作中触发此验证器:
//app/controllers/registration.js
export default Ember.Controller.extend({
session: Ember.inject.service('session'),
actions: {
submit: function() {
this.get('session')
.authenticate('authenticator:registration', this.get('model'));
}
}
}
我们的注册表绑定到一个注册模型对象,如下所示:
//app/models/registration.js
export default DS.Model.extend(SiteModelMixin, {
email : DS.attr("string"),
password : DS.attr("string"),
authToken : DS.attr("string")
});
Marcow的回答应该适用于大多数情况,但在我的情况下,注册响应包括完整的身份验证会话以及新创建用户的所有数据(即,登录和注册响应是相同的)。使用表单中相同的凭据执行登录请求稍微有点不理想。在这种情况下,b/c有一个额外的网络请求(可能由于网络状况不稳定等原因而失败) 在我的例子中,响应包括一个身份验证令牌,因此我只向我的自定义身份验证程序提供一个对象,该对象检查接收数据中的
身份验证\u令牌
,如果是,则假设数据是有效的用户会话(如果需要,可以对会话数据进行更严格的验证)并使用接收到的数据解析承诺,从而在会话服务中使用Ember Simple Auth对用户会话进行身份验证
如果没有身份验证令牌,我尝试使用身份验证者的
authenticate()
调用接收的数据对象中的email
和password
属性登录,并根据登录尝试的结果解决或拒绝。我不想使用另一个网络请求,当我的API返回带有注册响应的令牌时,我开始沿着Drew Nichols的路线前进,但不喜欢创建单独的模型。最后,我发现您可以简单地扩展适配器中模型的handleResponse()
hook。然后,您只需要一个简单的验证器,它将返回输入数据(因此通过身份验证过程运行该数据以将其添加到会话)
这些都在余烬2.7中
以下是用户
型号的适配器:
// app/adapters/user.js
export default ApplicationAdapter.extend({
session: Ember.inject.service(),
handleResponse(status, headers, payload, requestData) {
if (this.isSuccess(status, headers, payload) && requestData.method === 'POST') {
this.get('session').authenticate('authenticator:registration', payload).catch((reason) => {
console.error( 'User Adapter Error:', reason );
});
}
this._super(...arguments);
},
});
以及上面调用的注册
验证器:
// app/authenticators/registration.js
export default Base.extend({
authenticate(response) {
return new Ember.RSVP.Promise((resolve) => {
Ember.run.join(null, resolve, response);
});
},
});
然后,要调用控制器(或任何地方),只需执行常规的save()
:
有些部分我必须这样更改。get('session')。authenticate('app:authenticators:custom',{identification:this.get('username'),password:this.get('password')});to::self.get('session').authenticate('app:authenticators:custom',{identification:self.get('username'),password:self.get('password')});如果我刷新,我会得到以下错误Uncaught TypeError:无法读取未定义的属性'restore',第1136行this.container.lookup(authenticatorFactory).restore(restoredContent).then(函数(content){该错误表示Ember.SimpleAuth保留了一个验证器工厂,但它没有从容器中获取验证器实例。您可能注册的验证器不正确吗?我在这里实现的只是我用于正常登录的登录的OAuth2设置。以下是我使用的全部代码:到目前为止,我也是这么做的。现在我正在尝试迁移到ember simple auth 1.0,会话不再有设置()方法。有什么想法吗?在ember simple auth 1.0下,我们将上述函数移到了自定义验证器中。我将在单独的答案中发布代码,因为它不适合在评论回复中。我希望看到一些代码显示这是如何完成的。我是ember的新手,目前正在尝试解决这个确切的问题。
this.get('model').save()