diff --git a/src/containers/App.tsx b/src/containers/App.tsx index b98120a3..bcd0a5ba 100644 --- a/src/containers/App.tsx +++ b/src/containers/App.tsx @@ -44,7 +44,6 @@ const Component: FC = ({ modal: { is_shown } }) => { - diff --git a/src/containers/dialogs/RestorePasswordDialog/index.tsx b/src/containers/dialogs/RestorePasswordDialog/index.tsx index b010cc84..370272bf 100644 --- a/src/containers/dialogs/RestorePasswordDialog/index.tsx +++ b/src/containers/dialogs/RestorePasswordDialog/index.tsx @@ -18,7 +18,7 @@ const mapStateToProps = state => ({ restore: selectAuthRestore(state), }); -const mapDispatchToProps = pick(['authRequestRestoreCode', 'authSetRestore'], AUTH_ACTIONS); +const mapDispatchToProps = pick(['authRestorePassword', 'authSetRestore'], AUTH_ACTIONS); type IProps = IDialogProps & ReturnType & typeof mapDispatchToProps & {}; @@ -26,7 +26,7 @@ const RestorePasswordDialogUnconnected: FC = ({ restore: { error, is_loading, is_succesfull, user }, authSetRestore, onRequestClose, - authRequestRestoreCode, + authRestorePassword, }) => { const [password, setPassword] = useState(''); const [password_again, setPasswordAgain] = useState(''); @@ -41,11 +41,10 @@ const RestorePasswordDialogUnconnected: FC = ({ event.preventDefault(); if (doesnt_match) return; - // if (!field) return; - // - // authRequestRestoreCode(field); + + authRestorePassword(password); }, - [doesnt_match] + [doesnt_match, authRestorePassword] ); useEffect(() => { @@ -69,7 +68,8 @@ const RestorePasswordDialogUnconnected: FC = ({ -
Отлично, добро пожаловать домой!
+
Пароль обновлен
+
Добро пожаловать домой, ~{user.username}!
diff --git a/src/redux/auth/actions.ts b/src/redux/auth/actions.ts index 89bac4bc..3f1f501c 100644 --- a/src/redux/auth/actions.ts +++ b/src/redux/auth/actions.ts @@ -91,3 +91,8 @@ export const authShowRestoreModal = (code: string) => ({ type: AUTH_USER_ACTIONS.SHOW_RESTORE_MODAL, code, }); + +export const authRestorePassword = (password: string) => ({ + type: AUTH_USER_ACTIONS.RESTORE_PASSWORD, + password, +}); diff --git a/src/redux/auth/api.ts b/src/redux/auth/api.ts index f94b05e4..a4e76dbb 100644 --- a/src/redux/auth/api.ts +++ b/src/redux/auth/api.ts @@ -78,3 +78,9 @@ export const apiCheckRestoreCode = ({ code }): Promise> => .get(API.USER.REQUEST_CODE(code)) .then(resultMiddleware) .catch(errorMiddleware); + +export const apiRestoreCode = ({ code, password }): Promise> => + api + .post(API.USER.REQUEST_CODE(code), { password }) + .then(resultMiddleware) + .catch(errorMiddleware); diff --git a/src/redux/auth/constants.ts b/src/redux/auth/constants.ts index 8d821cfb..bd320a82 100644 --- a/src/redux/auth/constants.ts +++ b/src/redux/auth/constants.ts @@ -22,6 +22,7 @@ export const AUTH_USER_ACTIONS = { SET_RESTORE: 'SET_RESTORE', REQUEST_RESTORE_CODE: 'REQUEST_RESTORE_CODE', SHOW_RESTORE_MODAL: 'SHOW_RESTORE_MODAL', + RESTORE_PASSWORD: 'RESTORE_PASSWORD', }; export const USER_ERRORS = { diff --git a/src/redux/auth/sagas.ts b/src/redux/auth/sagas.ts index d5674a47..9df41cb8 100644 --- a/src/redux/auth/sagas.ts +++ b/src/redux/auth/sagas.ts @@ -17,6 +17,7 @@ import { authShowRestoreModal, authSetRestore, authRequestRestoreCode, + authRestorePassword, } from '~/redux/auth/actions'; import { apiUserLogin, @@ -28,9 +29,16 @@ import { apiUpdateUser, apiRequestRestoreCode, apiCheckRestoreCode, + apiRestoreCode, } from '~/redux/auth/api'; import { modalSetShown, modalShowDialog } from '~/redux/modal/actions'; -import { selectToken, selectAuthProfile, selectAuthUser, selectAuthUpdates } from './selectors'; +import { + selectToken, + selectAuthProfile, + selectAuthUser, + selectAuthUpdates, + selectAuthRestore, +} from './selectors'; import { IResultWithStatus, INotification, IMessageNotification } from '../types'; import { IUser, IAuthState } from './types'; import { REHYDRATE, RehydrateAction } from 'redux-persist'; @@ -293,12 +301,7 @@ function* requestRestoreCode({ field }: ReturnType) { if (!code && !code.length) { - return yield put( - authSetRestore({ - error: ERRORS.CODE_IS_INVALID, - is_loading: false, - }) - ); + return yield put(authSetRestore({ error: ERRORS.CODE_IS_INVALID, is_loading: false })); } yield put(authSetRestore({ user: null, is_loading: true })); @@ -306,13 +309,41 @@ function* showRestoreModal({ code }: ReturnType) { const { error, data } = yield call(apiCheckRestoreCode, { code }); if (data.error || error || !data.user) { + yield put( + authSetRestore({ is_loading: false, error: data.error || error || ERRORS.CODE_IS_INVALID }) + ); + + return yield put(modalShowDialog(DIALOGS.RESTORE_PASSWORD)); + } + + yield put(authSetRestore({ user: data.user, code, is_loading: false })); + yield put(modalShowDialog(DIALOGS.RESTORE_PASSWORD)); +} + +function* restorePassword({ password }: ReturnType) { + if (!password) return; + + yield put(authSetRestore({ is_loading: true })); + const { code } = yield select(selectAuthRestore); + + if (!code) { + return yield put(authSetRestore({ error: ERRORS.CODE_IS_INVALID, is_loading: false })); + } + + const { error, data } = yield call(apiRestoreCode, { code, password }); + + if (data.error || error || !data.user || !data.token) { return yield put( authSetRestore({ is_loading: false, error: data.error || error || ERRORS.CODE_IS_INVALID }) ); } - yield put(modalShowDialog(DIALOGS.RESTORE_PASSWORD)); - yield put(authSetRestore({ user: data.user, is_loading: false })); + yield put(authSetToken(data.token)); + yield put(authSetUser(data.user)); + + yield put(authSetRestore({ is_loading: false, is_succesfull: true, error: null })); + + yield call(refreshUser); } function* authSaga() { @@ -327,8 +358,9 @@ function* authSaga() { yield takeLatest(AUTH_USER_ACTIONS.SEND_MESSAGE, sendMessage); yield takeLatest(AUTH_USER_ACTIONS.SET_LAST_SEEN_MESSAGES, setLastSeenMessages); yield takeLatest(AUTH_USER_ACTIONS.PATCH_USER, patchUser); - yield takeLatest(AUTH_USER_ACTIONS.SHOW_RESTORE_MODAL, showRestoreModal); yield takeLatest(AUTH_USER_ACTIONS.REQUEST_RESTORE_CODE, requestRestoreCode); + yield takeLatest(AUTH_USER_ACTIONS.SHOW_RESTORE_MODAL, showRestoreModal); + yield takeLatest(AUTH_USER_ACTIONS.RESTORE_PASSWORD, restorePassword); } export default authSaga;