1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

logging user in by social

This commit is contained in:
Fedor Katurov 2020-07-29 18:49:29 +07:00
parent 505242c099
commit 43968f14b2
6 changed files with 71 additions and 16 deletions

View file

@ -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`,
},

View file

@ -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<IProps> = ({
error,
userSendLoginRequest,
userSetLoginError,
authLoginWithSocial,
modalShowDialog,
}) => {
const [username, setUserName] = useState('');
@ -62,20 +63,21 @@ const LoginDialogUnconnected: FC<IProps> = ({
[]
);
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<IProps> = ({
onClick={onRestoreRequest}
className={styles.forgot_button}
>
Вспомнить пароль
Вспомнить парольF
</Button>
</Group>
</div>

View file

@ -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,
});

View file

@ -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<IResultWithStatus<{
token: string;
needs_login: boolean;
}>> =>
api
.post(API.USER.LOGIN_WITH_SOCIAL, { token, username, password })
.then(resultMiddleware)
.catch(errorMiddleware);

View file

@ -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 = {

View file

@ -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<typeof authAttachSocial>) {
}
}
function* loginWithSocial({ token }: ReturnType<typeof authLoginWithSocial>) {
try {
yield put(userSetLoginError(''));
const {
data,
error,
}: Unwrap<ReturnType<typeof apiLoginWithSocial>> = 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;