Skip to content
Snippets Groups Projects
login.js 3.32 KiB
Newer Older
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import React from 'react';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import PropTypes from 'prop-types';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import { Text, TextInput, StyleSheet } from 'react-native';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import * as actions from '../actions';
import RocketChat from '../lib/rocketchat';
import KeyboardView from '../components/KeyboardView';

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
const styles = StyleSheet.create({
	view: {
		flex: 1,
		flexDirection: 'column',
		justifyContent: 'center',
		alignItems: 'stretch',
		backgroundColor: '#fff'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	},
	input: {
		height: 40,
		borderColor: '#aaa',
		marginLeft: 20,
		marginRight: 20,
		marginTop: 10,
		padding: 5,
		borderWidth: 0,
		backgroundColor: '#f6f6f6'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	},
	error: {
		textAlign: 'center',
		color: 'red',
		paddingTop: 5
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
@connect(state => ({
	server: state.server,
	Accounts_EmailOrUsernamePlaceholder: state.settings.Accounts_EmailOrUsernamePlaceholder,
	Accounts_PasswordPlaceholder: state.settings.Accounts_PasswordPlaceholder
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
}), dispatch => ({
	actions: bindActionCreators(actions, dispatch)
}))
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
export default class LoginView extends React.Component {
	static propTypes = {
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		navigator: PropTypes.object.isRequired,
		server: PropTypes.string.isRequired,
		Accounts_EmailOrUsernamePlaceholder: PropTypes.string,
		Accounts_PasswordPlaceholder: PropTypes.string
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	static navigationOptions = () => ({
		title: 'Login'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	});

	constructor(props) {
		super(props);

		this.state = {
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			username: '',
			password: ''
		this.props.navigator.setTitle({
			title: 'Login'
		});
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	componentWillReceiveProps(nextProps) {
		this.props.navigator.setSubTitle({
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			subtitle: nextProps.server
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		this.setState({
			error: undefined
		});

		const credentials = {
			username: this.state.username,
			password: this.state.password,
			code: this.state.code
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed

		RocketChat.loginWithPassword(credentials, (error) => {
			if (error) {
				if (error.error === 'totp-required') {
					this.setState({ totp: true });
					this.codeInput.focus();
				} else {
					this.setState({
						error: error.reason
					});
				}
			} else {
				this.props.navigator.dismissModal();
			}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	renderTOTP = () => {
		if (this.state.totp) {
			return (
				<TextInput
					ref={ref => this.codeInput = ref}
					style={styles.input}
					onChangeText={code => this.setState({ code })}
					keyboardType='numeric'
					autoCorrect={false}
					returnKeyType='done'
					autoCapitalize='none'
					onSubmitEditing={this.submit}
					placeholder='Code'
				/>
			);
		}
	}

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	render() {
		return (
			<KeyboardView style={styles.view} keyboardVerticalOffset={64}>
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				<TextInput
					style={styles.input}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					onChangeText={username => this.setState({ username })}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					keyboardType='email-address'
					autoCorrect={false}
					returnKeyType='done'
					autoCapitalize='none'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					autoFocus
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					onSubmitEditing={this.submit}
					placeholder={this.props.Accounts_EmailOrUsernamePlaceholder || 'Email or username'}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				/>
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				<TextInput
					style={styles.input}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					onChangeText={password => this.setState({ password })}
					secureTextEntry
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					autoCorrect={false}
					returnKeyType='done'
					autoCapitalize='none'
					onSubmitEditing={this.submit}
					placeholder={this.props.Accounts_PasswordPlaceholder || 'Password'}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				/>
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				{this.renderTOTP()}
				<Text style={styles.error}>{this.state.error}</Text>
			</KeyboardView>