// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. // Copyright (c) 2015-2016 Yuya Ochiai import React from 'react'; import {Button, Col, FormLabel, Form, FormGroup, FormControl, Modal} from 'react-bootstrap'; import {LoginModalData} from 'types/auth'; import {ModalMessage} from 'types/modals'; import {AuthenticationResponseDetails, AuthInfo} from 'electron/renderer'; import urlUtils from 'common/utils/url'; import {MODAL_INFO} from 'common/communication'; type Props = { onCancel: (request: AuthenticationResponseDetails) => void; onLogin: (request: AuthenticationResponseDetails, username: string, password: string) => void; getAuthInfo: () => void; }; type State = { username: string; password: string; request?: AuthenticationResponseDetails; authInfo?: AuthInfo; }; export default class LoginModal extends React.PureComponent { constructor(props: Props) { super(props); this.state = { username: '', password: '', }; } componentDidMount() { window.addEventListener('message', this.handleAuthInfoMessage); this.props.getAuthInfo(); } componentWillUnmount() { window.removeEventListener('message', this.handleAuthInfoMessage); } handleAuthInfoMessage = (event: {data: ModalMessage}) => { switch (event.data.type) { case MODAL_INFO: { const {request, authInfo} = event.data.data; this.setState({request, authInfo}); break; } default: break; } } handleSubmit = (event: React.FormEvent) => { event.preventDefault(); this.props.onLogin(this.state.request!, this.state.username, this.state.password); this.setState({ username: '', password: '', request: undefined, authInfo: undefined, }); } handleCancel = (event: React.MouseEvent) => { event.preventDefault(); this.props.onCancel(this.state.request!); this.setState({ username: '', password: '', request: undefined, authInfo: undefined, }); } setUsername = (e: React.ChangeEvent) => { this.setState({username: e.target.value}); } setPassword = (e: React.ChangeEvent) => { this.setState({password: e.target.value}); } render() { let theServer = ''; if (!(this.state.request && this.state.authInfo)) { theServer = ''; } else if (this.state.authInfo.isProxy) { theServer = `The proxy ${this.state.authInfo.host}:${this.state.authInfo.port}`; } else { const tmpURL = urlUtils.parseURL(this.state.request.url); theServer = `The server ${tmpURL?.protocol}//${tmpURL?.host}`; } const message = `${theServer} requires a username and password.`; return ( {'Authentication Required'}

{ message }

{'User Name'} ) => { e.stopPropagation(); }} /> {'Password'} ) => { e.stopPropagation(); }} />
{ ' ' }
); } }