【问题标题】:authentication using ember-simple-auth-devise and rails backend使用 ember-simple-auth-devise 和 rails 后端进行身份验证
【发布时间】:2015-10-13 17:19:53
【问题描述】:

我正在尝试使用以下版本在 ember 中实现身份验证系统:

DEBUG: -------------------------------
ember.debug.js:5378 DEBUG: Ember                    : 1.13.7
ember.debug.js:5378 DEBUG: Ember Data               : 1.13.8
ember.debug.js:5378 DEBUG: jQuery                   : 1.11.3
ember.debug.js:5378 DEBUG: Ember Simple Auth        : 0.8.0
ember.debug.js:5378 DEBUG: Ember Simple Auth Devise : 0.8.0
ember.debug.js:5378 DEBUG: -------------------------------

我按照https://github.com/simplabs/ember-simple-auth/tree/master/packages/ember-simple-auth-devise github 上的说明进行操作

现在当我点击“注册”时,它返回 500 错误:

POST http://localhost:4200/users/sign_in 500 (Internal Server Error)

我觉得应该去 localhost:3000/users/sign_in,但不是。

我使用ember s --proxy http://localhost:3000 命令启动了服务器,但不明白发生了什么。

这是我的参考代码:

控制器/login.js

import Ember from 'ember';
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';

export default Ember.Controller.extend(LoginControllerMixin, {
  authenticator: 'simple-auth-authenticator:devise'
});

路由/login.js

import Ember from 'ember';
import UnauthenticatedRouteMixin from 'simple-auth/mixins/unauthenticated-route-mixin';

export default Ember.Route.extend(UnauthenticatedRouteMixin);

模板/application.hbs

<h2 id="title">Welcome to Ember.js - With Authentication!</h2>

{{#if session.isAuthenticated}}
    Hi, {{session.email}}
    <br/>
    You can't see this text unless you're logged in!
    <br/>
    Click this button to logout:
    <button {{action 'invalidateSession'}}>Logout</button>
    <br/>
    If you try to go to the
    {{#link-to "login"}}Login{{/link-to}}
    page, you should get redirected!
{{else}}
  {{#link-to "login"}}Login{{/link-to}}
{{/if}}

{{outlet}}

模板/login.hbs

<form {{action "authenticate" on="submit"}}>
    <div>
        <label for="identification">E-mail</label><br />
      {{input value=identification placeholder="E-mail" type="text" name="email"}}
    </div>

    <div>
        <label for="password">Password</label><br />
      {{input value=password placeholder="Password" type="password" name="password"}}
    </div>

    <div>
        <button type="submit">Log in</button>
    </div>
</form>

config/environment.js

...
  ENV['simple-auth'] = {
    authorizer: 'simple-auth-authorizer:devise'
  }

  ENV['simple-auth-devise'] = {
    tokenAttributeName: 'token',
    identificationAttributeName: 'email'
  }
...

我的 Rails 路线如下所示:

  devise_for :users, controllers: { sessions: 'sessions' }

【问题讨论】:

  • 请求是否到达过 Rails 后端?
  • 是的,它返回,ActionController::RoutingError (No route matches [POST] "/users/sign_in"):(在上面添加了我的设计路线)
  • 这意味着 /users/sign_in 路由没有在 Rails 应用程序中配置,这是 500 错误

标签: ember.js ember-simple-auth


【解决方案1】:

在您的 routes.rb 文件中,您可能需要添加 at: 'users' 作为 devise 为 sign_in 提供的默认路由是 http://ip_address_to_your_server/sign_in 并创建您的自定义 URL,例如 http://localhost:3000/users/sign_in 您有将其映射到“用户”。生成的代码如下所示

devise_for :users, at 'users',controllers: { sessions: 'sessions' }

这样你就可以在 /users 调用 SessionsController

希望这会有所帮助。否则请考虑发布在 Rails 后端实现的代码,因为问题出在 Rails 端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多