// Copyright 2021 The Casdoor 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"; import {Button, Form, Input, Radio, Result, Row, Select, message} from "antd"; import * as Setting from "../Setting"; import * as AuthBackend from "./AuthBackend"; import * as ProviderButton from "./ProviderButton"; import i18next from "i18next"; import * as Util from "./Util"; import {authConfig} from "./Auth"; import * as ApplicationBackend from "../backend/ApplicationBackend"; import * as AgreementModal from "../common/modal/AgreementModal"; import {SendCodeInput} from "../common/SendCodeInput"; import RegionSelect from "../common/select/RegionSelect"; import CustomGithubCorner from "../common/CustomGithubCorner"; import LanguageSelect from "../common/select/LanguageSelect"; import {withRouter} from "react-router-dom"; import {CountryCodeSelect} from "../common/select/CountryCodeSelect"; import * as PasswordChecker from "../common/PasswordChecker"; import * as InvitationBackend from "../backend/InvitationBackend"; const formItemLayout = { labelCol: { xs: { span: 24, }, sm: { span: 8, }, }, wrapperCol: { xs: { span: 24, }, sm: { span: 16, }, }, }; const renderFormItem = (signupItem) => { const commonProps = { name: signupItem.name.toLowerCase(), label: signupItem.label || signupItem.name, rules: [ { required: signupItem.required, message: i18next.t(`signup:Please input your ${signupItem.label || signupItem.name}!`), }, ], }; if (!signupItem.type || signupItem.type === "Input") { return ( ); } else if (signupItem.type === "Single Choice" || signupItem.type === "Multiple Choices") { return ( ); } else if (signupItem.name === "Display name") { if (signupItem.rule === "First, last" && Setting.getLanguage() !== "zh") { return ( ); } return ( ); } else if (signupItem.name === "Affiliation") { return ( ); } else if (signupItem.name === "ID card") { return ( ); } else if (signupItem.name === "Country/Region") { return ( { this.setState({region: value}); }} /> ); } else if (signupItem.name === "Email" || signupItem.name === "Phone" || signupItem.name === "Email or Phone" || signupItem.name === "Phone or Email") { const renderEmailItem = () => { return ( { if (this.state.email !== "" && !Setting.isValidEmail(this.state.email)) { this.setState({validEmail: false}); return Promise.reject(i18next.t("signup:The input is not valid Email!")); } if (signupItem.regex) { const reg = new RegExp(signupItem.regex); if (!reg.test(this.state.email)) { this.setState({validEmail: false}); return Promise.reject(i18next.t("signup:The input Email doesn't match the signup item regex!")); } } this.setState({validEmail: true}); return Promise.resolve(); }, }, ]} > this.setState({email: e.target.value})} /> { signupItem.rule !== "No verification" && } ); }; const renderPhoneItem = () => { return ( ({ validator: (_, value) => { if (!required && !value) { return Promise.resolve(); } if (value && !Setting.isValidPhone(value, getFieldValue("countryCode"))) { this.setState({validPhone: false}); return Promise.reject(i18next.t("signup:The input is not valid Phone!")); } this.setState({validPhone: true}); return Promise.resolve(); }, }), ]} > this.setState({phone: e.target.value})} /> { signupItem.rule !== "No verification" && } ); }; if (signupItem.name === "Email") { return renderEmailItem(); } else if (signupItem.name === "Phone") { return renderPhoneItem(); } else if (signupItem.name === "Email or Phone" || signupItem.name === "Phone or Email") { let emailOrPhoneMode = this.state.emailOrPhoneMode; if (emailOrPhoneMode === "") { emailOrPhoneMode = signupItem.name === "Email or Phone" ? "Email" : "Phone"; } return ( { this.setState({ emailOrPhoneMode: e.target.value, }); }} value={emailOrPhoneMode}> { signupItem.name === "Email or Phone" ? ( {i18next.t("general:Email")} {i18next.t("general:Phone")} ) : ( {i18next.t("general:Phone")} {i18next.t("general:Email")} ) } { emailOrPhoneMode === "Email" ? renderEmailItem() : renderPhoneItem() } ); } else { return null; } } else if (signupItem.name === "Password") { return ( { const errorMsg = PasswordChecker.checkPasswordComplexity(value, application.organizationObj.passwordOptions); if (errorMsg === "") { return Promise.resolve(); } else { return Promise.reject(errorMsg); } }, }, ]} hasFeedback > ); } else if (signupItem.name === "Confirm password") { return ( ({ validator(rule, value) { if (!value || getFieldValue("password") === value) { return Promise.resolve(); } return Promise.reject(i18next.t("signup:Your confirmed password is inconsistent with the password!")); }, }), ]} > ); } else if (signupItem.name === "Invitation code") { return ( ); } else if (signupItem.name === "Agreement") { return AgreementModal.renderAgreementFormItem(application, required, tailFormItemLayout, this); } else if (signupItem.name.startsWith("Text ")) { return (
); } else if (signupItem.name === "Signup button") { return (   {i18next.t("signup:Have account?")}  { const linkInStorage = sessionStorage.getItem("signinUrl"); if (linkInStorage !== null && linkInStorage !== "") { Setting.goToLinkSoft(this, linkInStorage); } else { Setting.redirectToLoginPage(application, this.props.history); } }}> {i18next.t("signup:sign in now")} ); } else if (signupItem.name === "Providers") { const showForm = Setting.isPasswordEnabled(application) || Setting.isCodeSigninEnabled(application) || Setting.isWebAuthnEnabled(application) || Setting.isLdapEnabled(application); if (signupItem.rule === "None" || signupItem.rule === "") { signupItem.rule = showForm ? "small" : "big"; } return ( application.providers.filter(providerItem => this.isProviderVisible(providerItem)).map((providerItem, id) => { return ( { const agreementChecked = this.form.current.getFieldValue("agreement"); if (agreementChecked !== undefined && typeof agreementChecked === "boolean" && !agreementChecked) { e.preventDefault(); message.error(i18next.t("signup:Please accept the agreement!")); } }}> { ProviderButton.renderProviderLogo(providerItem.provider, application, null, null, signupItem.rule, this.props.location) } ); }) ); } else if (validItems.includes(signupItem.name)) { return renderFormItem(signupItem); } } renderForm(application) { if (!application.enableSignUp) { return ( Setting.redirectToLoginPage(application, this.props.history)}> { i18next.t("login:Sign In") } , ]} > ); } if (this.state.invitation !== undefined) { if (this.state.invitation.username !== "") { this.form.current?.setFieldValue("username", this.state.invitation.username); } if (this.state.invitation.email !== "") { this.form.current?.setFieldValue("email", this.state.invitation.email); } if (this.state.invitation.phone !== "") { this.form.current?.setFieldValue("phone", this.state.invitation.phone); } if (this.state.invitationCode !== "") { this.form.current?.setFieldValue("invitationCode", this.state.invitationCode); } } return (
this.onFinish(values)} onFinishFailed={(errorInfo) => this.onFinishFailed(errorInfo.values, errorInfo.errorFields, errorInfo.outOfDate)} initialValues={{ application: application.name, organization: application.organization, countryCode: application.organizationObj.countryCodes?.[0], }} size="large" layout={Setting.isMobile() ? "vertical" : "horizontal"} style={{width: Setting.isMobile() ? "300px" : "400px"}} > { application.signupItems?.map((signupItem, idx) => { return (
" + signupItem.customCss + "")}} /> {this.renderFormItem(application, signupItem)}
); }) } ); } render() { const application = this.getApplicationObj(); if (application === undefined || application === null) { return null; } let existSignupButton = false; application.signupItems?.map(item => { item.name === "Signup button" ? existSignupButton = true : null; }); if (!existSignupButton) { application.signupItems?.push({ customCss: "", label: "", name: "Signup button", placeholder: "", visible: true, }); } if (application.signupHtml !== "") { return (
); } return (
{Setting.inIframe() || Setting.isMobile() ? null :
} {Setting.inIframe() || !Setting.isMobile() ? null :
}
{ Setting.renderHelmet(application) } { Setting.renderLogo(application) } { this.renderForm(application) }
); } } export default withRouter(SignupPage);