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 {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({

View File

@ -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 = '/';

View File

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

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