Guest User

Untitled

a guest
Jan 17th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import * as actions from '../../actions';
  4. import Logo from "../img/image-center.png";
  5. import "./login.css";
  6.  
  7.  
  8. class Login extends Component {
  9. constructor(props){
  10. super(props);
  11. this.state = {
  12. errorMsg : ""
  13. };
  14. this.loginClicked = this.loginClicked.bind(this);
  15. console.log("loggedIn:", this.props.login);
  16. }
  17.  
  18. loginClicked() {
  19. try {
  20. this.props.loginUser(this.refs.email.value, this.refs.password.value)
  21. .then(() => {
  22. console.log("thisprops", this.props);
  23. });
  24. }
  25. catch(ex){
  26. this.state.errorMsg = "Unable to connect to server";
  27. console.log("error", ex);
  28. }
  29. }
  30.  
  31. render() {
  32. return (
  33.  
  34. <div className="login-bg">
  35. <div className="container signForm">
  36. <div className="col s12 l6 login-form">
  37. <p className="center-align">Login to Trade Portal</p>
  38. <form>
  39. <div className="row">
  40. <div className="input-field">
  41. <input id="email" type="email" className="validate" ref="email" />
  42. <label htmlFor="email">Email</label>
  43. </div>
  44. </div>
  45. <div className="row">
  46. <div className="input-field">
  47. <input id="password" type="password" className="validate" ref="password" />
  48. <label htmlFor="password">Password</label>
  49. </div>
  50. </div>
  51. <div className="row">
  52. <button
  53. className="btn waves-effect waves-light"
  54. type="button"
  55. name="action"
  56. onClick={this.loginClicked}
  57. >
  58. Submit
  59. </button>
  60. <a href="/subcontractor" className="register">
  61. Register here
  62. </a>
  63. </div>
  64. <div className="row"><div style={{textAlign: "center", color:"red"}}>{this.props.login.message}</div></div>
  65.  
  66. </form>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. );
  72. }
  73. }
  74.  
  75.  
  76. function mapStateToProps({login}){
  77. return { login } ;
  78. }
  79.  
  80. export default connect(mapStateToProps, actions)(Login);
  81.  
  82. import axios from "axios";
  83. import { FETCH_USER, LOGIN_USER, LOGIN_FAILED } from "./types";
  84.  
  85.  
  86. export const fetchUser = () => async dispatch => {
  87. const res = await axios.get("/api/Account/GetUser");
  88. dispatch({ type: FETCH_USER, payload: res.data });
  89. };
  90.  
  91. export const loginUser = (username, password) => async dispatch => {
  92. try {
  93. const res = await axios.post("/api/Account/Login", {username: username, password: password, persistant: false });
  94. const message = res.data ? "" : "Incorrect username or password";
  95. dispatch({ type: LOGIN_USER, payload: { success: res.data, message: message } });
  96. }
  97. catch(ex){
  98. console.log("Login Failed:", ex);
  99. dispatch({ type: LOGIN_FAILED, payload: { success: false, message: "Unable to connect to authentication server" } });
  100. }
  101. }
  102.  
  103. import { LOGIN_USER, LOGIN_FAILED } from "../actions/types";
  104.  
  105. export default function(state = null, action) {
  106. console.log(action);
  107. switch (action.type) {
  108. case LOGIN_USER:
  109. return { success: action.payload.success, message: action.payload.message };
  110. break;
  111. case LOGIN_FAILED:
  112. return { success: false, message: action.payload.message };
  113. break;
  114. default:
  115. return { success: false, message: ""};
  116. }
  117. }
Add Comment
Please, Sign In to add comment