// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. const Utils = require('../utils/utils.jsx'); const Client = require('../utils/client.jsx'); const UserStore = require('../stores/user_store.jsx'); const BrowserStore = require('../stores/browser_store.jsx'); export default class Login extends React.Component { constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); this.state = {}; } handleSubmit(e) { e.preventDefault(); var state = {}; const name = this.props.teamName; if (!name) { state.serverError = 'Bad team name'; this.setState(state); return; } const email = ReactDOM.findDOMNode(this.refs.email).value.trim(); if (!email) { state.serverError = 'An email is required'; this.setState(state); return; } const password = ReactDOM.findDOMNode(this.refs.password).value.trim(); if (!password) { state.serverError = 'A password is required'; this.setState(state); return; } if (!BrowserStore.isLocalStorageSupported()) { state.serverError = 'This service requires local storage to be enabled. Please enable it or exit private browsing.'; this.setState(state); return; } state.serverError = ''; this.setState(state); Client.loginByEmail(name, email, password, () => { UserStore.setLastEmail(email); const redirect = Utils.getUrlParameter('redirect'); if (redirect) { window.location.href = decodeURIComponent(redirect); } else { window.location.href = '/' + name + '/channels/town-square'; } }, (err) => { if (err.message === 'Login failed because email address has not been verified') { window.location.href = '/verify_email?teamname=' + encodeURIComponent(name) + '&email=' + encodeURIComponent(email); return; } state.serverError = err.message; this.valid = false; this.setState(state); } ); } render() { let serverError; if (this.state.serverError) { serverError = ; } let priorEmail = UserStore.getLastEmail(); const emailParam = Utils.getUrlParameter('email'); if (emailParam) { priorEmail = decodeURIComponent(emailParam); } const teamDisplayName = this.props.teamDisplayName; const teamName = this.props.teamName; let focusEmail = false; let focusPassword = false; if (priorEmail === '') { focusEmail = true; } else { focusPassword = true; } let loginMessage = []; if (global.window.mm_config.EnableSignUpWithGitLab === 'true') { loginMessage.push( {'with GitLab'} ); } let errorClass = ''; if (serverError) { errorClass = ' has-error'; } const verifiedParam = Utils.getUrlParameter('verified'); let verifiedBox = ''; if (verifiedParam) { verifiedBox = (
{' Email Verified'}
); } let emailSignup; if (global.window.mm_config.EnableSignUpWithEmail === 'true') { emailSignup = (
); } if (loginMessage.length > 0 && emailSignup) { loginMessage = (
{loginMessage}
{'or'}
); } let forgotPassword; if (emailSignup) { forgotPassword = (
{'I forgot my password'}
); } let userSignUp = null; if (this.props.inviteId) { userSignUp = (
{`Don't have an account? `} {'Create one now'}
); } let teamSignUp = null; if (global.window.mm_config.EnableTeamCreation === 'true') { teamSignUp = (
{'Want to create your own team? '} {'Create one now'}
); } return (
{'Sign in to:'}

{teamDisplayName}

{'on '}{global.window.mm_config.SiteName}

{verifiedBox}
{serverError}
{loginMessage} {emailSignup} {userSignUp}
{'Find other teams'}
{forgotPassword} {teamSignUp}
); } } Login.defaultProps = { teamName: '', teamDisplayName: '' }; Login.propTypes = { teamName: React.PropTypes.string, teamDisplayName: React.PropTypes.string, inviteId: React.PropTypes.string };