【发布时间】:2015-02-09 05:10:21
【问题描述】:
我使用 Ember Simple Auth(不是 Ember CLI 版本)进行了从 0.6.4 到 0.7.2 的更新以进行设计,现在我的身份验证根本不起作用 :(,你有什么想法吗?非常感谢为您的帮助:)
PS : 显然,在 authenticate_with_http_token 执行 |token, options| 之后,ApplicationController (application_controller.rb) 不会继续。并且 authenticate_with_http_token 为空(用 puts 测试)
login_controller.js
App.LoginController = Ember.Controller.extend(SimpleAuth.LoginControllerMixin, {
authenticator: 'simple-auth-authenticator:devise'
//authenticator: 'authenticator:custom'
});
application.js.coffee
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',
};
login.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.coffee
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.coffee
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
【问题讨论】:
-
您可以在应用程序路由中实现
sessionAuthenticationSucceeded(当您使用ApplicationRouteMixin时),而不是在初始化程序中列出会话事件 - 使其更简单。 -
是的,很好的建议 :),谢谢 :)
标签: ruby-on-rails ember.js devise coffeescript ember-simple-auth