Advertisement
Guest User

Untitled

a guest
Oct 8th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { Link, Redirect } from 'react-router-dom';
  3. import { bindActionCreators } from 'redux';
  4. import { connect } from 'react-redux';
  5. import * as authActions from '../../actions/authActions';
  6. import axios from 'axios';
  7.  
  8.  
  9. class LoginForm extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. email: '',
  14. password: '',
  15. errors: {},
  16. isLoading: false,
  17. };
  18. }
  19.  
  20. onChange(e) {
  21. this.setState({
  22. [e.target.name]: e.target.value
  23. })
  24. }
  25.  
  26. onSubmit(e) {
  27. e.preventDefault();
  28. this.setState({ errors: {}, isLoading: true });
  29. this.props.actions.logInUser( { data: { user: { email: this.state.email, password: this.state.password }}})
  30. }
  31.  
  32. render() {
  33. return(
  34. <div>
  35. <form onSubmit={this.onSubmit.bind(this)}>
  36. <div className="field">
  37. <label className="label"> Email </label>
  38. <div className="control">
  39. <input type="email"
  40. name="email"
  41. value={this.state.email}
  42. onChange={this.onChange.bind(this)}
  43. className="input" />
  44. </div>
  45. </div>
  46. <div className="field">
  47. <label className="label"> Mot de passe </label>
  48. <div className="control">
  49. <input type="password"
  50. ref="password"
  51. name="password"
  52. value={this.state.password}
  53. onChange={this.onChange.bind(this)}
  54. className="input" />
  55. </div>
  56. </div>
  57. <div className="form-group">
  58. <input type="submit" value="Signup" className="button is-primary" />
  59. </div>
  60. <Link to={{ pathname: '/register' }}>Inscription</Link>
  61. </form>
  62. </div>
  63. );
  64. }
  65. }
  66.  
  67. function mapDispatchToProps(dispatch) {
  68. return {
  69. actions: bindActionCreators(authActions, dispatch)
  70. };
  71. }
  72.  
  73. export default connect(null, mapDispatchToProps)(LoginForm);
  74.  
  75. import axios from 'axios';
  76. import setAuthorizationToken from '../utils/setAuthorizationToken';
  77. import jwtDecode from 'jwt-decode';
  78. import { browserHistory } from 'react-router';
  79. import * as types from './types';
  80. import sessionApi from '../api/SessionApi';
  81.  
  82. export function loginSuccess() {
  83. return {
  84. type: types.LOG_IN_SUCCESS
  85. }
  86. }
  87.  
  88.  
  89. export function loginFailed() {
  90. return {
  91. type: types.LOG_IN_FAILED
  92. }
  93. }
  94.  
  95. export function logInUser(credentials) {
  96. return function(dispatch) {
  97. return sessionApi.login(credentials)
  98. .then(response => {
  99. console.log(response);
  100. if(response.data) {
  101. sessionStorage.setItem('jwt', response.data.authentication_token);
  102. dispatch(loginSuccess());
  103. browserHistory.push('/dashboard/' + response.data.id);
  104. } else {
  105. dispatch(loginFailed());
  106. }
  107. })
  108. .catch(error => {
  109. throw(error);
  110. })
  111. }
  112. }
  113.  
  114. import React, { Component } from 'react';
  115. import axios from 'axios';
  116. import * as authActions from '../../actions/authActions';
  117.  
  118. class Dashboard extends Component {
  119. constructor(props) {
  120. super(props);
  121. }
  122.  
  123. render() {
  124. return(
  125. <div>
  126. <h1> Hello user </h1>
  127. </div>
  128. );
  129. }
  130. }
  131.  
  132. export default Dashboard;
  133.  
  134. import * as types from '../actions/types';
  135. import initialState from './initialState';
  136. import { browserHistory } from 'react-router';
  137.  
  138. export default function sessionReducer(state = initialState.session, action) {
  139. switch(action.type) {
  140. case types.LOG_IN_SUCCESS:
  141. return !!sessionStorage.jwt;
  142. case types.LOG_IN_FAILED:
  143. console.log('login failed');
  144. browserHistory.push('/login');
  145. default:
  146. return state;
  147. }
  148. }
  149.  
  150. import axios from 'axios';
  151. class SessionApi {
  152. static login(credentials) {
  153. return axios.post('https://spendin-api.herokuapp.com/api/v1/sessions', credentials)
  154. .then(response => {
  155. console.log(response);
  156. return response;
  157. })
  158. .catch(error => {
  159. return error;
  160. });
  161. }
  162. }
  163.  
  164. export default SessionApi;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement