Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. export class LoginForm extends React.Component {
  2.  
  3. componentWillReceiveProps(newProps){
  4.  
  5. const { location, isAuthenticated, history } = newProps;
  6. if(isAuthenticated){
  7.  
  8. if(location.state && location.state.referrer){
  9. history.push(location.state.referrer.pathname);
  10. }else{
  11. history.replace('/app');
  12. }
  13. }
  14. }
  15.  
  16. render() {
  17. let usernameInput, passwordInput;
  18. const { showErrorMessage, errorMessage, onLogin } = this.props;
  19. return (
  20.  
  21.  
  22. <div className='sme-login-center-view-container'>
  23. <HeaderComponent />
  24.  
  25. <Col lg={4} lgOffset={4} sm={12} xs={12}>
  26. <Col lg={10} lgOffset={2} sm={4} smOffset={4}>
  27. <Form className="sme-login-box" onSubmit={(e)=> {
  28. e.preventDefault();
  29. let creds = {username: usernameInput.value, password: passwordInput.value};
  30. onLogin(creds);
  31. }
  32. }>
  33.  
  34. <span className='text-center sme-login-title-text'><h4>User Login</h4></span>
  35.  
  36. <FormGroup >
  37. {errorMessage ? (<Alert bsStyle="danger"><strong>Error!</strong> {errorMessage}</Alert>) : null }
  38. </FormGroup>
  39.  
  40. <FormGroup controlId="formHorizontalUsername">
  41. <FormControl type="username" placeholder="Username" bsStyle="form-rounded"
  42. inputRef={(ref) => {usernameInput = ref}}
  43. />
  44. <FormControl.Feedback>
  45. <span className="fa fa-user-o sme-login-input-feedback-span"></span>
  46. </FormControl.Feedback>
  47. </FormGroup>
  48.  
  49. <FormGroup controlId="formHorizontalPassword">
  50. <FormControl type="password" placeholder="Password"
  51. inputRef={(ref) => {passwordInput = ref}}/>
  52. <FormControl.Feedback>
  53. <span className="fa fa-lock sme-login-input-feedback-span"></span>
  54. </FormControl.Feedback>
  55. </FormGroup>
  56.  
  57. <FormGroup>
  58. <Button type="submit" block >Login</Button>
  59. </FormGroup>
  60.  
  61. </Form>
  62. </Col>
  63. </Col>
  64. </div>
  65.  
  66. );
  67. }
  68. }
  69.  
  70. const mapStateToProps = state => {
  71. return state.authenticationReducer.login
  72. }
  73.  
  74. export const Login = withRouter(connect( mapStateToProps,{ onLogin: loginUser })(LoginForm))
  75.  
  76. export function requestLogin(creds) {
  77. return {
  78. type: LOGIN_REQUEST,
  79. isFetching: true,
  80. isAuthenticated: false,
  81. creds
  82. }
  83. }
  84. export function receiveLogin() {
  85. return {
  86. type: LOGIN_SUCCESS,
  87. isFetching: false,
  88. isAuthenticated: true
  89. }
  90. }
  91. export function loginError(message) {
  92. return {
  93. type: LOGIN_FAILURE,
  94. isFetching: false,
  95. isAuthenticated: false,
  96. errorMessage: message
  97. }
  98. }
  99. export function loginUser(creds) {
  100. return dispatch => {
  101. dispatch(requestLogin(creds))
  102. return axios.post(url, data)
  103. .then(response => {
  104. if (!response.status === 200) {
  105. dispatch(loginError(response.statusText))
  106. } else {
  107. dispatch(receiveLogin())
  108. }
  109. }
  110. ).catch(function (error) {
  111. dispatch(loginError(error.response.statusText))
  112. }
  113. ) }
  114. }
  115.  
  116. export function login(state = {
  117. isFetching: false,
  118. isAuthenticated: false
  119. }, action) {
  120.  
  121. switch (action.type) {
  122. case LOGIN_REQUEST:
  123. case LOGIN_SUCCESS:
  124. case LOGIN_FAILURE:
  125. return Object.assign({}, state, action)
  126. default:
  127. return state
  128. }
  129. }
  130.  
  131. class AuthorizedRouteComponent extends React.Component {
  132.  
  133. componentWillMount() {
  134. this.props.getUser();
  135. }
  136. render() {
  137. const { component: Component, pending, logged, location, ...rest } = this.props;
  138. return (
  139. <Route {...rest} render={props => {
  140. if (pending) return <div>Loading...</div>
  141. return logged
  142. ? <Component {...this.props} />
  143. :<Redirect to={{
  144. pathname: '/auth/login',
  145. state: { referrer: location }
  146. }}/>
  147. }} />
  148. )
  149. }
  150. }
  151.  
  152. const mapStateToProps = state => {
  153. return state.authenticationReducer.loggedUser
  154. }
  155.  
  156. const AuthorizedRoute = connect(mapStateToProps, { getUser: getLoggedUser })(AuthorizedRouteComponent);
  157.  
  158. export default AuthorizedRoute
  159.  
  160. export function requestFetch() {
  161. return {
  162. type: FETCH_REQUEST,
  163. pending: true,
  164. logged: false
  165. }
  166. }
  167. export function receiveFetch(userData) {
  168. return {
  169. type: FETCH_SUCCESS,
  170. pending: false,
  171. logged: true,
  172. userData
  173. }
  174. }
  175. export function fetchError(message) {
  176. return {
  177. type: FETCH_FAILURE,
  178. pending: false,
  179. logged: false,
  180. errorMessage:message
  181. }
  182. }
  183. export function getLoggedUser() {
  184. return dispatch => {
  185. dispatch(requestFetch())
  186. return axios.get('/o3/rest/user/userdetails')
  187. .then(response => {
  188.  
  189. if (!response.status === 200) {
  190. dispatch(fetchError(response.statusText))
  191. } else {
  192. dispatch(receiveFetch(response.data))
  193. }
  194. }
  195. ).catch(function (error) {
  196. dispatch(fetchError(error.response.statusText))
  197. }
  198. )
  199. }
  200. }
  201.  
  202. export function loggedUser(state = initialState, action) {
  203. switch (action.type) {
  204. case FETCH_REQUEST:
  205. case FETCH_SUCCESS:
  206. case FETCH_FAILURE:
  207. let obj = Object.assign({}, state, action);
  208. return obj;
  209. default:
  210. return state
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement