Files
SCM-Manager/scm-ui/ui-webapp/src/containers/Login.tsx

110 lines
3.4 KiB
TypeScript
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React from "react";
import { connect } from "react-redux";
2020-08-04 11:41:00 +02:00
import { Redirect, RouteComponentProps, withRouter } from "react-router-dom";
import { compose } from "redux";
import styled from "styled-components";
2020-08-04 11:41:00 +02:00
import { getLoginFailure, getMe, isAnonymous, isLoginPending, login } from "../modules/auth";
import { getLoginInfoLink, getLoginLink } from "../modules/indexResource";
import LoginInfo from "../components/LoginInfo";
2020-08-04 11:41:00 +02:00
import { Me } from "@scm-manager/ui-types";
2018-07-02 16:05:58 +02:00
2020-08-04 11:41:00 +02:00
type Props = RouteComponentProps & {
authenticated: boolean;
2020-08-04 11:41:00 +02:00
me: Me;
loading: boolean;
error?: Error;
link: string;
loginInfoLink?: string;
2018-07-13 10:57:11 +02:00
// dispatcher props
login: (link: string, username: string, password: string) => void;
2018-07-10 16:52:23 +02:00
};
const HeroSection = styled.section`
padding-top: 2em;
`;
2018-07-05 16:48:56 +02:00
2019-08-13 08:23:37 +02:00
class Login extends React.Component<Props> {
handleLogin = (username: string, password: string): void => {
2019-08-13 08:23:37 +02:00
const { link, login } = this.props;
login(link, username, password);
2018-07-11 14:59:01 +02:00
};
2018-07-05 16:48:56 +02:00
2018-07-13 10:57:11 +02:00
renderRedirect = () => {
const { from } = this.props.location.state || {
from: {
pathname: "/"
}
};
return <Redirect to={from} />;
2018-07-13 10:57:11 +02:00
};
2018-07-02 16:05:58 +02:00
render() {
2020-08-04 11:41:00 +02:00
const { authenticated, me, ...restProps } = this.props;
2018-07-13 10:57:11 +02:00
2020-08-04 11:41:00 +02:00
if (authenticated && !!me) {
2018-07-13 10:57:11 +02:00
return this.renderRedirect();
}
2018-07-02 16:05:58 +02:00
return (
<HeroSection className="hero">
2018-07-11 21:01:29 +02:00
<div className="hero-body">
2019-08-13 08:23:37 +02:00
<div className="container">
<div className="columns is-centered">
<LoginInfo loginHandler={this.handleLogin} {...restProps} />
2018-07-11 14:59:01 +02:00
</div>
</div>
2018-07-02 16:05:58 +02:00
</div>
</HeroSection>
);
2018-07-02 16:05:58 +02:00
}
}
2020-01-08 13:40:07 +01:00
const mapStateToProps = (state: any) => {
const authenticated = state?.auth?.me && !isAnonymous(state.auth.me);
2020-08-04 11:41:00 +02:00
const me = getMe(state);
const loading = isLoginPending(state);
const error = getLoginFailure(state);
2018-10-11 09:54:12 +02:00
const link = getLoginLink(state);
const loginInfoLink = getLoginInfoLink(state);
return {
authenticated,
2020-08-04 11:41:00 +02:00
me,
loading,
2018-10-11 08:30:37 +02:00
error,
link,
loginInfoLink
};
2018-07-05 16:48:56 +02:00
};
2020-01-08 13:40:07 +01:00
const mapDispatchToProps = (dispatch: any) => {
2018-07-05 16:48:56 +02:00
return {
2019-10-21 10:57:56 +02:00
login: (loginLink: string, username: string, password: string) => dispatch(login(loginLink, username, password))
2018-07-05 16:48:56 +02:00
};
};
2020-01-08 13:40:07 +01:00
export default compose(withRouter, connect(mapStateToProps, mapDispatchToProps))(Login);