Skip to content

Instantly share code, notes, and snippets.

@CarlosLevir
Created April 5, 2019 20:25
Show Gist options
  • Save CarlosLevir/3af767b23dd78698ec9d7f83a65f94a6 to your computer and use it in GitHub Desktop.
Save CarlosLevir/3af767b23dd78698ec9d7f83a65f94a6 to your computer and use it in GitHub Desktop.
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