casdoor/web/src/auth/LoginPage.js

302 lines
9.5 KiB
JavaScript
Raw Normal View History

2021-02-13 13:30:51 +08:00
// 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.
2021-02-13 12:15:19 +08:00
import React from "react";
2021-04-18 23:14:46 +08:00
import {Button, Checkbox, Col, Form, Input, Row} from "antd";
2021-02-13 12:15:19 +08:00
import {LockOutlined, UserOutlined} from "@ant-design/icons";
2021-02-14 15:40:57 +08:00
import * as AuthBackend from "./AuthBackend";
2021-04-19 01:14:41 +08:00
import * as ApplicationBackend from "../backend/ApplicationBackend";
2021-02-14 16:59:08 +08:00
import * as Provider from "./Provider";
2021-02-14 15:40:57 +08:00
import * as Util from "./Util";
2021-03-26 21:58:19 +08:00
import * as Setting from "../Setting";
2021-03-29 22:11:28 +08:00
import {GithubLoginButton, GoogleLoginButton} from "react-social-login-buttons";
import QqLoginButton from "./QqLoginButton";
2021-04-27 22:03:22 +08:00
import i18next from "i18next";
2021-02-13 12:15:19 +08:00
2021-03-26 21:56:51 +08:00
class LoginPage extends React.Component {
2021-02-13 12:15:19 +08:00
constructor(props) {
super(props);
this.state = {
classes: props,
2021-03-20 11:34:04 +08:00
type: props.type,
2021-02-13 12:15:19 +08:00
applicationName: props.applicationName !== undefined ? props.applicationName : (props.match === undefined ? null : props.match.params.applicationName),
application: null,
2021-03-20 11:34:04 +08:00
msg: null,
2021-02-13 12:15:19 +08:00
};
}
2021-03-27 11:38:15 +08:00
UNSAFE_componentWillMount() {
2021-03-20 11:34:04 +08:00
if (this.state.type === "login") {
this.getApplication();
} else if (this.state.type === "code") {
this.getApplicationLogin();
} else {
Util.showMessage("error", `Unknown authentication type: ${this.state.type}`);
}
}
getApplicationLogin() {
2021-03-20 16:51:10 +08:00
const oAuthParams = Util.getOAuthGetParameters();
AuthBackend.getApplicationLogin(oAuthParams)
2021-03-20 11:34:04 +08:00
.then((res) => {
if (res.status === "ok") {
this.setState({
application: res.data,
});
} else {
// Util.showMessage("error", res.msg);
this.setState({
application: res.data,
msg: res.msg,
});
}
});
2021-02-13 12:15:19 +08:00
}
getApplication() {
2021-02-13 23:00:43 +08:00
if (this.state.applicationName === null) {
return;
}
2021-04-19 01:14:41 +08:00
ApplicationBackend.getApplication("admin", this.state.applicationName)
2021-02-13 12:15:19 +08:00
.then((application) => {
this.setState({
application: application,
});
});
}
getApplicationObj() {
if (this.props.application !== undefined) {
return this.props.application;
} else {
return this.state.application;
}
}
onFinish(values) {
2021-03-20 16:51:10 +08:00
values["type"] = this.state.type;
const oAuthParams = Util.getOAuthGetParameters();
AuthBackend.login(values, oAuthParams)
2021-02-13 12:15:19 +08:00
.then((res) => {
if (res.status === 'ok') {
2021-03-21 00:38:00 +08:00
const responseType = this.state.type;
if (responseType === "login") {
2021-03-20 13:05:34 +08:00
Util.showMessage("success", `Logged in successfully`);
2021-03-26 21:58:19 +08:00
Setting.goToLink("/");
2021-03-21 00:38:00 +08:00
} else if (responseType === "code") {
2021-03-20 22:34:22 +08:00
const code = res.data;
2021-03-26 21:58:19 +08:00
Setting.goToLink(`${oAuthParams.redirectUri}?code=${code}&state=${oAuthParams.state}`);
2021-03-20 22:34:22 +08:00
// Util.showMessage("success", `Authorization code: ${res.data}`);
2021-03-20 13:05:34 +08:00
}
2021-02-13 12:15:19 +08:00
} else {
2021-03-26 21:57:41 +08:00
Util.showMessage("error", `Failed to log in: ${res.msg}`);
2021-02-13 12:15:19 +08:00
}
});
};
2021-06-11 20:33:22 +08:00
getSigninButton(type) {
const text = i18next.t("login:Sign in with {type}").replace("{type}", type);
2021-03-29 22:11:28 +08:00
if (type === "GitHub") {
2021-06-11 20:33:22 +08:00
return <GithubLoginButton text={text} align={"center"} />
2021-03-29 22:11:28 +08:00
} else if (type === "Google") {
2021-06-11 20:33:22 +08:00
return <GoogleLoginButton text={text} align={"center"} />
2021-03-29 22:11:28 +08:00
} else if (type === "QQ") {
2021-06-11 20:33:22 +08:00
return <QqLoginButton text={text} align={"center"} />
2021-03-29 22:11:28 +08:00
} else {
2021-06-11 20:33:22 +08:00
return text;
2021-03-29 22:11:28 +08:00
}
}
2021-03-28 20:20:40 +08:00
renderProviderLogo(provider, application, width, margin, size) {
if (size === "small") {
return (
<a key={provider.displayName} href={Provider.getAuthUrl(application, provider, "signup")}>
<img width={width} height={width} src={Provider.getAuthLogo(provider)} alt={provider.displayName} style={{margin: margin}} />
</a>
)
} else {
return (
2021-03-29 22:11:28 +08:00
<div key={provider.displayName} style={{marginBottom: "10px"}}>
<a href={Provider.getAuthUrl(application, provider, "signup")}>
{
2021-06-11 20:33:22 +08:00
this.getSigninButton(provider.type)
2021-03-29 22:11:28 +08:00
}
</a>
</div>
2021-03-28 20:20:40 +08:00
)
}
}
2021-03-20 12:29:34 +08:00
renderForm(application) {
if (this.state.msg !== null) {
2021-03-25 23:22:34 +08:00
return Util.renderMessage(this.state.msg)
2021-03-20 11:34:04 +08:00
}
2021-03-26 21:58:30 +08:00
if (application.enablePassword) {
return (
<Form
name="normal_login"
initialValues={{
organization: application.organization,
remember: true
}}
onFinish={this.onFinish.bind(this)}
style={{width: "250px"}}
size="large"
2021-02-13 12:15:19 +08:00
>
2021-04-28 21:25:58 +08:00
<Form.Item
style={{height: 0, visibility: "hidden"}}
name="organization"
rules={[
{
required: true,
message: 'Please input your organization!',
},
]}
2021-03-26 21:58:30 +08:00
>
2021-02-13 12:15:19 +08:00
</Form.Item>
2021-03-26 21:58:30 +08:00
<Form.Item
name="username"
2021-05-23 23:40:44 +08:00
rules={[{ required: true, message: i18next.t("login:Please input your username, Email or phone!") }]}
2021-03-26 21:58:30 +08:00
>
<Input
prefix={<UserOutlined className="site-form-item-icon" />}
2021-05-23 23:40:44 +08:00
placeholder={i18next.t("login:username, Email or phone")}
2021-03-26 21:58:30 +08:00
disabled={!application.enablePassword}
/>
</Form.Item>
<Form.Item
name="password"
2021-04-28 00:47:12 +08:00
rules={[{ required: true, message: i18next.t("login:Please input your password!") }]}
2021-02-13 12:15:19 +08:00
>
2021-03-26 21:58:30 +08:00
<Input
prefix={<LockOutlined className="site-form-item-icon" />}
type="password"
2021-05-14 23:45:20 +08:00
placeholder={i18next.t("login:Password")}
2021-03-26 21:58:30 +08:00
disabled={!application.enablePassword}
/>
</Form.Item>
<Form.Item>
<Form.Item name="remember" valuePropName="checked" noStyle>
<Checkbox style={{float: "left"}} disabled={!application.enablePassword}>
2021-04-27 22:03:22 +08:00
{i18next.t("login:Auto login")}
2021-03-26 21:58:30 +08:00
</Checkbox>
</Form.Item>
<a style={{float: "right"}} onClick={() => {
Setting.goToForget(this, application);
}}>
{i18next.t("login:Forgot password?")}
</a>
2021-03-26 21:58:30 +08:00
</Form.Item>
<Form.Item>
<Button
type="primary"
htmlType="submit"
style={{width: "100%"}}
disabled={!application.enablePassword}
>
2021-04-27 22:03:22 +08:00
{i18next.t("login:Sign In")}
2021-03-26 21:58:30 +08:00
</Button>
{
!application.enableSignUp ? null : (
<div style={{float: "right"}}>
2021-04-27 22:03:22 +08:00
{i18next.t("login:No account yet?")}&nbsp;
<a onClick={() => {
Setting.goToSignup(this, application);
}}>
2021-04-27 22:03:22 +08:00
{i18next.t("login:sign up now")}
</a>
2021-03-26 21:58:30 +08:00
</div>
)
}
</Form.Item>
<Form.Item>
{
2021-06-14 21:35:19 +08:00
application.providers.filter(providerItem => Setting.isProviderVisibleForSignIn(providerItem)).map(providerItem => {
return this.renderProviderLogo(providerItem.provider, application, 30, 5, "small");
2021-03-26 21:58:30 +08:00
})
}
</Form.Item>
</Form>
);
} else {
return (
<div style={{marginTop: "20px"}}>
2021-03-28 19:15:10 +08:00
<div style={{fontSize: 16, textAlign: "left"}}>
2021-06-11 20:33:22 +08:00
{i18next.t("login:To access")}&nbsp;
2021-03-27 11:38:15 +08:00
<a target="_blank" rel="noreferrer" href={application.homepageUrl}>
2021-03-29 22:11:28 +08:00
<span style={{fontWeight: "bold"}}>
{application.displayName}
</span>
2021-03-26 21:58:30 +08:00
</a>
2021-06-11 20:33:22 +08:00
:
2021-02-13 12:15:19 +08:00
</div>
2021-03-26 21:58:30 +08:00
<br/>
2021-02-13 23:00:43 +08:00
{
2021-06-14 21:35:19 +08:00
application.providers.filter(providerItem => Setting.isProviderVisibleForSignIn(providerItem)).map(providerItem => {
return this.renderProviderLogo(providerItem.provider, application, 40, 10, "big");
2021-02-13 23:00:43 +08:00
})
}
2021-03-28 19:15:10 +08:00
{
!application.enableSignUp ? null : (
<div>
<br/>
<div style={{float: "right"}}>
{i18next.t("login:No account yet?")}&nbsp;
<a onClick={() => {
Setting.goToSignup(this, application);
}}>
{i18next.t("login:sign up now")}
</a>
2021-03-28 19:15:10 +08:00
</div>
</div>
)
}
2021-03-26 21:58:30 +08:00
</div>
)
}
2021-02-13 12:15:19 +08:00
}
render() {
const application = this.getApplicationObj();
if (application === null) {
2021-03-26 21:58:19 +08:00
return Util.renderMessageLarge(this, this.state.msg);
2021-02-13 12:15:19 +08:00
}
return (
<Row>
2021-03-20 11:34:04 +08:00
<Col span={24} style={{display: "flex", justifyContent: "center"}}>
2021-02-13 12:15:19 +08:00
<div style={{marginTop: "80px", textAlign: "center"}}>
2021-04-29 21:28:24 +08:00
{
Setting.renderHelmet(application)
}
{
2021-04-28 15:54:50 +08:00
Setting.renderLogo(application)
}
2021-03-15 00:28:34 +08:00
{/*{*/}
{/* this.state.clientId !== null ? "Redirect" : null*/}
{/*}*/}
2021-02-13 12:15:19 +08:00
{
this.renderForm(application)
}
</div>
</Col>
</Row>
)
}
}
2021-03-26 21:56:51 +08:00
export default LoginPage;