casdoor/web/src/auth/LoginPage.js

812 lines
28 KiB
JavaScript
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-02-13 13:30:51 +08:00
//
// 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";
import {Button, Checkbox, Col, Form, Input, Result, Row, Spin, Tabs} from "antd";
2021-02-13 12:15:19 +08:00
import {LockOutlined, UserOutlined} from "@ant-design/icons";
import * as UserWebauthnBackend from "../backend/UserWebauthnBackend";
2021-02-14 15:40:57 +08:00
import * as AuthBackend from "./AuthBackend";
import * as OrganizationBackend from "../backend/OrganizationBackend";
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";
import * as ProviderButton from "./ProviderButton";
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-10-09 21:15:57 +08:00
import SelfLoginButton from "./SelfLoginButton";
2021-04-27 22:03:22 +08:00
import i18next from "i18next";
import CustomGithubCorner from "../CustomGithubCorner";
2022-01-07 20:34:27 +08:00
import {CountDownInput} from "../common/CountDownInput";
import SelectLanguageBox from "../SelectLanguageBox";
import {CaptchaModal} from "../common/CaptchaModal";
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),
2022-08-06 23:47:28 +08:00
owner: props.owner !== undefined ? props.owner : (props.match === undefined ? null : props.match.params.owner),
2021-02-13 12:15:19 +08:00
application: null,
2021-06-14 22:42:58 +08:00
mode: props.mode !== undefined ? props.mode : (props.match === undefined ? null : props.match.params.mode), // "signup" or "signin"
2021-03-20 11:34:04 +08:00
msg: null,
2021-11-28 21:10:21 +08:00
username: null,
validEmailOrPhone: false,
validEmail: false,
validPhone: false,
2022-08-06 23:54:56 +08:00
loginMethod: "password",
enableCaptchaModal: false,
openCaptchaModal: false,
verifyCaptcha: undefined,
2021-02-13 12:15:19 +08:00
};
if (this.state.type === "cas" && props.match?.params.casApplicationName !== undefined) {
this.state.owner = props.match?.params.owner;
this.state.applicationName = props.match?.params.casApplicationName;
}
2021-02-13 12:15:19 +08:00
}
2021-03-27 11:38:15 +08:00
UNSAFE_componentWillMount() {
if (this.state.type === "login" || this.state.type === "cas") {
2021-03-20 11:34:04 +08:00
this.getApplication();
} else if (this.state.type === "code") {
this.getApplicationLogin();
} else if (this.state.type === "saml") {
this.getSamlApplication();
2021-03-20 11:34:04 +08:00
} else {
Setting.showMessage("error", `Unknown authentication type: ${this.state.type}`);
2021-03-20 11:34:04 +08:00
}
}
componentDidUpdate(prevProps, prevState, snapshot) {
if (this.state.application && !prevState.application) {
const defaultCaptchaProviderItems = this.getDefaultCaptchaProviderItems(this.state.application);
if (!defaultCaptchaProviderItems) {
return;
}
this.setState({enableCaptchaModal: defaultCaptchaProviderItems.some(providerItem => providerItem.rule === "Always")});
}
}
2021-03-20 11:34:04 +08:00
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 {
// Setting.showMessage("error", res.msg);
2021-03-20 11:34:04 +08:00
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;
}
if (this.state.owner === null || this.state.owner === undefined || this.state.owner === "") {
ApplicationBackend.getApplication("admin", this.state.applicationName)
.then((application) => {
this.setState({
application: application,
});
2021-02-13 12:15:19 +08:00
});
} else {
OrganizationBackend.getDefaultApplication("admin", this.state.owner)
.then((res) => {
if (res.status === "ok") {
this.setState({
application: res.data,
applicationName: res.data.name,
});
} else {
Setting.showMessage("error", res.msg);
}
});
}
2021-02-13 12:15:19 +08:00
}
getSamlApplication() {
if (this.state.applicationName === null) {
return;
}
ApplicationBackend.getApplication(this.state.owner, this.state.applicationName)
.then((application) => {
this.setState({
application: application,
});
2022-08-07 00:17:27 +08:00
}
);
}
2021-02-13 12:15:19 +08:00
getApplicationObj() {
if (this.props.application !== undefined) {
return this.props.application;
} else {
return this.state.application;
}
}
2021-06-20 22:17:03 +08:00
onUpdateAccount(account) {
this.props.onUpdateAccount(account);
}
parseOffset(offset) {
if (offset === 2 || offset === 4 || Setting.inIframe() || Setting.isMobile()) {
return "0 auto";
}
if (offset === 1) {
return "0 10%";
}
if (offset === 3) {
return "0 60%";
}
}
2022-09-25 21:41:52 +08:00
populateOauthValues(values) {
const oAuthParams = Util.getOAuthGetParameters();
if (oAuthParams !== null && oAuthParams.responseType !== null && oAuthParams.responseType !== "") {
values["type"] = oAuthParams.responseType;
} else {
values["type"] = this.state.type;
}
values["phonePrefix"] = this.getApplicationObj()?.organizationObj.phonePrefix;
if (oAuthParams !== null) {
values["samlRequest"] = oAuthParams.samlRequest;
}
if (values["samlRequest"] !== null && values["samlRequest"] !== "" && values["samlRequest"] !== undefined) {
values["type"] = "saml";
}
if (this.state.application.organization !== null && this.state.application.organization !== undefined) {
values["organization"] = this.state.application.organization;
2022-09-25 21:41:52 +08:00
}
}
postCodeLoginAction(res) {
const application = this.getApplicationObj();
const ths = this;
const oAuthParams = Util.getOAuthGetParameters();
const code = res.data;
const concatChar = oAuthParams?.redirectUri?.includes("?") ? "&" : "?";
const noRedirect = oAuthParams.noRedirect;
if (Setting.hasPromptPage(application)) {
AuthBackend.getAccount("")
.then((res) => {
let account = null;
if (res.status === "ok") {
account = res.data;
account.organization = res.data2;
this.onUpdateAccount(account);
if (Setting.isPromptAnswered(account, application)) {
Setting.goToLink(`${oAuthParams.redirectUri}${concatChar}code=${code}&state=${oAuthParams.state}`);
} else {
Setting.goToLinkSoft(ths, `/prompt/${application.name}?redirectUri=${oAuthParams.redirectUri}&code=${code}&state=${oAuthParams.state}`);
}
} else {
Setting.showMessage("error", `Failed to sign in: ${res.msg}`);
}
});
} else {
if (noRedirect === "true") {
window.close();
const newWindow = window.open(`${oAuthParams.redirectUri}${concatChar}code=${code}&state=${oAuthParams.state}`);
if (newWindow) {
setInterval(() => {
if (!newWindow.closed) {
newWindow.close();
}
}, 1000);
}
} else {
Setting.goToLink(`${oAuthParams.redirectUri}${concatChar}code=${code}&state=${oAuthParams.state}`);
}
}
}
2021-02-13 12:15:19 +08:00
onFinish(values) {
if (this.state.loginMethod === "webAuthn") {
let username = this.state.username;
if (username === null || username === "") {
username = values["username"];
}
2022-09-25 21:41:52 +08:00
this.signInWithWebAuthn(username, values);
return;
}
if (this.state.loginMethod === "password" && this.state.enableCaptchaModal) {
this.setState({
openCaptchaModal: true,
verifyCaptcha: (captchaType, captchaToken, secret) => {
values["captchaType"] = captchaType;
values["captchaToken"] = captchaToken;
values["clientSecret"] = secret;
this.login(values);
},
});
} else {
this.login(values);
}
}
login(values) {
// here we are supposed to determine whether Casdoor is working as an OAuth server or CAS server
if (this.state.type === "cas") {
// CAS
const casParams = Util.getCasParameters();
values["type"] = this.state.type;
AuthBackend.loginCas(values, casParams).then((res) => {
if (res.status === "ok") {
let msg = "Logged in successfully. ";
2022-04-25 20:00:57 +08:00
if (casParams.service === "") {
// If service was not specified, Casdoor must display a message notifying the client that it has successfully initiated a single sign-on session.
msg += "Now you can visit apps protected by Casdoor.";
2021-03-20 13:05:34 +08:00
}
Setting.showMessage("success", msg);
this.setState({openCaptchaModal: false});
if (casParams.service !== "") {
const st = res.data;
const newUrl = new URL(casParams.service);
newUrl.searchParams.append("ticket", st);
window.location.href = newUrl.toString();
}
2021-02-13 12:15:19 +08:00
} else {
this.setState({openCaptchaModal: false});
Setting.showMessage("error", `Failed to log in: ${res.msg}`);
2021-02-13 12:15:19 +08:00
}
});
} else {
// OAuth
const oAuthParams = Util.getOAuthGetParameters();
2022-09-25 21:41:52 +08:00
this.populateOauthValues(values);
AuthBackend.login(values, oAuthParams)
.then((res) => {
if (res.status === "ok") {
const responseType = values["type"];
if (responseType === "login") {
Setting.showMessage("success", i18next.t("application:Logged in successfully"));
const link = Setting.getFromLink();
Setting.goToLink(link);
} else if (responseType === "code") {
2022-09-25 21:41:52 +08:00
this.postCodeLoginAction(res);
// Setting.showMessage("success", `Authorization code: ${res.data}`);
} else if (responseType === "token" || responseType === "id_token") {
const accessToken = res.data;
Setting.goToLink(`${oAuthParams.redirectUri}#${responseType}=${accessToken}?state=${oAuthParams.state}&token_type=bearer`);
} else if (responseType === "saml") {
const SAMLResponse = res.data;
const redirectUri = res.data2;
Setting.goToLink(`${redirectUri}?SAMLResponse=${encodeURIComponent(SAMLResponse)}&RelayState=${oAuthParams.relayState}`);
}
} else {
this.setState({openCaptchaModal: false});
Setting.showMessage("error", `Failed to log in: ${res.msg}`);
}
});
}
}
2021-02-13 12:15:19 +08:00
2021-06-14 22:42:58 +08:00
isProviderVisible(providerItem) {
if (this.state.mode === "signup") {
return Setting.isProviderVisibleForSignUp(providerItem);
} else {
return Setting.isProviderVisibleForSignIn(providerItem);
}
}
2021-03-20 12:29:34 +08:00
renderForm(application) {
if (this.state.msg !== null) {
return Util.renderMessage(this.state.msg);
2021-03-20 11:34:04 +08:00
}
2021-06-14 22:42:58 +08:00
if (this.state.mode === "signup" && !application.enableSignUp) {
return (
<Result
status="error"
title={i18next.t("application:Sign Up Error")}
subTitle={i18next.t("application:The application does not allow to sign up new account")}
2021-06-14 22:42:58 +08:00
extra={[
<Button type="primary" key="signin" onClick={() => Setting.redirectToLoginPage(application, this.props.history)}>
{
i18next.t("login:Sign In")
}
</Button>,
2021-06-14 22:42:58 +08:00
]}
>
</Result>
);
2021-06-14 22:42:58 +08:00
}
2021-03-26 21:58:30 +08:00
if (application.enablePassword) {
return (
<Form
name="normal_login"
initialValues={{
organization: application.organization,
2021-06-20 22:17:03 +08:00
application: application.name,
2021-08-06 20:04:08 +08:00
autoSignin: true,
2021-03-26 21:58:30 +08:00
}}
onFinish={(values) => {this.onFinish(values);}}
2021-11-28 21:10:21 +08:00
style={{width: "300px"}}
2021-03-26 21:58:30 +08:00
size="large"
2021-02-13 12:15:19 +08:00
>
2021-06-21 01:01:16 +08:00
<Form.Item
hidden={true}
2021-06-21 01:01:16 +08:00
name="application"
rules={[
{
required: true,
message: i18next.t("application:Please input your application!"),
2021-06-21 01:01:16 +08:00
},
]}
>
</Form.Item>
2021-04-28 21:25:58 +08:00
<Form.Item
hidden={true}
2021-04-28 21:25:58 +08:00
name="organization"
rules={[
{
required: true,
message: i18next.t("application:Please input your organization!"),
2021-04-28 21:25:58 +08:00
},
]}
2021-03-26 21:58:30 +08:00
>
2021-02-13 12:15:19 +08:00
</Form.Item>
{this.renderMethodChoiceBox()}
<Row style={{minHeight: 130, alignItems: "center"}}>
<Col span={24}>
<Form.Item
name="username"
rules={[
{
required: true,
message: i18next.t("login:Please input your username, Email or phone!"),
},
{
validator: (_, value) => {
2022-10-03 15:10:48 +08:00
if (this.state.loginMethod === "verificationCode") {
if (this.state.email !== "" && !Setting.isValidEmail(this.state.username) && !Setting.isValidPhone(this.state.username)) {
this.setState({validEmailOrPhone: false});
return Promise.reject(i18next.t("login:The input is not valid Email or Phone!"));
}
if (Setting.isValidPhone(this.state.username)) {
this.setState({validPhone: true});
}
if (Setting.isValidEmail(this.state.username)) {
this.setState({validEmail: true});
}
}
this.setState({validEmailOrPhone: true});
return Promise.resolve();
},
},
]}
>
<Input
id = "input"
prefix={<UserOutlined className="site-form-item-icon" />}
2022-10-03 15:10:48 +08:00
placeholder={(this.state.loginMethod === "verificationCode") ? i18next.t("login:Email or phone") : i18next.t("login:username, Email or phone")}
disabled={!application.enablePassword}
onChange={e => {
this.setState({
username: e.target.value,
});
}}
/>
</Form.Item>
</Col>
{
this.renderPasswordOrCodeInput()
}
</Row>
2021-03-26 21:58:30 +08:00
<Form.Item>
<Form.Item name="autoSignin" valuePropName="checked" noStyle>
2021-03-26 21:58:30 +08:00
<Checkbox style={{float: "left"}} disabled={!application.enablePassword}>
2021-08-06 20:04:08 +08:00
{i18next.t("login:Auto sign in")}
2021-03-26 21:58:30 +08:00
</Checkbox>
</Form.Item>
{
Setting.renderForgetLink(application, i18next.t("login:Forgot password?"))
}
2021-03-26 21:58:30 +08:00
</Form.Item>
<Form.Item>
2022-10-03 15:10:48 +08:00
<Button
type="primary"
htmlType="submit"
style={{width: "100%", marginBottom: "5px"}}
disabled={!application.enablePassword}
>
{
this.state.loginMethod === "webAuthn" ? i18next.t("login:Sign in with WebAuthn") :
i18next.t("login:Sign In")
}
</Button>
{
this.renderCaptchaModal(application)
}
2021-03-26 21:58:30 +08:00
{
this.renderFooter(application)
2021-03-26 21:58:30 +08:00
}
</Form.Item>
<Form.Item>
{
2021-06-14 22:42:58 +08:00
application.providers.filter(providerItem => this.isProviderVisible(providerItem)).map(providerItem => {
return ProviderButton.renderProviderLogo(providerItem.provider, application, 30, 5, "small", this.props.location);
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-02-13 12:15:19 +08:00
</div>
<br />
2021-02-13 23:00:43 +08:00
{
2021-06-14 22:42:58 +08:00
application.providers.filter(providerItem => this.isProviderVisible(providerItem)).map(providerItem => {
return ProviderButton.renderProviderLogo(providerItem.provider, application, 40, 10, "big", this.props.location);
2021-02-13 23:00:43 +08:00
})
}
<div>
<br />
{
this.renderFooter(application)
}
</div>
2021-03-26 21:58:30 +08:00
</div>
);
2021-03-26 21:58:30 +08:00
}
2021-02-13 12:15:19 +08:00
}
getDefaultCaptchaProviderItems(application) {
const providers = application?.providers;
if (providers === undefined || providers === null) {
return null;
}
return providers.filter(providerItem => {
if (providerItem.provider === undefined || providerItem.provider === null) {
return false;
}
return providerItem.provider.category === "Captcha" && providerItem.provider.type === "Default";
});
}
renderCaptchaModal(application) {
if (!this.state.enableCaptchaModal) {
return null;
}
const provider = this.getDefaultCaptchaProviderItems(application)
.filter(providerItem => providerItem.rule === "Always")
.map(providerItem => providerItem.provider)[0];
return <CaptchaModal
owner={provider.owner}
name={provider.name}
captchaType={provider.type}
subType={provider.subType}
clientId={provider.clientId}
clientId2={provider.clientId2}
clientSecret={provider.clientSecret}
clientSecret2={provider.clientSecret2}
open={this.state.openCaptchaModal}
onOk={(captchaType, captchaToken, secret) => this.state.verifyCaptcha?.(captchaType, captchaToken, secret)}
canCancel={false}
/>;
}
2021-06-14 22:42:58 +08:00
renderFooter(application) {
if (this.state.mode === "signup") {
return (
<div style={{float: "right"}}>
{i18next.t("signup:Have account?")}&nbsp;
{
Setting.renderLoginLink(application, i18next.t("signup:sign in now"))
}
2021-06-14 22:42:58 +08:00
</div>
);
2021-06-14 22:42:58 +08:00
} else {
return (
2021-11-28 21:10:21 +08:00
<React.Fragment>
<span style={{float: "right"}}>
{
!application.enableSignUp ? null : (
<>
{i18next.t("login:No account?")}&nbsp;
{
Setting.renderSignupLink(application, i18next.t("login:sign up now"))
}
</>
)
}
2021-11-28 21:10:21 +08:00
</span>
</React.Fragment>
);
2021-06-14 22:42:58 +08:00
}
}
2022-09-30 01:51:58 +08:00
sendSilentSigninData(data) {
if (Setting.inIframe()) {
const message = {tag: "Casdoor", type: "SilentSignin", data: data};
window.parent.postMessage(message, "*");
}
}
2021-10-09 21:15:57 +08:00
renderSignedInBox() {
if (this.props.account === undefined || this.props.account === null) {
2022-09-30 01:51:58 +08:00
this.sendSilentSigninData("user-not-logged-in");
2021-10-09 21:15:57 +08:00
return null;
}
2022-09-30 01:51:58 +08:00
const application = this.getApplicationObj();
if (this.props.account.owner !== application.organization) {
return null;
}
2021-10-09 21:15:57 +08:00
const params = new URLSearchParams(this.props.location.search);
const silentSignin = params.get("silentSignin");
2022-01-23 13:02:55 +08:00
if (silentSignin !== null) {
2022-09-30 01:51:58 +08:00
this.sendSilentSigninData("signing-in");
2022-01-23 13:02:55 +08:00
const values = {};
2022-01-23 13:02:55 +08:00
values["application"] = this.state.application.name;
this.onFinish(values);
}
2022-09-27 20:06:46 +08:00
if (application.enableAutoSignin) {
const values = {};
values["application"] = this.state.application.name;
this.onFinish(values);
}
2021-10-09 21:15:57 +08:00
return (
<div>
{/* {*/}
2022-04-25 20:00:57 +08:00
{/* JSON.stringify(silentSignin)*/}
{/* }*/}
2021-10-09 21:15:57 +08:00
<div style={{fontSize: 16, textAlign: "left"}}>
{i18next.t("login:Continue with")}&nbsp;:
</div>
<br />
2021-10-09 21:15:57 +08:00
<SelfLoginButton account={this.props.account} onClick={() => {
const values = {};
2021-10-09 21:15:57 +08:00
values["application"] = this.state.application.name;
this.onFinish(values);
}} />
<br />
<br />
2021-10-09 21:15:57 +08:00
<div style={{fontSize: 16, textAlign: "left"}}>
{i18next.t("login:Or sign in with another account")}&nbsp;:
</div>
</div>
);
2021-10-09 21:15:57 +08:00
}
2022-09-25 21:41:52 +08:00
signInWithWebAuthn(username, values) {
const oAuthParams = Util.getOAuthGetParameters();
this.populateOauthValues(values);
const application = this.getApplicationObj();
return fetch(`${Setting.ServerUrl}/api/webauthn/signin/begin?owner=${application.organization}&name=${username}`, {
method: "GET",
2022-08-06 23:54:56 +08:00
credentials: "include",
})
.then(res => res.json())
.then((credentialRequestOptions) => {
if ("status" in credentialRequestOptions) {
Setting.showMessage("error", credentialRequestOptions.msg);
throw credentialRequestOptions.status.msg;
}
credentialRequestOptions.publicKey.challenge = UserWebauthnBackend.webAuthnBufferDecode(credentialRequestOptions.publicKey.challenge);
credentialRequestOptions.publicKey.allowCredentials.forEach(function(listItem) {
listItem.id = UserWebauthnBackend.webAuthnBufferDecode(listItem.id);
});
return navigator.credentials.get({
2022-08-06 23:54:56 +08:00
publicKey: credentialRequestOptions.publicKey,
2022-07-12 20:45:22 +08:00
});
})
.then((assertion) => {
const authData = assertion.response.authenticatorData;
const clientDataJSON = assertion.response.clientDataJSON;
const rawId = assertion.rawId;
const sig = assertion.response.signature;
const userHandle = assertion.response.userHandle;
2022-10-01 11:10:55 +08:00
return fetch(`${Setting.ServerUrl}/api/webauthn/signin/finish${AuthBackend.oAuthParamsToQuery(oAuthParams)}`, {
method: "POST",
credentials: "include",
body: JSON.stringify({
id: assertion.id,
rawId: UserWebauthnBackend.webAuthnBufferEncode(rawId),
type: assertion.type,
response: {
authenticatorData: UserWebauthnBackend.webAuthnBufferEncode(authData),
clientDataJSON: UserWebauthnBackend.webAuthnBufferEncode(clientDataJSON),
signature: UserWebauthnBackend.webAuthnBufferEncode(sig),
userHandle: UserWebauthnBackend.webAuthnBufferEncode(userHandle),
},
2022-08-06 23:54:56 +08:00
}),
})
.then(res => res.json()).then((res) => {
if (res.status === "ok") {
2022-09-25 21:41:52 +08:00
const responseType = values["type"];
if (responseType === "code") {
this.postCodeLoginAction(res);
} else if (responseType === "token" || responseType === "id_token") {
const accessToken = res.data;
Setting.goToLink(`${oAuthParams.redirectUri}#${responseType}=${accessToken}?state=${oAuthParams.state}&token_type=bearer`);
} else {
Setting.showMessage("success", i18next.t("login:Successfully logged in with webauthn credentials"));
2022-09-25 21:41:52 +08:00
Setting.goToLink("/");
}
} else {
Setting.showMessage("error", res.msg);
}
})
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}${error}`);
});
2022-07-12 20:45:22 +08:00
});
}
renderPasswordOrCodeInput() {
const application = this.getApplicationObj();
if (this.state.loginMethod === "password") {
2022-10-03 15:10:48 +08:00
return (
<Col span={24}>
<Form.Item
name="password"
rules={[{required: true, message: i18next.t("login:Please input your password!")}]}
>
2022-10-03 15:10:48 +08:00
<Input.Password
prefix={<LockOutlined className="site-form-item-icon" />}
type="password"
placeholder={i18next.t("login:Password")}
disabled={!application.enablePassword}
/>
</Form.Item>
</Col>
2022-07-12 20:45:22 +08:00
);
2022-10-03 15:10:48 +08:00
} else if (this.state.loginMethod === "verificationCode") {
return (
<Col span={24}>
<Form.Item
name="code"
rules={[{required: true, message: i18next.t("login:Please input your code!")}]}
>
<CountDownInput
disabled={this.state.username?.length === 0 || !this.state.validEmailOrPhone}
onButtonClickArgs={[this.state.username, this.state.validEmail ? "email" : "phone", Setting.getApplicationName(application)]}
application={application}
/>
</Form.Item>
</Col>
);
} else {
return null;
}
}
2022-07-12 20:45:22 +08:00
renderMethodChoiceBox() {
const application = this.getApplicationObj();
const items = [
{label: i18next.t("login:Password"), key: "password"},
];
application.enableCodeSignin ? items.push({label: i18next.t("login:Verification Code"), key: "verificationCode"}) : null;
application.enableWebAuthn ? items.push({label: i18next.t("login:WebAuthn"), key: "webAuthn"}) : null;
2022-10-03 15:10:48 +08:00
if (application.enableCodeSignin || application.enableWebAuthn) {
return (
<div>
<Tabs items={items} size={"small"} defaultActiveKey="password" onChange={(key) => {this.setState({loginMethod: key});}} centered>
</Tabs>
</div>
2022-07-12 20:45:22 +08:00
);
}
}
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
}
if (application.signinHtml !== "") {
return (
<div dangerouslySetInnerHTML={{__html: application.signinHtml}} />
);
}
const visibleOAuthProviderItems = application.providers.filter(providerItem => this.isProviderVisible(providerItem));
if (this.props.application === undefined && !application.enablePassword && visibleOAuthProviderItems.length === 1) {
Setting.goToLink(Provider.getAuthUrl(application, visibleOAuthProviderItems[0].provider, "signup"));
return (
<div style={{textAlign: "center"}}>
<Spin size="large" tip={i18next.t("login:Signing in...")} style={{paddingTop: "10%"}} />
</div>
);
}
2021-02-13 12:15:19 +08:00
return (
<div className="loginBackground" style={{backgroundImage: Setting.inIframe() || Setting.isMobile() ? null : `url(${application.formBackgroundUrl})`}}>
<CustomGithubCorner />
<div className="login-content" style={{margin: this.parseOffset(application.formOffset)}}>
2022-12-04 15:53:46 +08:00
{Setting.inIframe() || Setting.isMobile() ? null : <div dangerouslySetInnerHTML={{__html: application.formCss}} />}
<div className="login-panel">
<div className="side-image" style={{display: application.formOffset !== 4 ? "none" : null}}>
<div dangerouslySetInnerHTML={{__html: application.formSideHtml}} />
</div>
<div className="login-form">
<div >
<div>
{
Setting.renderHelmet(application)
}
{
Setting.renderLogo(application)
}
{/* {*/}
{/* this.state.clientId !== null ? "Redirect" : null*/}
{/* }*/}
<SelectLanguageBox languages={application.organizationObj.languages} style={{top: "55px", right: "5px", position: "absolute"}} />
{
this.renderSignedInBox()
}
{
this.renderForm(application)
}
</div>
</div>
</div>
</div>
</div>
</div>
);
2021-02-13 12:15:19 +08:00
}
}
export default LoginPage;