Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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