Aurelia身份验证-自定义响应消息-控制台错误 [英] Aurelia auth - custom response message - console error

查看:83
本文介绍了Aurelia身份验证-自定义响应消息-控制台错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

aurelia-auth插件与.NET Core webAPI一起使用,我希望返回用户尝试使用不正确的用户名和/或密码登录时出现自定义错误.

Using this aurelia-auth plugin together with .NET Core webAPI, I wish to return a custom error when user tries to log in with incorrect username and/or password.

WebAPI:

public HttpResponseMessage Login()
{
    if (passwordValid)
    {
        return Request.CreateResponse(new LoginResponseViewModel { Token = token });
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.Unauthorized, "Incorrect username or password!");
    }
}

脚本

logIn(email, password) {
    return this.auth.login(email, password)
        .then(response => {
            console.log("success logged " + response);
        })
        .catch(err => {
            console.log("login failure: " + err);
        });
}

  1. 如何在登录功能捕获错误中访问未经授权的响应自定义消息用户名或密码不正确"?
  2. 似乎aurelia-auth登录功能必须接收httpStatusCode.Unauthorized才能使它理解密码/用户名不正确,但这会生成401 (Unauthorized)控制台错误.我可以抑制此控制台错误,还是返回aurelia-auth可以理解的json结果?
  1. How do I access my unauthorized response custom message "Incorrect username or password" in the login function catch error?
  2. It seems like the aurelia-auth login function must receive a httpStatusCode.Unauthorized in order for it to understand that the password/username was incorrect, but that generates a 401 (Unauthorized) console error. Can I suppress this console error or perhaps return a json result which aurelia-auth understands?

推荐答案

1.如何访问自定义的未经授权的响应消息

服务器响应:

return Request.CreateResponse(HttpStatusCode.Unauthorized, "My message");

客户:

return this.auth.login(email, password)
    .then(response => {
        console.log("success logged " + response);
    })
    .catch(error => error.json().then(serverError =>
        console.log(serverError) //My message 
    ));

2.抑制控制台错误或返回aurelia-auth可以理解的json结果

当前版本的aurelia-auth无法返回aurelia-auth可以理解的json结果.

2. Suppress console error or return a json result which aurelia-auth understands

It is not possible in the current version of aurelia-auth to return a json result that aurelia-auth understand.

我决定将我当前的 aurelia-auth 包替换为

I decided to replace my current aurelia-auth package with aurelia-authentication.

Aurelia认证卫星化程序库.有了新软件包,我现在可以做更多的事情(刷新令牌,从服务器返回自定义消息等).

Aurelia-authentication is fork of aurelia-auth which itself is a port of the great Satellizer library. With the new package I can now do much more, (refresh token, return custom messages from server etc)..

这篇关于Aurelia身份验证-自定义响应消息-控制台错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆