From faeb93494c50226077cf6141bfa575e9b0e36704 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sun, 14 Feb 2021 15:40:57 +0800 Subject: [PATCH] Improve auth folder. --- web/src/App.js | 7 ++--- .../AccountBackend.js => auth/AuthBackend.js} | 0 web/src/auth/AuthCallback.js | 4 +-- web/src/auth/Face.js | 12 ++++----- web/src/auth/Util.js | 27 +++++++++++++++++++ 5 files changed, 39 insertions(+), 11 deletions(-) rename web/src/{backend/AccountBackend.js => auth/AuthBackend.js} (100%) create mode 100644 web/src/auth/Util.js diff --git a/web/src/App.js b/web/src/App.js index 5b1d4b56..9c3ebf0b 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -18,7 +18,6 @@ import * as Setting from "./Setting"; import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons'; import {Avatar, BackTop, Dropdown, Layout, Menu} from 'antd'; import {Switch, Route, withRouter, Redirect, Link} from 'react-router-dom' -import * as AccountBackend from "./backend/AccountBackend"; import OrganizationListPage from "./OrganizationListPage"; import OrganizationEditPage from "./OrganizationEditPage"; import UserListPage from "./UserListPage"; @@ -31,7 +30,9 @@ import AccountPage from "./account/AccountPage"; import LoginPage from "./account/LoginPage"; import HomePage from "./basic/HomePage"; import CustomGithubCorner from "./CustomGithubCorner"; + import Face from "./auth/Face"; +import * as AuthBackend from "./auth/AuthBackend"; import AuthCallback from "./auth/AuthCallback"; const { Header, Footer } = Layout; @@ -82,7 +83,7 @@ class App extends Component { } getAccount() { - AccountBackend.getAccount() + AuthBackend.getAccount() .then((res) => { const account = Setting.parseJson(res.data); this.setState({ @@ -97,7 +98,7 @@ class App extends Component { submitted: false, }); - AccountBackend.logout() + AuthBackend.logout() .then((res) => { if (res.status === 'ok') { this.setState({ diff --git a/web/src/backend/AccountBackend.js b/web/src/auth/AuthBackend.js similarity index 100% rename from web/src/backend/AccountBackend.js rename to web/src/auth/AuthBackend.js diff --git a/web/src/auth/AuthCallback.js b/web/src/auth/AuthCallback.js index 5d91a96d..793f85d7 100644 --- a/web/src/auth/AuthCallback.js +++ b/web/src/auth/AuthCallback.js @@ -15,7 +15,7 @@ import React from "react"; import {message, Spin} from "antd"; import {withRouter} from "react-router-dom"; -import * as AccountBackend from "../backend/AccountBackend"; +import * as AuthBackend from "./AuthBackend"; import {getClientUrl} from "./Auth"; class AuthCallback extends React.Component { @@ -50,7 +50,7 @@ class AuthCallback extends React.Component { authLogin() { let redirectUrl; redirectUrl = `${getClientUrl()}/callback/${this.state.providerType}/${this.state.providerName}/${this.state.addition}`; - AccountBackend.authLogin(this.state.providerName, this.state.code, this.state.state, redirectUrl, this.state.addition) + AuthBackend.authLogin(this.state.providerName, this.state.code, this.state.state, redirectUrl, this.state.addition) .then((res) => { if (res.status === "ok") { window.location.href = '/'; diff --git a/web/src/auth/Face.js b/web/src/auth/Face.js index ae0f2d62..20a7ae6d 100644 --- a/web/src/auth/Face.js +++ b/web/src/auth/Face.js @@ -16,9 +16,9 @@ import React from "react"; import {Button, Checkbox, Col, Form, Input, Row} from "antd"; import {LockOutlined, UserOutlined} from "@ant-design/icons"; import * as ApplicationBackend from "../backend/ApplicationBackend"; -import * as AccountBackend from "../backend/AccountBackend"; -import * as Setting from "../Setting"; +import * as AuthBackend from "./AuthBackend"; import * as Auth from "./Auth"; +import * as Util from "./Util"; class Face extends React.Component { constructor(props) { @@ -56,14 +56,14 @@ class Face extends React.Component { } onFinish(values) { - AccountBackend.login(values) + AuthBackend.login(values) .then((res) => { if (res.status === 'ok') { this.props.onLoggedIn(); - Setting.showMessage("success", `Logged in successfully`); - Setting.goToLink("/"); + Util.showMessage("success", `Logged in successfully`); + Util.goToLink("/"); } else { - Setting.showMessage("error", `Log in failed:${res.msg}`); + Util.showMessage("error", `Log in failed:${res.msg}`); } }); }; diff --git a/web/src/auth/Util.js b/web/src/auth/Util.js new file mode 100644 index 00000000..12639ff8 --- /dev/null +++ b/web/src/auth/Util.js @@ -0,0 +1,27 @@ +// Copyright 2021 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import {message} from "antd"; + +export function goToLink(link) { + window.location.href = link; +} + +export function showMessage(type, text) { + if (type === "success") { + message.success(text); + } else if (type === "error") { + message.error(text); + } +}