Logging in

When you first load the UI, you should see the login screen that looks like this:

The following mock users exist as part of the API:

  • user1
  • user2
  • user3
  • user4
  • user5

The password isn't actually validated against anything, so leaving it blank or entering gibberish should authenticate any of the preceding users. Let's take a look at the Login component that renders this page:

import React, { Component } from 'react';

import { withStyles } from 'material-ui/styles';
import TextField from 'material-ui/TextField';
import Button from 'material-ui/Button';

import { login } from './api';

const styles = theme => ({
  container: {
    display: 'flex',
    flexWrap: 'wrap'
  },
  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit,
    width: 200
  },
  button: {
    margin: theme.spacing.unit
  }
});

class Login extends Component {
  state = {
    user: '',
    password: ''
  };

onInputChange = name => event => { this.setState({ [name]: event.target.value }); }; onLoginClick = () => { login(this.state).then(resp => { if (resp.status === 200) { this.props.history.push('/'); } }); }; componentWillMount() { this.props.setTitle('Login'); } render() { const { classes } = this.props; return ( <div className={classes.container}> <TextField id="user" label="User" className={classes.textField} value={this.state.user} onChange={this.onInputChange('user')} margin="normal" /> <TextField id="password" label="Password" className={classes.textField} value={this.state.password} onChange={this.onInputChange('password')} type="password" autoComplete="current-password" margin="normal" /> <Button variant="raised" color="primary" className={classes.button} onClick={this.onLoginClick} > Login </Button> </div> ); } } export default withStyles(styles)(Login);

There's a lot of Material-UI going on here, but feel free to ignore the majority of it. The important bit is the login() function that's imported from the api module. This is used to make a call to the /api/login endpoint. The reason this is relevant from the perspective of production React deployment is because this is an interaction with a service that might be deployed as its own container.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.220.114