From da38f7a6ba6a24b5d51efb66424f16de06bb48ce Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Thu, 25 Mar 2021 23:22:34 +0800 Subject: [PATCH] Improve login error handling. --- controllers/auth.go | 5 +++- web/src/auth/AuthCallback.js | 14 ++++++++-- web/src/auth/Face.js | 51 +++--------------------------------- web/src/auth/Util.js | 48 ++++++++++++++++++++++++++++++++- 4 files changed, 66 insertions(+), 52 deletions(-) diff --git a/controllers/auth.go b/controllers/auth.go index 17a1453a..b604f154 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -116,7 +116,10 @@ func (c *ApiController) Login() { // https://github.com/golang/oauth2/issues/123#issuecomment-103715338 token, err := idProvider.GetToken(form.Code) if err != nil { - panic(err) + resp = &Response{Status: "error", Msg: err.Error()} + c.Data["json"] = resp + c.ServeJSON() + return } if !token.Valid() { diff --git a/web/src/auth/AuthCallback.js b/web/src/auth/AuthCallback.js index 1f4f9cd2..57ae2cf2 100644 --- a/web/src/auth/AuthCallback.js +++ b/web/src/auth/AuthCallback.js @@ -24,6 +24,7 @@ class AuthCallback extends React.Component { super(props); this.state = { classes: props, + msg: null, }; } @@ -82,7 +83,10 @@ class AuthCallback extends React.Component { // Util.showMessage("success", `Authorization code: ${res.data}`); } } else { - Util.showMessage("error", `Log in failed:${res.msg}`); + // Util.showMessage("error", `Log in failed:${res.msg}`); + this.setState({ + msg: res.msg, + }); } }); } @@ -90,7 +94,13 @@ class AuthCallback extends React.Component { render() { return (
- + { + (this.state.msg === null) ? ( + + ) : ( + Util.renderMessageLarge(this.state.msg) + ) + }
) } diff --git a/web/src/auth/Face.js b/web/src/auth/Face.js index 901b1586..dcb4484c 100644 --- a/web/src/auth/Face.js +++ b/web/src/auth/Face.js @@ -13,7 +13,7 @@ // limitations under the License. import React from "react"; -import {Alert, Button, Checkbox, Col, Form, Input, Result, Row} from "antd"; +import {Button, Checkbox, Col, Form, Input, Row} from "antd"; import {LockOutlined, UserOutlined} from "@ant-design/icons"; import * as AuthBackend from "./AuthBackend"; import * as Provider from "./Provider"; @@ -102,54 +102,9 @@ class Face extends React.Component { }); }; - renderMessage() { - if (this.state.msg !== null) { - return ( -
- - Detail - - } - /> -
- ) - } else { - return null; - } - } - - renderMessageLarge() { - if (this.state.msg !== null) { - return ( -
- - Details - , - , - ]} - > - -
- ) - } else { - return null; - } - } - renderForm(application) { if (this.state.msg !== null) { - return this.renderMessage() + return Util.renderMessage(this.state.msg) } return ( @@ -250,7 +205,7 @@ class Face extends React.Component { render() { const application = this.getApplicationObj(); if (application === null) { - return this.renderMessageLarge(); + return Util.renderMessageLarge(this.state.msg); } return ( diff --git a/web/src/auth/Util.js b/web/src/auth/Util.js index c8c87ad1..b5f24a99 100644 --- a/web/src/auth/Util.js +++ b/web/src/auth/Util.js @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {message} from "antd"; +import React from "react"; +import {Alert, Button, message, Result} from "antd"; export function goToLink(link) { window.location.href = link; @@ -26,6 +27,51 @@ export function showMessage(type, text) { } } +export function renderMessage(msg) { + if (msg !== null) { + return ( +
+ + Detail + + } + /> +
+ ) + } else { + return null; + } +} + +export function renderMessageLarge(msg) { + if (msg !== null) { + return ( +
+ + Details + , + , + ]} + > + +
+ ) + } else { + return null; + } +} + export function trim(str, ch) { if (str === undefined) { return undefined;