From 43968f14b2b9255d0f070dfd0de6b9ed3b7a396a Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Wed, 29 Jul 2020 18:49:29 +0700 Subject: [PATCH] logging user in by social --- src/constants/api.ts | 1 + src/containers/dialogs/LoginDialog/index.tsx | 32 +++++++++++--------- src/redux/auth/actions.ts | 7 +++++ src/redux/auth/api.ts | 19 +++++++++++- src/redux/auth/constants.ts | 1 + src/redux/auth/sagas.ts | 27 +++++++++++++++++ 6 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/constants/api.ts b/src/constants/api.ts index 47a8cf4d..66f7069b 100644 --- a/src/constants/api.ts +++ b/src/constants/api.ts @@ -18,6 +18,7 @@ export const API = { GET_SOCIALS: '/oauth/', DROP_SOCIAL: (provider, id) => `/oauth/${provider}/${id}`, ATTACH_SOCIAL: `/oauth/attach`, + LOGIN_WITH_SOCIAL: `/oauth/login`, // TODO: REMOVE VKONTAKTE_LOGIN: `${process.env.API_HOST}/oauth/vkontakte/redirect/login`, }, diff --git a/src/containers/dialogs/LoginDialog/index.tsx b/src/containers/dialogs/LoginDialog/index.tsx index 5a05dc04..1b2bcada 100644 --- a/src/containers/dialogs/LoginDialog/index.tsx +++ b/src/containers/dialogs/LoginDialog/index.tsx @@ -16,13 +16,13 @@ import * as ACTIONS from '~/redux/auth/actions'; import * as MODAL_ACTIONS from '~/redux/modal/actions'; import { ISocialProvider } from '~/redux/auth/types'; import { Grid } from '~/components/containers/Grid'; -import { GodRays } from '~/components/main/GodRays'; const mapStateToProps = selectAuthLogin; const mapDispatchToProps = { userSendLoginRequest: ACTIONS.userSendLoginRequest, userSetLoginError: ACTIONS.userSetLoginError, + authLoginWithSocial: ACTIONS.authLoginWithSocial, modalShowDialog: MODAL_ACTIONS.modalShowDialog, }; @@ -33,6 +33,7 @@ const LoginDialogUnconnected: FC = ({ error, userSendLoginRequest, userSetLoginError, + authLoginWithSocial, modalShowDialog, }) => { const [username, setUserName] = useState(''); @@ -62,20 +63,21 @@ const LoginDialogUnconnected: FC = ({ [] ); - const onMessage = useCallback((event: MessageEvent) => { - if (!event?.data?.type) return; + const onMessage = useCallback( + (event: MessageEvent) => { + if (!event?.data?.type) return; - switch (event?.data?.type) { - case 'oauth_processed': - // return authAttachSocial(event?.data?.payload?.token); - return console.log('oauth_processed', event?.data?.payload?.token); - case 'oauth_error': - // return authSetSocials({ error: event?.data?.payload?.error || '' }); - return console.log('oauth_error', { error: event?.data?.payload?.error || '' }); - default: - return; - } - }, []); + switch (event?.data?.type) { + case 'oauth_processed': + return authLoginWithSocial(event?.data?.payload?.token); + case 'oauth_error': + return userSetLoginError(event?.data?.payload?.error); + default: + return; + } + }, + [authLoginWithSocial, userSetLoginError] + ); useEffect(() => { if (error) userSetLoginError(null); @@ -137,7 +139,7 @@ const LoginDialogUnconnected: FC = ({ onClick={onRestoreRequest} className={styles.forgot_button} > - Вспомнить пароль + Вспомнить парольF diff --git a/src/redux/auth/actions.ts b/src/redux/auth/actions.ts index 0f32fd18..15e8eae4 100644 --- a/src/redux/auth/actions.ts +++ b/src/redux/auth/actions.ts @@ -126,3 +126,10 @@ export const authAttachSocial = (token: string) => ({ type: AUTH_USER_ACTIONS.ATTACH_SOCIAL, token, }); + +export const authLoginWithSocial = (token: string, username?: string, password?: string) => ({ + type: AUTH_USER_ACTIONS.LOGIN_WITH_SOCIAL, + token, + username, + password, +}); diff --git a/src/redux/auth/api.ts b/src/redux/auth/api.ts index 1e63b5da..eeb8340a 100644 --- a/src/redux/auth/api.ts +++ b/src/redux/auth/api.ts @@ -2,7 +2,7 @@ import { api, configWithToken, errorMiddleware, resultMiddleware } from '~/utils import { API } from '~/constants/api'; import { IMessage, INotification, IResultWithStatus } from '~/redux/types'; import { userLoginTransform } from '~/redux/auth/transforms'; -import { ISocialAccount, IUser } from './types'; +import { ISocialAccount, IToken, IUser } from './types'; export const apiUserLogin = ({ username, @@ -129,3 +129,20 @@ export const apiAttachSocial = ({ .post(API.USER.ATTACH_SOCIAL, { token }, configWithToken(access)) .then(resultMiddleware) .catch(errorMiddleware); + +export const apiLoginWithSocial = ({ + token, + username, + password, +}: { + token: string; + username?: string; + password?: string; +}): Promise> => + api + .post(API.USER.LOGIN_WITH_SOCIAL, { token, username, password }) + .then(resultMiddleware) + .catch(errorMiddleware); diff --git a/src/redux/auth/constants.ts b/src/redux/auth/constants.ts index 722fc34a..0905e379 100644 --- a/src/redux/auth/constants.ts +++ b/src/redux/auth/constants.ts @@ -30,6 +30,7 @@ export const AUTH_USER_ACTIONS = { ADD_SOCIAL: 'ADD_SOCIAL', SET_SOCIALS: 'SET_SOCIALS', ATTACH_SOCIAL: 'ATTACH_SOCIAL', + LOGIN_WITH_SOCIAL: 'LOGIN_WITH_SOCIAL', }; export const USER_ERRORS = { diff --git a/src/redux/auth/sagas.ts b/src/redux/auth/sagas.ts index bc987464..cb6b323b 100644 --- a/src/redux/auth/sagas.ts +++ b/src/redux/auth/sagas.ts @@ -6,6 +6,7 @@ import { authGetMessages, authLoadProfile, authLoggedIn, + authLoginWithSocial, authOpenProfile, authPatchUser, authRequestRestoreCode, @@ -33,6 +34,7 @@ import { apiCheckRestoreCode, apiDropSocial, apiGetSocials, + apiLoginWithSocial, apiRequestRestoreCode, apiRestoreCode, apiUpdateUser, @@ -447,6 +449,30 @@ function* attachSocial({ token }: ReturnType) { } } +function* loginWithSocial({ token }: ReturnType) { + try { + yield put(userSetLoginError('')); + + const { + data, + error, + }: Unwrap> = yield call(apiLoginWithSocial, { token }); + + if (error) { + throw new Error(error); + } + + if (data.token) { + yield put(authSetToken(data.token)); + yield put(modalSetShown(false)); + yield call(refreshUser); + return; + } + } catch (e) { + yield put(userSetLoginError(e.message)); + } +} + function* authSaga() { yield takeEvery(REHYDRATE, checkUserSaga); yield takeLatest([REHYDRATE, AUTH_USER_ACTIONS.LOGGED_IN], startPollingSaga); @@ -466,6 +492,7 @@ function* authSaga() { yield takeLatest(AUTH_USER_ACTIONS.GET_SOCIALS, getSocials); yield takeLatest(AUTH_USER_ACTIONS.DROP_SOCIAL, dropSocial); yield takeLatest(AUTH_USER_ACTIONS.ATTACH_SOCIAL, attachSocial); + yield takeLatest(AUTH_USER_ACTIONS.LOGIN_WITH_SOCIAL, loginWithSocial); } export default authSaga;