Created
December 22, 2016 15:53
-
-
Save rodrigooler/6c5a2bc582a7bcbf5a66b7fd04e3a926 to your computer and use it in GitHub Desktop.
login.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { browserHistory } from 'react-router' | |
| import { callApi } from '../middleware/api' | |
| export const LOGIN_REQUEST = 'LOGIN_REQUEST' | |
| export const LOGIN_SUCCESS = 'LOGIN_SUCCESS' | |
| export const LOGIN_FAILURE = 'LOGIN_FAILURE' | |
| function requestLogin(creds) { | |
| return { | |
| type: LOGIN_REQUEST, | |
| isFetching: true, | |
| isAuthenticated: false | |
| } | |
| } | |
| function receiveLogin(user) { | |
| return { | |
| type: LOGIN_SUCCESS, | |
| isFetching: false, | |
| isAuthenticated: true, | |
| user | |
| } | |
| } | |
| function loginError(message) { | |
| return { | |
| type: LOGIN_FAILURE, | |
| isFetching: false, | |
| isAuthenticated: false, | |
| message | |
| } | |
| } | |
| export function loginUser(creds) { | |
| return dispatch => { | |
| dispatch(requestLogin(creds)) | |
| let result = callApi('usuarios/login/', creds, 'POST', false).then(x => console.log(x)) | |
| console.log(result); | |
| localStorage.setItem('id_token', result.token) | |
| localStorage.setItem('user', JSON.stringify(result)) | |
| dispatch(receiveLogin(result)) | |
| browserHistory.push('/pages/gerenciamento-armazem') | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment