Skip to content

Instantly share code, notes, and snippets.

@zulucoda
Last active January 27, 2019 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zulucoda/4ee9873aaf1cd5926674a55ddc694c9c to your computer and use it in GitHub Desktop.
Save zulucoda/4ee9873aaf1cd5926674a55ddc694c9c to your computer and use it in GitHub Desktop.
After Refactor - user login proccess
/**
* Authorise user
* @param username
* @param password
*/
export function* authoriseUserSaga(username, password) {
try {
// call login Api endpoint with user username and password
const loginService = new LoginService();
const response = yield call([loginService, get, username, password]);
// set auth token in store
yield put(LoginActions.setAuthToken(response.token));
return true;
} catch (e) {
// not authorised
return false;
}
}
/**
* Get user settings
*/
export function* getUserSettingsSaga() {
try {
// call the settings API endpoint with authorisation token
const settingsService = new SettingsService();
const response = yield call([settingsService, get]);
// set the user settings in store
yield put(SettingsActions.onReceiveSettings(response));
} catch (e) {
// some api error log
yield put(
SettingsActions.setError({
message: 'Cannot retrieve User settings. please try again later.',
}),
);
}
}
/**
* After Refactor - user login proccess
*/
export function* userLoginSagaRefactor() {
// 1. get the username and password from state.
const { username, password } = yield select(getLoginFormState);
// 2. authorise the user
const isAuthorised = yield call(authoriseUserSaga, username, password);
if (isAuthorised) {
//3. remove the username and password from store
yield put(LoginActions.clearLogin());
//4. get and set user settings
yield call(getUserSettingsSaga);
} else {
// NOT Authorised - incorrect username or password
yield put(
LoginActions.setError({ message: 'incorrect username or password' }),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment