|
1 | 1 | import React from 'react'; |
| 2 | +import { Router } from 'react-router' |
2 | 3 |
|
3 | | -import iClientUser from './../../services/iClientUser'; |
| 4 | +import User from 'services/User'; |
| 5 | +import ErrorComponent from 'components/Error/Error'; |
4 | 6 |
|
5 | | -class LoginComponent extends React.Component{ |
| 7 | +class LoginComponent extends React.Component |
| 8 | +{ |
6 | 9 | constructor(props, context) { |
7 | 10 | super(props, context); |
8 | 11 | this.handleSubmit = this.handleSubmit.bind(this); |
| 12 | + this.handleDeleteMessage = this.handleDeleteMessage.bind(this); |
| 13 | + this.state = { |
| 14 | + error: '' |
| 15 | + }; |
9 | 16 | } |
10 | 17 |
|
11 | 18 | handleSubmit(e) { |
12 | 19 | e.preventDefault(); |
13 | 20 |
|
14 | | - iClientUser.login( |
| 21 | + User.login( |
15 | 22 | this.refs.username.value, |
16 | 23 | this.refs.password.value |
17 | 24 | ).then((response) => { |
18 | 25 | if (response.data.success == 200) { |
19 | 26 | localStorage.token = response.data.token; |
20 | | - window.location.href = "/"; |
| 27 | + this.context.router.push("/"); |
| 28 | + } |
| 29 | + }).catch((error) => { |
| 30 | + this.setState({error: 'Authentication failed'}); |
| 31 | + if (error.response.data) { |
| 32 | + this.setState({error: error.response.data.error}); |
21 | 33 | } |
22 | 34 | }); |
23 | 35 | } |
24 | 36 |
|
| 37 | + handleDeleteMessage() { |
| 38 | + this.setState({error: ''}); |
| 39 | + } |
| 40 | + |
25 | 41 | render() { |
26 | 42 | return ( |
27 | | - <div className="hero-body"> |
28 | | - <div className="container"> |
29 | | - <div className="columns is-vcentered"> |
30 | | - <div className="column is-4 is-offset-4"> |
31 | | - <h1 className="title has-text-centered"> |
32 | | - IClient |
33 | | - </h1> |
34 | | - <form onSubmit={this.handleSubmit}> |
35 | | - <div className="box"> |
36 | | - <label className="label">Username</label> |
37 | | - <p className="control"> |
38 | | - <input |
39 | | - ref='username' |
40 | | - className="input" |
41 | | - type="text" |
42 | | - placeholder="Ex: jsmith" |
43 | | - /> |
44 | | - </p> |
45 | | - <label className="label">Password</label> |
46 | | - <p className="control"> |
47 | | - <input |
48 | | - ref='password' |
49 | | - className="input" |
50 | | - type="password" |
51 | | - placeholder="●●●●●●●" |
52 | | - /> |
53 | | - </p> |
54 | | - <hr /> |
55 | | - <p className="control"> |
56 | | - <button className="button is-primary">Login</button> |
57 | | - </p> |
58 | | - <p className="has-text-centered"> |
59 | | - <a href="register.html">Register an Account</a> |
60 | | - | |
61 | | - <a href="#">Forgot password</a> |
62 | | - </p> |
63 | | - </div> |
64 | | - </form> |
| 43 | + <section className="hero is-fullheight is-primary"> |
| 44 | + <div className="hero-body"> |
| 45 | + <div className="container"> |
| 46 | + <div className="columns is-vcentered"> |
| 47 | + <div className="column is-4 is-offset-4"> |
| 48 | + <h1 className="title has-text-centered"> |
| 49 | + IClient |
| 50 | + </h1> |
| 51 | + <form onSubmit={this.handleSubmit}> |
| 52 | + <div className="box"> |
| 53 | + <ErrorComponent error={this.state.error} /> |
| 54 | + <label className="label">Username</label> |
| 55 | + <p className="control"> |
| 56 | + <input |
| 57 | + ref='username' |
| 58 | + className="input" |
| 59 | + type="text" |
| 60 | + placeholder="Ex: jsmith" |
| 61 | + /> |
| 62 | + </p> |
| 63 | + <label className="label">Password</label> |
| 64 | + <p className="control"> |
| 65 | + <input |
| 66 | + ref='password' |
| 67 | + className="input" |
| 68 | + type="password" |
| 69 | + placeholder="●●●●●●●" |
| 70 | + /> |
| 71 | + </p> |
| 72 | + <hr /> |
| 73 | + <p className="control"> |
| 74 | + <button className="button is-primary">Login</button> |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + </form> |
| 78 | + </div> |
65 | 79 | </div> |
66 | 80 | </div> |
67 | 81 | </div> |
68 | | - </div> |
| 82 | + </section> |
69 | 83 | ); |
70 | 84 | } |
71 | 85 | } |
72 | 86 |
|
| 87 | +LoginComponent.contextTypes = { |
| 88 | + router: React.PropTypes.object.isRequired |
| 89 | +}; |
| 90 | + |
73 | 91 | export default LoginComponent; |
0 commit comments