[英]Handling user registration in an Ember.js/Rails/Devise app
I'm playing around with writing a pure Ember.js app on top of Rails 4 and I'm puzzled how user management is handled . 我正在玩Rails 4上的纯Ember.js应用程序, 我很困惑如何处理用户管理 。 My original idea was to use pure server-rendered templates (ERB) to do the user registration and login via Devise, and then the rest of the app would use the Ember framework.
我最初的想法是使用纯服务器呈现模板(ERB)来进行用户注册并通过Devise登录,然后应用程序的其余部分将使用Ember框架。
The problem with that is that Ember wants to take over the <body> tag and control the entire viewport. 问题在于Ember想要接管<body>标签并控制整个视口。 In this way I can't pick and choose which aspects of the app should use server-rendered Erb templates and which should live in the Ember logic.
通过这种方式,我无法选择应用程序的哪些方面应该使用服务器呈现的Erb模板,哪些方面应该存在于Ember逻辑中。
I see plenty of examples of how to deal with a user that's already logged-in and ember-auth looks interesting to facilitate authentication-aware controllers, but I've seen no tutorials or suggestions on allowing the full user signup experience to take place in the Ember app. 我看到很多关于如何处理已经登录的用户以及ember-auth看起来很有趣以促进身份验证感知控制器的示例,但我没有看到关于允许完整用户注册体验的教程或建议Ember应用程序。
Am I missing something, either from a technical perspective where I just haven't found the right code or from a architectural perspective where I shouldn't be doing it this way? 我是否遗漏了某些东西,无论是从技术角度来看,我还没有找到正确的代码,或者从架构角度来看,我不应该这样做?
This is with ember-rails (0.12.0 w/1.0.0.rc3.3 ember-source), Rails 4.0.0.rc1, and Devise (rails4 branch). 这是使用ember-rails(0.12.0 w / 1.0.0.rc3.3 ember-source),Rails 4.0.0.rc1和Devise(rails4分支)。
ember-auth
dev here. ember-auth
dev在这里。
You don't actually need any special treatment for user sign up. 您实际上并不需要对用户注册进行任何特殊处理。 Treat user sign up as you would for another model, in the sense that creating a
user
model will not require authentication. 像对待其他模型一样对待用户注册,因为创建
user
模型不需要身份验证。 (Editing it or deleting it should require authentication though.) (编辑或删除它应该要求身份验证。)
Your implementation might look like: 您的实现可能如下所示:
App.User = DS.Model.extend
email: DS.attr 'string'
password: DS.attr 'string'
App.UsersNewRoute = Em.Route.extend
model: ->
App.User.createRecord()
App.UsersNewController = Em.ObjectController.extend
create: ->
@store.commit()
Error-checking, template code, etc, skipped for brevity. 为简洁起见,跳过错误检查,模板代码等。
This is here for reference to what worked based off of @heartsentwined's answer since pasting in comments doesn't work very well. 这是为了参考基于@ heartsentwined的答案所做的工作,因为在评论中粘贴效果不是很好。 See the comments for more info.
有关详细信息,请参阅注释。 Since my api returns the user json I just pass in the format its expecting.
由于我的api返回用户json,我只是传递它期望的格式。
didCreate: function() {
var user = App.Auth.get('_response').response.user;
var auth = {auth_token: user.auth_token, id: user.id};
App.Auth.get('_response').canonicalize(auth);
App.Auth.trigger('signInSuccess');
}
UPDATE: I switched to ember-model and now do this in the same place that I call model.save()
(the submit
action of SignupController). 更新:我切换到ember-model,现在在我调用
model.save()
( model.save()
的submit
动作model.save()
的同一个地方执行此操作。
var model = this.get('model');
model.on('didCreateRecord', function() {
var user = this.data;
var auth = {auth_token: user.auth_token, user_id: user.id, remember_token: user.remember_token};
App.Auth.get('_response').canonicalize(auth);
App.Auth.trigger('signInSuccess');
});
model.save();
The solutions above ALMOST but not quite worked for me. 上面的解决方案几乎没有,但对我来说并不是很有用。 Here is what did work:
这是做了什么工作:
didCreate: function() {
var user = App.Auth.get('_response').response.user;
App.Auth.signIn({
data: {
'email': user.email,
'password': this.get('password'),
'remember': true
}
});
}
App.Auth.signIn is used in the documentation explicitly: http://ember-auth.herokuapp.com/docs App.Auth.signIn明确用于文档: http ://ember-auth.herokuapp.com/docs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.