casdoor/web/src/auth/SignupPage.js

452 lines
12 KiB
JavaScript
Raw Normal View History

2021-03-26 21:57:41 +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.
import React from 'react';
2021-03-26 21:58:10 +08:00
import {Link} from "react-router-dom";
2021-05-13 22:23:04 +08:00
import {Form, Input, Checkbox, Button, Row, Col, Result} from 'antd';
2021-03-26 21:57:41 +08:00
import * as Setting from "../Setting";
import * as AuthBackend from "./AuthBackend";
2021-04-28 00:47:12 +08:00
import i18next from "i18next";
2021-04-28 15:54:50 +08:00
import * as Util from "./Util";
import {authConfig} from "./Auth";
import * as ApplicationBackend from "../backend/ApplicationBackend";
import * as UserBackend from "../backend/UserBackend";
import {CountDownInput} from "../component/CountDownInput";
2021-03-26 21:57:41 +08:00
const formItemLayout = {
labelCol: {
xs: {
span: 24,
},
sm: {
2021-04-28 15:54:50 +08:00
span: 6,
2021-03-26 21:57:41 +08:00
},
},
wrapperCol: {
xs: {
span: 24,
},
sm: {
2021-04-28 15:54:50 +08:00
span: 18,
2021-03-26 21:57:41 +08:00
},
},
};
const tailFormItemLayout = {
wrapperCol: {
xs: {
span: 24,
offset: 0,
},
sm: {
span: 16,
offset: 8,
},
},
};
2021-04-27 22:47:44 +08:00
class SignupPage extends React.Component {
2021-03-26 21:57:41 +08:00
constructor(props) {
super(props);
this.state = {
classes: props,
2021-06-14 13:13:39 +08:00
applicationName: props.match?.params.applicationName !== undefined ? props.match.params.applicationName : authConfig.appName,
2021-04-28 15:54:50 +08:00
application: null,
email: "",
phone: "",
emailCode: "",
phoneCode: ""
2021-03-26 21:57:41 +08:00
};
this.form = React.createRef();
}
2021-04-28 15:54:50 +08:00
UNSAFE_componentWillMount() {
if (this.state.applicationName !== undefined) {
this.getApplication();
} else {
Util.showMessage("error", `Unknown application name: ${this.state.applicationName}`);
}
}
getApplication() {
if (this.state.applicationName === undefined) {
return;
}
ApplicationBackend.getApplication("admin", this.state.applicationName)
.then((application) => {
this.setState({
application: application,
});
});
}
2021-04-28 22:40:21 +08:00
getResultPath(application) {
if (authConfig.appName === application.name) {
return "/result";
} else {
2021-06-20 09:46:06 +08:00
if (Setting.hasPromptPage(application)) {
return `/prompt/${application.name}`;
} else {
return `/result/${application.name}`;
}
2021-04-28 22:40:21 +08:00
}
}
2021-06-14 13:13:39 +08:00
getApplicationObj() {
if (this.props.application !== undefined) {
return this.props.application;
} else {
return this.state.application;
}
}
onUpdateAccount(account) {
this.props.onUpdateAccount(account);
}
2021-03-26 21:57:41 +08:00
onFinish(values) {
2021-06-14 13:13:39 +08:00
const application = this.getApplicationObj();
values.phonePrefix = application.organizationObj.phonePrefix;
2021-04-27 22:47:44 +08:00
AuthBackend.signup(values)
2021-03-26 21:57:41 +08:00
.then((res) => {
if (res.status === 'ok') {
AuthBackend.getAccount("")
.then((res) => {
let account = null;
if (res.status === "ok") {
account = res.data;
account.organization = res.data2;
this.onUpdateAccount(account);
Setting.goToLinkSoft(this, this.getResultPath(application));
} else {
if (res.msg !== "Please sign in first") {
Setting.showMessage("error", `Failed to sign in: ${res.msg}`);
}
}
});
2021-03-26 21:57:41 +08:00
} else {
Setting.showMessage("error", i18next.t(`signup:${res.msg}`));
2021-03-26 21:57:41 +08:00
}
});
}
onFinishFailed(values, errorFields, outOfDate) {
this.form.current.scrollToField(errorFields[0].name);
}
2021-06-16 14:06:41 +08:00
renderFormItem(application, signupItem) {
if (!signupItem.visible) {
return null;
}
2021-06-16 15:25:54 +08:00
const required = signupItem.required;
2021-06-16 14:06:41 +08:00
if (signupItem.name === "Username") {
return (
2021-03-26 21:57:41 +08:00
<Form.Item
2021-04-28 00:47:12 +08:00
name="username"
label={i18next.t("signup:Username")}
2021-03-26 21:57:41 +08:00
rules={[
{
2021-06-16 15:25:54 +08:00
required: required,
message: i18next.t("forget:Please input your username!"),
2021-03-26 21:57:41 +08:00
whitespace: true,
},
]}
>
<Input />
</Form.Item>
2021-06-16 14:06:41 +08:00
)
} else if (signupItem.name === "Display name") {
return (
2021-03-26 21:57:41 +08:00
<Form.Item
2021-04-28 21:25:58 +08:00
name="name"
2021-06-16 15:25:54 +08:00
label={signupItem.rule === "Personal" ? i18next.t("general:Personal name") : i18next.t("general:Display name")}
2021-03-26 21:57:41 +08:00
rules={[
{
2021-06-16 15:25:54 +08:00
required: required,
message: signupItem.rule === "Personal" ? i18next.t("signup:Please input your personal name!") : i18next.t("signup:Please input your display name!"),
2021-03-26 21:57:41 +08:00
whitespace: true,
},
]}
>
<Input />
</Form.Item>
2021-06-16 14:06:41 +08:00
)
} else if (signupItem.name === "Affiliation") {
return (
2021-03-26 21:57:41 +08:00
<Form.Item
2021-04-27 22:35:14 +08:00
name="affiliation"
2021-04-28 00:47:12 +08:00
label={i18next.t("user:Affiliation")}
2021-03-26 21:57:41 +08:00
rules={[
{
2021-06-16 15:25:54 +08:00
required: required,
2021-04-28 00:47:12 +08:00
message: i18next.t("signup:Please input your affiliation!"),
2021-03-26 21:57:41 +08:00
whitespace: true,
},
]}
>
<Input />
</Form.Item>
2021-06-16 14:06:41 +08:00
)
} else if (signupItem.name === "Email") {
return (
<React.Fragment>
<Form.Item
name="email"
label={i18next.t("general:Email")}
rules={[
{
type: 'email',
message: i18next.t("signup:The input is not valid Email!"),
},
{
2021-06-16 15:25:54 +08:00
required: required,
2021-06-16 14:06:41 +08:00
message: i18next.t("signup:Please input your Email!"),
},
]}
>
<Input onChange={e => this.setState({email: e.target.value})} />
</Form.Item>
<Form.Item
name="emailCode"
label={i18next.t("code:Email code")}
2021-06-16 14:06:41 +08:00
rules={[{
2021-06-16 15:25:54 +08:00
required: required,
message: i18next.t("code:Please input your verification code!"),
2021-06-16 14:06:41 +08:00
}]}
>
<CountDownInput
defaultButtonText={i18next.t("code:Send code")}
2021-06-16 14:06:41 +08:00
onButtonClick={UserBackend.sendCode}
onButtonClickArgs={[this.state.email, "email", application?.organizationObj.owner + "/" + application?.organizationObj.name]}
coolDownTime={60}
/>
</Form.Item>
</React.Fragment>
)
} else if (signupItem.name === "Password") {
return (
2021-06-16 15:25:54 +08:00
<Form.Item
name="password"
label={i18next.t("general:Password")}
rules={[
{
required: required,
message: i18next.t("login:Please input your password!"),
},
]}
hasFeedback
>
<Input.Password />
</Form.Item>
)
} else if (signupItem.name === "Confirm password") {
return (
<Form.Item
name="confirm"
label={i18next.t("signup:Confirm")}
dependencies={['password']}
hasFeedback
rules={[
{
required: required,
message: i18next.t("signup:Please confirm your password!"),
},
({ getFieldValue }) => ({
validator(rule, value) {
if (!value || getFieldValue('password') === value) {
return Promise.resolve();
}
2021-06-16 14:06:41 +08:00
2021-06-16 15:25:54 +08:00
return Promise.reject(i18next.t("signup:Your confirmed password is inconsistent with the password!"));
},
}),
]}
>
<Input.Password />
</Form.Item>
2021-06-16 14:06:41 +08:00
)
} else if (signupItem.name === "Phone") {
return (
<React.Fragment>
<Form.Item
name="phone"
label={i18next.t("general:Phone")}
rules={[
{
2021-06-16 15:25:54 +08:00
required: required,
2021-06-16 14:06:41 +08:00
message: i18next.t("signup:Please input your phone number!"),
},
]}
>
<Input
style={{
width: '100%',
}}
addonBefore={`+${this.state.application?.organizationObj.phonePrefix}`}
onChange={e => this.setState({phone: e.target.value})}
/>
</Form.Item>
<Form.Item
name="phoneCode"
label={i18next.t("code:Phone code")}
2021-06-16 14:06:41 +08:00
rules={[
{
2021-06-16 15:25:54 +08:00
required: required,
message: i18next.t("code:Please input your phone verification code!"),
2021-06-16 14:06:41 +08:00
},
]}
>
<CountDownInput
defaultButtonText={i18next.t("code:Send code")}
2021-06-16 14:06:41 +08:00
onButtonClick={UserBackend.sendCode}
onButtonClickArgs={[this.state.phone, "phone", application.organizationObj.owner + "/" + application.organizationObj.name]}
coolDownTime={60}
/>
</Form.Item>
</React.Fragment>
)
} else if (signupItem.name === "Agreement") {
return (
2021-03-26 21:57:41 +08:00
<Form.Item
2021-06-16 14:06:41 +08:00
name="agreement"
valuePropName="checked"
2021-06-16 15:25:54 +08:00
rules={[
{
required: required,
message: i18next.t("signup:Please accept the agreement!"),
},
]}
2021-06-16 14:06:41 +08:00
{...tailFormItemLayout}
2021-03-26 21:57:41 +08:00
>
2021-06-16 14:06:41 +08:00
<Checkbox>
{i18next.t("signup:Accept")}&nbsp;
<Link to={"/agreement"}>
{i18next.t("signup:Terms of Use")}
</Link>
</Checkbox>
2021-03-26 21:57:41 +08:00
</Form.Item>
2021-06-16 14:06:41 +08:00
)
}
}
2021-03-26 21:57:41 +08:00
2021-06-16 14:06:41 +08:00
renderForm(application) {
if (!application.enableSignUp) {
return (
<Result
status="error"
title="Sign Up Error"
subTitle={"The application does not allow to sign up new account"}
extra={[
<Link onClick={() => {
Setting.goToLogin(this, application);
}}>
<Button type="primary" key="signin">
Sign In
</Button>
</Link>
2021-03-26 21:57:41 +08:00
]}
>
2021-06-16 14:06:41 +08:00
</Result>
)
}
return (
<Form
{...formItemLayout}
ref={this.form}
name="signup"
onFinish={(values) => this.onFinish(values)}
onFinishFailed={(errorInfo) => this.onFinishFailed(errorInfo.values, errorInfo.errorFields, errorInfo.outOfDate)}
initialValues={{
application: application.name,
organization: application.organization,
}}
style={{width: !Setting.isMobile() ? "400px" : "250px"}}
size="large"
>
2021-03-26 21:57:41 +08:00
<Form.Item
2021-06-16 14:06:41 +08:00
style={{height: 0, visibility: "hidden"}}
name="application"
2021-03-26 21:57:41 +08:00
rules={[
{
required: true,
2021-06-16 14:06:41 +08:00
message: 'Please input your application!',
2021-03-26 21:57:41 +08:00
},
]}
>
</Form.Item>
<Form.Item
2021-06-16 14:06:41 +08:00
style={{height: 0, visibility: "hidden"}}
name="organization"
rules={[
{
required: true,
2021-06-16 14:06:41 +08:00
message: 'Please input your organization!',
},
]}
>
2021-03-26 21:57:41 +08:00
</Form.Item>
2021-06-16 14:06:41 +08:00
{
application.signupItems?.map(signupItem => this.renderFormItem(application, signupItem))
2021-06-16 14:06:41 +08:00
}
2021-03-26 21:57:41 +08:00
<Form.Item {...tailFormItemLayout}>
<Button type="primary" htmlType="submit">
2021-04-28 00:47:12 +08:00
{i18next.t("account:Sign Up")}
2021-03-26 21:57:41 +08:00
</Button>
2021-04-28 00:47:12 +08:00
&nbsp;&nbsp;{i18next.t("signup:Have account?")}&nbsp;
2021-04-28 22:40:21 +08:00
<Link onClick={() => {
Setting.goToLogin(this, application);
}}>
2021-04-28 00:47:12 +08:00
{i18next.t("signup:sign in now")}
2021-03-26 21:57:41 +08:00
</Link>
</Form.Item>
</Form>
)
}
render() {
2021-06-14 13:13:39 +08:00
const application = this.getApplicationObj();
2021-04-28 15:54:50 +08:00
if (application === null) {
return null;
}
2021-03-26 21:57:41 +08:00
return (
<div>
&nbsp;
<Row>
<Col span={24} style={{display: "flex", justifyContent: "center"}} >
2021-04-28 15:54:50 +08:00
<div style={{marginTop: "10px", textAlign: "center"}}>
2021-04-29 21:28:24 +08:00
{
Setting.renderHelmet(application)
}
2021-04-28 15:54:50 +08:00
{
Setting.renderLogo(application)
}
{
this.renderForm(application)
}
</div>
2021-03-26 21:57:41 +08:00
</Col>
</Row>
</div>
)
}
}
2021-04-27 22:47:44 +08:00
export default SignupPage;