// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import * as Utils from '../utils/utils.jsx'; import * as client from '../utils/client.jsx'; import UserStore from '../stores/user_store.jsx'; import BrowserStore from '../stores/browser_store.jsx'; export default class SignupUserComplete extends React.Component { constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); var initialState = BrowserStore.getGlobalItem(this.props.hash); if (!initialState) { initialState = {}; initialState.wizard = 'welcome'; initialState.user = {}; initialState.user.team_id = this.props.teamId; initialState.user.email = this.props.email; initialState.original_email = this.props.email; } this.state = initialState; } handleSubmit(e) { e.preventDefault(); const providedEmail = ReactDOM.findDOMNode(this.refs.email).value.trim(); if (!providedEmail) { this.setState({nameError: '', emailError: 'This field is required', passwordError: ''}); return; } if (!Utils.isEmail(providedEmail)) { this.setState({nameError: '', emailError: 'Please enter a valid email address', passwordError: ''}); return; } const providedUsername = ReactDOM.findDOMNode(this.refs.name).value.trim().toLowerCase(); if (!providedUsername) { this.setState({nameError: 'This field is required', emailError: '', passwordError: '', serverError: ''}); return; } const usernameError = Utils.isValidUsername(providedUsername); if (usernameError === 'Cannot use a reserved word as a username.') { this.setState({nameError: 'This username is reserved, please choose a new one.', emailError: '', passwordError: '', serverError: ''}); return; } else if (usernameError) { this.setState({ nameError: 'Username must begin with a letter, and contain between 3 to 15 lowercase characters made up of numbers, letters, and the symbols \'.\', \'-\' and \'_\'.', emailError: '', passwordError: '', serverError: '' }); return; } const providedPassword = ReactDOM.findDOMNode(this.refs.password).value.trim(); if (!providedPassword || providedPassword.length < 5) { this.setState({nameError: '', emailError: '', passwordError: 'Please enter at least 5 characters', serverError: ''}); return; } const user = { team_id: this.props.teamId, email: providedEmail, username: providedUsername, password: providedPassword, allow_marketing: true }; this.setState({ user, nameError: '', emailError: '', passwordError: '', serverError: '' }); client.createUser(user, this.props.data, this.props.hash, () => { client.track('signup', 'signup_user_02_complete'); client.loginByEmail(this.props.teamName, user.email, user.password, () => { UserStore.setLastEmail(user.email); if (this.props.hash > 0) { BrowserStore.setGlobalItem(this.props.hash, JSON.stringify({wizard: 'finished'})); } window.location.href = '/' + this.props.teamName + '/channels/town-square'; }, (err) => { if (err.message === 'Login failed because email address has not been verified') { window.location.href = '/verify_email?email=' + encodeURIComponent(user.email) + '&teamname=' + encodeURIComponent(this.props.teamName); } else { this.setState({serverError: err.message}); } } ); }, (err) => { this.setState({serverError: err.message}); } ); } render() { client.track('signup', 'signup_user_01_welcome'); if (this.state.wizard === 'finished') { return