使用 ember-simple-auth-devise 和 rails 后端进行身份验证

authentication using ember-simple-auth-devise and rails backend

我正在尝试在 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: -------------------------------

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

现在当我点击 'sign up' 它返回 500 错误:

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

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

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

这是我的代码供参考:

controller/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'
});

routes/login.js

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

export default Ember.Route.extend(UnauthenticatedRouteMixin);

templates/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}}

templates/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' }

在您的 routes.rb 文件中,您可能需要添加 at: 'users' 因为 devise 为 sign_in 提供的默认路由是 http://ip_address_to_your_server/sign_in and to create your custom url like http://localhost:3000/users/sign_in 你必须将它映射到 'users'。生成的代码看起来像

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

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

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