You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.3 KiB
139 lines
4.3 KiB
import { effects } from "redux-saga"; |
|
import { |
|
isSendingAuthRequest, |
|
setAuthRequestError, |
|
setAuthRequestSuccess, |
|
setEmailVerificationError, |
|
setEmailVerificationSuccess, |
|
clearAuthRequestError, |
|
clearEmailVerificationError, |
|
clearAuthRequestSuccess, |
|
clearEmailVerificationSuccess, |
|
setSelfUserToken, |
|
setFormEmail, |
|
setFormPassword, |
|
setFormPasswordConfirmation, |
|
setFormEmailVerification, |
|
setFormOldPassword, |
|
} from "../actions/auth/reducer.actions"; |
|
import { |
|
registerUser, |
|
verifyEmail, |
|
loginUser, |
|
logoutUser, |
|
changePassword |
|
} from "../api/auth.api"; |
|
|
|
function* registerUserCall(postBody) { |
|
yield effects.put(isSendingAuthRequest(true)); |
|
const { email, password1, password2 } = postBody; |
|
try { |
|
return yield effects.call(registerUser, email, password1, password2); |
|
} catch (exception) { |
|
yield effects.put(setAuthRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingAuthRequest(false)); |
|
} |
|
} |
|
|
|
function* verifyEmailCall(postBody) { |
|
yield effects.put(isSendingAuthRequest(true)); |
|
const { emailKey } = postBody; |
|
try { |
|
return yield effects.call(verifyEmail, emailKey); |
|
} catch (exception) { |
|
yield effects.put(setEmailVerificationError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingAuthRequest(false)); |
|
} |
|
} |
|
|
|
function* loginUserCall(postBody) { |
|
yield effects.put(isSendingAuthRequest(true)); |
|
const { email, password } = postBody; |
|
try { |
|
return yield effects.call(loginUser, email, password); |
|
} catch (exception) { |
|
yield effects.put(setAuthRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingAuthRequest(false)); |
|
} |
|
} |
|
|
|
function* logoutUserCall() { |
|
yield effects.call(logoutUser); |
|
} |
|
|
|
function* changePasswordCall(postBody) { |
|
yield effects.put(isSendingAuthRequest(true)); |
|
const { new_password1, new_password2, old_password } = postBody; |
|
try { |
|
return yield effects.call(changePassword, new_password1, new_password2, old_password); |
|
} catch (exception) { |
|
yield effects.put(setAuthRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingAuthRequest(false)); |
|
} |
|
} |
|
|
|
export function* registerUserFlow(request) { |
|
yield effects.put(clearAuthRequestSuccess()); |
|
yield effects.put(clearAuthRequestError()); |
|
const wasSuccessful = yield effects.call(registerUserCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(setAuthRequestSuccess(wasSuccessful)); |
|
yield effects.put(clearAuthRequestError()); |
|
yield effects.put(setFormEmail("")); |
|
yield effects.put(setFormPassword("")); |
|
yield effects.put(setFormPasswordConfirmation("")); |
|
} |
|
} |
|
|
|
export function* verifyEmailFlow(request) { |
|
yield effects.put(clearEmailVerificationSuccess()); |
|
yield effects.put(clearEmailVerificationError()); |
|
const wasSuccessful = yield effects.call(verifyEmailCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(setEmailVerificationSuccess(wasSuccessful)); |
|
yield effects.put(clearEmailVerificationError()); |
|
yield effects.put(setFormEmailVerification("")); |
|
} |
|
} |
|
|
|
export function* loginUserFlow(request) { |
|
yield effects.put(clearAuthRequestSuccess()); |
|
yield effects.put(clearAuthRequestError()); |
|
const wasSuccessful = yield effects.call(loginUserCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(setSelfUserToken(wasSuccessful.key)); |
|
yield effects.put(setAuthRequestSuccess(wasSuccessful)); |
|
yield effects.put(clearAuthRequestError()); |
|
yield effects.put(setFormEmail("")); |
|
yield effects.put(setFormPassword("")); |
|
yield effects.put(setFormPasswordConfirmation("")); |
|
} |
|
} |
|
|
|
export function* logoutUserFlow(request) { |
|
yield effects.put(clearAuthRequestSuccess()); |
|
yield effects.put(clearAuthRequestError()); |
|
yield effects.call(logoutUserCall); |
|
yield effects.put(setSelfUserToken("")); |
|
} |
|
|
|
export function* changePasswordFlow(request) { |
|
yield effects.put(clearAuthRequestSuccess()); |
|
yield effects.put(clearAuthRequestError()); |
|
const wasSuccessful = yield effects.call(changePasswordCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(setAuthRequestSuccess(wasSuccessful)); |
|
yield effects.put(clearAuthRequestError()); |
|
yield effects.put(setFormOldPassword("")); |
|
yield effects.put(setFormPassword("")); |
|
yield effects.put(setFormPasswordConfirmation("")); |
|
} |
|
}
|
|
|