简体   繁体   English

Ember Simple Auth登录错误未显示在页面上

[英]Ember Simple Auth login errors not displaying on page

I am following an online video tutorial that has setting up Ember Simple Auth Login. 我正在关注一个在线视频教程,该教程设置了Ember Simple Auth Login。 Everything works with exception to displaying login errors (ie, 401 unauthorized) to the template. 一切正常,但向模板显示登录错误(即未经授权的401)。 I have checked the code for typos but cannot find any. 我已经检查了输入错误的代码,但找不到任何错误。

In the controller login, I tried logging the e.errors to the console, but the console just says 'Undefined'. 在控制器登录中,我尝试将e.errors记录到控制台,但控制台仅显示“未定义”。 Though, if I just send the error object e to the log then I get the error object with the error.detail inside of it. 但是,如果我只是将错误对象e到日志, error.detail在其中包含error.detail的错误对象。 Note: the error.detail is what I am trying to display on the login-form.hsb. 注意: error.detail是我试图在login-form.hsb上显示的内容。

Any help is appreciated! 任何帮助表示赞赏!

  • Ember : 3.4.5 灰烬:3.4.5
  • Ember Data : 3.4.2 灰烬数据:3.4.2
  • jQuery : 3.3.1 的jQuery:3.3.1
  • Ember Simple Auth : 1.7.0 灰烬简单身份验证:1.7.0

Controllers/login.js 控制器/login.js

import { inject as service } from '@ember/service';
import Controller from '@ember/controller';

export default Controller.extend({
    session: service('session'),

    actions: {
        login(attrs) {
            this.get('session').authenticate('authenticator:jwt', {
                email: attrs.email,
                password: attrs.password
            }).then(() => {
                this.transitionToRoute('index');
            }).catch((e) => {
                this.set('errors', e.errors); // nothing is being displayed
                console.log(e.errors); // Says Undefined in the console
                console.log(e); // Display error to console
            });
        }
    }
});

templates/login.hsb 模板/login.hsb

{{login-form user=model errors=errors onsubmit=(action "login")}}

templates/components/login-form.hbs 模板/组件/login-form.hbs

<div class="slide-out">
    <div class="slide-out-card">
        <div class="slide-out-heading">
            <div class="title">
                <h3>Login</h3>
            </div>
        </div>

        <div class="slide-out-content">
            <form onsubmit={{action "login"}}>
                <!-- this does not display the error message -->
                {{#each errors as |error|}}
                    {{error.detail}}
                    <div class="error-alert">{{error.detail}}</div>
                {{/each}}
                <div class="field">
                    <label>Email:</label>
                    {{input type="text" placeholder="Email" value=email}}
                </div>

                <div class="field">
                    <label>Password:</label>
                    {{input type="password" placeholder="Password" value=password}}
                </div>

                <div class="form-footer">
                    <button type="submit" class="btn-submit">save</button>
                </div>
            </form>
        </div>
    </div>
</div>

The answer was received from Embercasts. 答案是从Embercasts收到的。 There is a breaking change in Simple Auth. 简单身份验证有一个重大变化。 The code should now be e.json.errors 该代码现在应该是e.json.errors

export default Controller.extend({
    session: service('session'),

    actions: {
        login(attrs) {
            this.get('session').authenticate('authenticator:jwt', {
                email: attrs.email,
                password: attrs.password
            }).then(() => {
                this.transitionToRoute('index');
            }).catch((e) => {
                this.set('errors', e.json.errors); // Breaking change to Simple Auth - should be e.json.errors, instead of e.errors
            });
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM