我使用 Ember Simple Auth(不是 Ember CLI 版本)从 0.6.4 更新到 0.7.2 进行设计,现在我的身份验证根本不起作用 :(,你有想法吗? 非常感谢您的帮助:)
PS :显然,应用程序控制器(application_controller.rb)在authenticate_with_http_token执行|token,options|并且authenticate_with_http_token为空后不再继续(使用看跌期权测试)
login_controller.js
App.LoginController = Ember.Controller.extend(SimpleAuth.LoginControllerMixin, {
authenticator: 'simple-auth-authenticator:devise'
//authenticator: 'authenticator:custom'
});
应用.js.咖啡
Ember.Application.initializer
name: "authentication"
after: "simple-auth"
initialize: (container, application) ->
applicationRoute = container.lookup("route:application")
session = container.lookup("simple-auth-session:main")
# handle the session events
session.on "sessionAuthenticationSucceeded", ->
applicationRoute.transitionTo "Myspace"
return
return
window.ENV = window.ENV || {}
window.ENV["simple-auth"] = { store: 'simple-auth-session-store:local-storage', authorizer: "simple-auth-authorizer:devise" };
window.ENV['simple-auth-devise'] = {
crossOriginWhitelist: ['*'],
serverTokenEndpoint: 'users/sign_in',
};
登录.hbs
<br />
<div class="row">
<div class="large-12 columns">
<form {{action 'authenticate' on='submit'}}>
<label for="identification">Login</label>
{{input id='identification' placeholder='Enter Login' value=identification}}
<label for="password">Password</label>
{{input id='password' placeholder='Enter Password' type='password' value=password}}
<button type="submit">Login</button>
</form>
</div>
</div>
login_route.js.咖啡
App.LoginRoute = Ember.Route.extend(
#model: (params) ->
#return @store.find('user', @get('session.user_id'))
setupController: (controller, model) ->
#controller.set "content", model
controller.set "errorMessage", null
return
actions:
sessionAuthenticationFailed: (responseBody) ->
message = responseBody.error
@controller.set "errorMessage", message
console.log "errorMessage : " + message
return )
myspace_route.js.咖啡
App.MyspaceRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, ....)
session_controller.rb
class SessionsController < Devise::SessionsController
def create
respond_to do |format|
format.html { super }
format.json do
self.resource = warden.authenticate!(auth_options)
sign_in(resource_name, resource)
data = {
user_token: self.resource.authentication_token,
user_email: self.resource.email
}
render json: data, status: 201
end
end
end
end
application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :null_session,
if: Proc.new { |c| c.request.format =~ %r{application/json} }
before_filter :skip_trackable, :authenticate_user_from_token!
private
def skip_trackable
request.env['warden'].request.env['devise.skip_trackable'] = '1'
end
def authenticate_user_from_token!
puts "authentification"
puts authenticate_with_http_token
authenticate_with_http_token do |token, options|
user_email = options[:user_email].presence
user = user_email && User.find_by_email(user_email)
puts "user.authentication_token"
puts user.authentication_token
puts token
puts "token"
if user && Devise.secure_compare(user.authentication_token, token)
sign_in user, store: false
end
end
end
end
您正在初始值设定项中设置在'simple-auth'
初始值设定项之后运行的初始值设定项中设置 window.ENV
对象,以便 Ember Simple Auth 实际上无法看到在其初始值设定项运行时设置的值。确保在运行'simple-auth'
初始值设定项之前设置这些值。
当然,您还应该切换到Ember CLI,;)
运行调试器后,它会转到:
余烬简单身份验证.js
authenticate: function() {
var args = Array.prototype.slice.call(arguments);
var authenticator = args.shift();
Ember.assert('Session#authenticate requires the authenticator factory to be specified, was ' + authenticator, !Ember.isEmpty(authenticator));
var _this = this;
var theAuthenticator = this.container.lookup(authenticator);
Ember.assert('No authenticator for factory "' + authenticator + '" could be found', !Ember.isNone(theAuthenticator));
return new Ember.RSVP.Promise(function(resolve, reject) {
theAuthenticator.authenticate.apply(theAuthenticator, args).then(function(content) {
_this.setup(authenticator, content, true);
resolve(); // <- it goes to here
}, function(error) {
_this.clear();
_this.trigger('sessionAuthenticationFailed', error);
reject(error);
});
});
},
带有令牌的 json 响应似乎还可以,身份验证器配置似乎也可以......
我在这个承诺中也有一个"拒绝"
余烬简单身份验证.js
restore: function() {
var _this = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
var restoredContent = _this.store.restore();
var authenticator = restoredContent.authenticator;
if (!!authenticator) {
delete restoredContent.authenticator;
_this.container.lookup(authenticator).restore(restoredContent).then(function(content) {
_this.setup(authenticator, content);
resolve();
}, function() {
_this.store.clear();
reject();
});
} else {
_this.store.clear();
reject();
}
});
},
拒绝承诺的痕迹:
VM7522:164 Ember Inspector (Promise Trace):
at new Promise (http://localhost:3000/assets/ember.js?body=1:10174:9)
at __exports__.default.Ember.ObjectProxy.extend.restore (http://localhost:3000/assets/ember-simple-auth.js?body=1:1116:16)
at __exports__.default (http://localhost:3000/assets/ember-simple-auth.js?body=1:1337:15)
at __exports__.default.initialize (http://localhost:3000/assets/ember-simple-auth.js?body=1:447:9)
at http://localhost:3000/assets/ember.js?body=1:43164:11
at visit (http://localhost:3000/assets/ember.js?body=1:43556:7)
at DAG.topsort (http://localhost:3000/assets/ember.js?body=1:43610:11)
at Namespace.extend.runInitializers (http://localhost:3000/assets/ember.js?body=1:43161:15)
at Namespace.extend._initialize (http://localhost:3000/assets/ember.js?body=1:43046:14)
编辑 1: 还有这个:
余烬-简单-身份验证-设计.js
restore: function(properties) {
var _this = this;
var propertiesObject = Ember.Object.create(properties);
return new Ember.RSVP.Promise(function(resolve, reject) {
if (!Ember.isEmpty(propertiesObject.get(_this.tokenAttributeName)) && !Ember.isEmpty(propertiesObject.get(_this.identificationAttributeName))) {
resolve(properties);
} else {
reject();
}
});
},
带痕迹 :
Ember Inspector (Promise Trace):
at new Promise (http://localhost:3000/assets/ember.js?body=1:10174:9)
at __exports__.default.Base.extend.restore (http://localhost:3000/assets/ember-simple-auth-devise.js?body=1:156:16)
at apply (http://localhost:3000/assets/ember.js?body=1:7993:27)
at superWrapper [as restore] (http://localhost:3000/assets/ember.js?body=1:7571:15)
at http://localhost:3000/assets/ember-simple-auth.js?body=1:1121:51
at invokeResolver (http://localhost:3000/assets/ember.js?body=1:10192:9)
at new Promise (http://localhost:3000/assets/ember.js?body=1:10178:9)
at __exports__.default.Ember.ObjectProxy.extend.restore (http://localhost:3000/assets/ember-simple-auth.js?body=1:1116:16)
at __exports__.default (http://localhost:3000/assets/ember-simple-auth.js?body=1:1337:15)
在 marcoow 的帮助下,刚刚修改了 https://github.com/simplabs/ember-simple-auth/tree/master/packages/ember-simple-auth-devise#server-side-setup SessionsController 像这样:
class SessionsController < Devise::SessionsController
def create
respond_to do |format|
format.html { super }
format.json do
self.resource = warden.authenticate!(auth_options)
sign_in(resource_name, resource)
@data = {
user_token: self.resource.authentication_token,
user_email: self.resource.email
}
render json: @data.to_json, status: 201
end
end
end
end
现在它正在工作
编辑 : to_json 解释 : http://apidock.com/rails/ActiveRecord/Serialization/to_json