Improve auth folder.

This commit is contained in:
Yang Luo
2021-02-14 15:40:57 +08:00
parent 001496b90f
commit faeb93494c
5 changed files with 39 additions and 11 deletions

View File

@ -18,7 +18,6 @@ import * as Setting from "./Setting";
import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons'; import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons';
import {Avatar, BackTop, Dropdown, Layout, Menu} from 'antd'; import {Avatar, BackTop, Dropdown, Layout, Menu} from 'antd';
import {Switch, Route, withRouter, Redirect, Link} from 'react-router-dom' import {Switch, Route, withRouter, Redirect, Link} from 'react-router-dom'
import * as AccountBackend from "./backend/AccountBackend";
import OrganizationListPage from "./OrganizationListPage"; import OrganizationListPage from "./OrganizationListPage";
import OrganizationEditPage from "./OrganizationEditPage"; import OrganizationEditPage from "./OrganizationEditPage";
import UserListPage from "./UserListPage"; import UserListPage from "./UserListPage";
@ -31,7 +30,9 @@ import AccountPage from "./account/AccountPage";
import LoginPage from "./account/LoginPage"; import LoginPage from "./account/LoginPage";
import HomePage from "./basic/HomePage"; import HomePage from "./basic/HomePage";
import CustomGithubCorner from "./CustomGithubCorner"; import CustomGithubCorner from "./CustomGithubCorner";
import Face from "./auth/Face"; import Face from "./auth/Face";
import * as AuthBackend from "./auth/AuthBackend";
import AuthCallback from "./auth/AuthCallback"; import AuthCallback from "./auth/AuthCallback";
const { Header, Footer } = Layout; const { Header, Footer } = Layout;
@ -82,7 +83,7 @@ class App extends Component {
} }
getAccount() { getAccount() {
AccountBackend.getAccount() AuthBackend.getAccount()
.then((res) => { .then((res) => {
const account = Setting.parseJson(res.data); const account = Setting.parseJson(res.data);
this.setState({ this.setState({
@ -97,7 +98,7 @@ class App extends Component {
submitted: false, submitted: false,
}); });
AccountBackend.logout() AuthBackend.logout()
.then((res) => { .then((res) => {
if (res.status === 'ok') { if (res.status === 'ok') {
this.setState({ this.setState({

View File

@ -15,7 +15,7 @@
import React from "react"; import React from "react";
import {message, Spin} from "antd"; import {message, Spin} from "antd";
import {withRouter} from "react-router-dom"; import {withRouter} from "react-router-dom";
import * as AccountBackend from "../backend/AccountBackend"; import * as AuthBackend from "./AuthBackend";
import {getClientUrl} from "./Auth"; import {getClientUrl} from "./Auth";
class AuthCallback extends React.Component { class AuthCallback extends React.Component {
@ -50,7 +50,7 @@ class AuthCallback extends React.Component {
authLogin() { authLogin() {
let redirectUrl; let redirectUrl;
redirectUrl = `${getClientUrl()}/callback/${this.state.providerType}/${this.state.providerName}/${this.state.addition}`; 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) => { .then((res) => {
if (res.status === "ok") { if (res.status === "ok") {
window.location.href = '/'; window.location.href = '/';

View File

@ -16,9 +16,9 @@ import React from "react";
import {Button, Checkbox, Col, Form, Input, Row} from "antd"; import {Button, Checkbox, Col, Form, Input, Row} from "antd";
import {LockOutlined, UserOutlined} from "@ant-design/icons"; import {LockOutlined, UserOutlined} from "@ant-design/icons";
import * as ApplicationBackend from "../backend/ApplicationBackend"; import * as ApplicationBackend from "../backend/ApplicationBackend";
import * as AccountBackend from "../backend/AccountBackend"; import * as AuthBackend from "./AuthBackend";
import * as Setting from "../Setting";
import * as Auth from "./Auth"; import * as Auth from "./Auth";
import * as Util from "./Util";
class Face extends React.Component { class Face extends React.Component {
constructor(props) { constructor(props) {
@ -56,14 +56,14 @@ class Face extends React.Component {
} }
onFinish(values) { onFinish(values) {
AccountBackend.login(values) AuthBackend.login(values)
.then((res) => { .then((res) => {
if (res.status === 'ok') { if (res.status === 'ok') {
this.props.onLoggedIn(); this.props.onLoggedIn();
Setting.showMessage("success", `Logged in successfully`); Util.showMessage("success", `Logged in successfully`);
Setting.goToLink("/"); Util.goToLink("/");
} else { } else {
Setting.showMessage("error", `Log in failed${res.msg}`); Util.showMessage("error", `Log in failed${res.msg}`);
} }
}); });
}; };

27
web/src/auth/Util.js Normal file
View File

@ -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);
}
}