Created
April 5, 2019 20:25
-
-
Save CarlosLevir/3af767b23dd78698ec9d7f83a65f94a6 to your computer and use it in GitHub Desktop.
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 { Base64 } from 'js-base64'; | |
import { call, put, takeLatest } from 'redux-saga/effects'; | |
import * as actions from '../actions/authActions'; | |
import * as types from '../actions/types'; | |
import * as API from '../api/authApi'; | |
import DeviceStorage from '../utils/deviceStorage'; | |
import * as NavigationService from '../utils/navigationService'; | |
import * as routes from '../utils/routeTypes'; | |
export function* login({ payload }) { | |
try { | |
const encoded = Base64.encode(`${payload.username}:${payload.password}`); | |
const headers = { | |
Authorization: `Basic ${encoded}` | |
}; | |
const response = yield call(API.requestLogin, headers); | |
yield call(DeviceStorage.set, 'AUTH', headers.Authorization); | |
yield put(actions.loginSuccess(response)); | |
NavigationService.navigate(routes.APP); | |
} catch (error) { | |
yield put(actions.loginError(error.message)); | |
} | |
} | |
function* authSaga() { | |
yield takeLatest(types.LOGIN, login); | |
} | |
export default authSaga; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment