// Copyright (c) 2015 Spinpunch, 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'); const Constants = require('../utils/constants.jsx'); import {config, strings} from '../utils/config.js'; export default class Login extends React.Component { constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); this.state = {}; } handleSubmit(e) { e.preventDefault(); let state = {}; const name = this.props.teamName; if (!name) { state.serverError = 'Bad team name'; this.setState(state); return; } const email = React.findDOMNode(this.refs.email).value.trim(); if (!email) { state.serverError = 'An email is required'; this.setState(state); return; } const password = React.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, function loggedIn(data) { UserStore.setCurrentUser(data); UserStore.setLastEmail(email); const redirect = Utils.getUrlParameter('redirect'); if (redirect) { window.location.href = decodeURIComponent(redirect); } else { window.location.href = '/' + name + '/channels/town-square'; } }, function loginFailed(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); }.bind(this) ); } 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 !== '') { focusPassword = true; } else { focusEmail = true; } const authServices = JSON.parse(this.props.authServices); let loginMessage = []; if (authServices.indexOf(Constants.GITLAB_SERVICE) !== -1) { loginMessage.push( with GitLab ); } let errorClass = ''; if (serverError) { errorClass = ' has-error'; } let emailSignup; if (authServices.indexOf(Constants.EMAIL_SERVICE) !== -1) { emailSignup = (