Skip to content
Snippets Groups Projects
login.js 1.83 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';
import { TextInput, StyleSheet, KeyboardAvoidingView, Platform } from 'react-native';
import realm from '../lib/realm';
import { loginWithPassword, loadSubscriptions, Accounts } from '../lib/meteor';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed

const styles = StyleSheet.create({
	view: {
		flex: 1,
		flexDirection: 'column',
		justifyContent: 'center',
		alignItems: 'stretch'
	},
	input: {
		height: 40,
		borderColor: '#aaa',
		marginLeft: 20,
		marginRight: 20,
		marginTop: 10,
		padding: 5,
		borderWidth: 0,
		backgroundColor: '#f6f6f6'
	}
});

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
Accounts.onLogin(() => {
	loadSubscriptions(() => {
		navigate('Rooms');
	});
});

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
export default class LoginView extends React.Component {
	static propTypes = {
		navigation: PropTypes.object.isRequired
	}

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	static navigationOptions = () => ({
		title: realm.objectForPrimaryKey('settings', 'Site_Name').value
	});

	constructor(props) {
		super(props);

		this.state = {
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			username: '',
			password: ''
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		navigate = this.props.navigation.navigate;
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		this.submit = () => {
			loginWithPassword({ username: this.state.username }, this.state.password);
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		};
	}

	render() {
		return (
Diego Sampaio's avatar
Diego Sampaio committed
			<KeyboardAvoidingView style={styles.view} behavior={Platform.OS === 'ios' && 'padding'}>
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}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					placeholder='Email or username'
				/>
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}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					placeholder='Password'
				/>
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			</KeyboardAvoidingView>