// 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"; import {Link} from "react-router-dom"; import {Button, Checkbox, Col, Form, Input, Result, Row, Spin} from "antd"; import {LockOutlined, UserOutlined} from "@ant-design/icons"; import * as AuthBackend from "./AuthBackend"; import * as ApplicationBackend from "../backend/ApplicationBackend"; import * as Provider from "./Provider"; import * as Util from "./Util"; import * as Setting from "../Setting"; import {GithubLoginButton, GoogleLoginButton} from "react-social-login-buttons"; import FacebookLoginButton from "./FacebookLoginButton"; import QqLoginButton from "./QqLoginButton"; import DingTalkLoginButton from "./DingTalkLoginButton"; import GiteeLoginButton from "./GiteeLoginButton"; import WechatLoginButton from "./WechatLoginButton"; import WeiboLoginButton from "./WeiboLoginButton"; import i18next from "i18next"; import LinkedInLoginButton from "./LinkedInLoginButton"; import WeComLoginButton from "./WeComLoginButton"; class LoginPage extends React.Component { constructor(props) { super(props); this.state = { classes: props, type: props.type, applicationName: props.applicationName !== undefined ? props.applicationName : (props.match === undefined ? null : props.match.params.applicationName), application: null, mode: props.mode !== undefined ? props.mode : (props.match === undefined ? null : props.match.params.mode), // "signup" or "signin" msg: null, }; } UNSAFE_componentWillMount() { 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() { const oAuthParams = Util.getOAuthGetParameters(); AuthBackend.getApplicationLogin(oAuthParams) .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, }); } }); } getApplication() { if (this.state.applicationName === null) { return; } ApplicationBackend.getApplication("admin", this.state.applicationName) .then((application) => { this.setState({ application: application, }); }); } getApplicationObj() { if (this.props.application !== undefined) { return this.props.application; } else { return this.state.application; } } onUpdateAccount(account) { this.props.onUpdateAccount(account); } onFinish(values) { const application = this.getApplicationObj(); const ths = this; values["type"] = this.state.type; const oAuthParams = Util.getOAuthGetParameters(); AuthBackend.login(values, oAuthParams) .then((res) => { if (res.status === 'ok') { const responseType = this.state.type; if (responseType === "login") { Util.showMessage("success", `Logged in successfully`); Setting.goToLink("/"); } else if (responseType === "code") { const code = res.data; 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}?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 { Setting.goToLink(`${oAuthParams.redirectUri}?code=${code}&state=${oAuthParams.state}`); } // Util.showMessage("success", `Authorization code: ${res.data}`); } } else { Util.showMessage("error", `Failed to log in: ${res.msg}`); } }); }; getSigninButton(type) { const text = i18next.t("login:Sign in with {type}").replace("{type}", type); if (type === "GitHub") { return } else if (type === "Google") { return } else if (type === "QQ") { return } else if (type === "Facebook") { return } else if (type === "Weibo") { return } else if (type === "Gitee") { return } else if (type === "WeChat") { return } else if (type === "DingTalk") { return } else if (type === "LinkedIn"){ return } else if (type === "WeCom") { return } return text; } renderProviderLogo(provider, application, width, margin, size) { if (size === "small") { return ( {provider.displayName} ) } else { return ( ) } } isProviderVisible(providerItem) { if (this.state.mode === "signup") { return Setting.isProviderVisibleForSignUp(providerItem); } else { return Setting.isProviderVisibleForSignIn(providerItem); } } renderForm(application) { if (this.state.msg !== null) { return Util.renderMessage(this.state.msg) } if (this.state.mode === "signup" && !application.enableSignUp) { return ( { Setting.goToLogin(this, application); }}> ]} > ) } if (application.enablePassword) { return (
{this.onFinish(values)}} style={{width: "250px"}} size="large" > } placeholder={i18next.t("login:username, Email or phone")} disabled={!application.enablePassword} /> } type="password" placeholder={i18next.t("login:Password")} disabled={!application.enablePassword} /> {i18next.t("login:Auto sign in")} { Setting.goToForget(this, application); }}> {i18next.t("login:Forgot password?")} { !application.enableSignUp ? null : this.renderFooter(application) } { application.providers.filter(providerItem => this.isProviderVisible(providerItem)).map(providerItem => { return this.renderProviderLogo(providerItem.provider, application, 30, 5, "small"); }) }
); } else { return (
{i18next.t("login:To access")}  {application.displayName} :

{ application.providers.filter(providerItem => this.isProviderVisible(providerItem)).map(providerItem => { return this.renderProviderLogo(providerItem.provider, application, 40, 10, "big"); }) } { !application.enableSignUp ? null : (

{ this.renderFooter(application) }
) }
) } } renderFooter(application) { if (this.state.mode === "signup") { return (
{i18next.t("signup:Have account?")}  { Setting.goToLogin(this, application); }}> {i18next.t("signup:sign in now")}
) } else { return ( ) } } render() { const application = this.getApplicationObj(); if (application === null) { return Util.renderMessageLarge(this, this.state.msg); } const visibleOAuthProviderItems = application.providers.filter(providerItem => this.isProviderVisible(providerItem)); if (this.props.application === undefined && visibleOAuthProviderItems.length === 1) { Setting.goToLink(Provider.getAuthUrl(application, visibleOAuthProviderItems[0].provider, "signup")); return (
) } return (
{ Setting.renderHelmet(application) } { Setting.renderLogo(application) } {/*{*/} {/* this.state.clientId !== null ? "Redirect" : null*/} {/*}*/} { this.renderForm(application) }
) } } export default LoginPage;