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:
parent
505242c099
commit
43968f14b2
6 changed files with 71 additions and 16 deletions
|
@ -18,6 +18,7 @@ export const API = {
|
||||||
GET_SOCIALS: '/oauth/',
|
GET_SOCIALS: '/oauth/',
|
||||||
DROP_SOCIAL: (provider, id) => `/oauth/${provider}/${id}`,
|
DROP_SOCIAL: (provider, id) => `/oauth/${provider}/${id}`,
|
||||||
ATTACH_SOCIAL: `/oauth/attach`,
|
ATTACH_SOCIAL: `/oauth/attach`,
|
||||||
|
LOGIN_WITH_SOCIAL: `/oauth/login`,
|
||||||
// TODO: REMOVE
|
// TODO: REMOVE
|
||||||
VKONTAKTE_LOGIN: `${process.env.API_HOST}/oauth/vkontakte/redirect/login`,
|
VKONTAKTE_LOGIN: `${process.env.API_HOST}/oauth/vkontakte/redirect/login`,
|
||||||
},
|
},
|
||||||
|
|
|
@ -16,13 +16,13 @@ import * as ACTIONS from '~/redux/auth/actions';
|
||||||
import * as MODAL_ACTIONS from '~/redux/modal/actions';
|
import * as MODAL_ACTIONS from '~/redux/modal/actions';
|
||||||
import { ISocialProvider } from '~/redux/auth/types';
|
import { ISocialProvider } from '~/redux/auth/types';
|
||||||
import { Grid } from '~/components/containers/Grid';
|
import { Grid } from '~/components/containers/Grid';
|
||||||
import { GodRays } from '~/components/main/GodRays';
|
|
||||||
|
|
||||||
const mapStateToProps = selectAuthLogin;
|
const mapStateToProps = selectAuthLogin;
|
||||||
|
|
||||||
const mapDispatchToProps = {
|
const mapDispatchToProps = {
|
||||||
userSendLoginRequest: ACTIONS.userSendLoginRequest,
|
userSendLoginRequest: ACTIONS.userSendLoginRequest,
|
||||||
userSetLoginError: ACTIONS.userSetLoginError,
|
userSetLoginError: ACTIONS.userSetLoginError,
|
||||||
|
authLoginWithSocial: ACTIONS.authLoginWithSocial,
|
||||||
modalShowDialog: MODAL_ACTIONS.modalShowDialog,
|
modalShowDialog: MODAL_ACTIONS.modalShowDialog,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ const LoginDialogUnconnected: FC<IProps> = ({
|
||||||
error,
|
error,
|
||||||
userSendLoginRequest,
|
userSendLoginRequest,
|
||||||
userSetLoginError,
|
userSetLoginError,
|
||||||
|
authLoginWithSocial,
|
||||||
modalShowDialog,
|
modalShowDialog,
|
||||||
}) => {
|
}) => {
|
||||||
const [username, setUserName] = useState('');
|
const [username, setUserName] = useState('');
|
||||||
|
@ -62,20 +63,21 @@ const LoginDialogUnconnected: FC<IProps> = ({
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
const onMessage = useCallback((event: MessageEvent) => {
|
const onMessage = useCallback(
|
||||||
if (!event?.data?.type) return;
|
(event: MessageEvent) => {
|
||||||
|
if (!event?.data?.type) return;
|
||||||
|
|
||||||
switch (event?.data?.type) {
|
switch (event?.data?.type) {
|
||||||
case 'oauth_processed':
|
case 'oauth_processed':
|
||||||
// return authAttachSocial(event?.data?.payload?.token);
|
return authLoginWithSocial(event?.data?.payload?.token);
|
||||||
return console.log('oauth_processed', event?.data?.payload?.token);
|
case 'oauth_error':
|
||||||
case 'oauth_error':
|
return userSetLoginError(event?.data?.payload?.error);
|
||||||
// return authSetSocials({ error: event?.data?.payload?.error || '' });
|
default:
|
||||||
return console.log('oauth_error', { error: event?.data?.payload?.error || '' });
|
return;
|
||||||
default:
|
}
|
||||||
return;
|
},
|
||||||
}
|
[authLoginWithSocial, userSetLoginError]
|
||||||
}, []);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (error) userSetLoginError(null);
|
if (error) userSetLoginError(null);
|
||||||
|
@ -137,7 +139,7 @@ const LoginDialogUnconnected: FC<IProps> = ({
|
||||||
onClick={onRestoreRequest}
|
onClick={onRestoreRequest}
|
||||||
className={styles.forgot_button}
|
className={styles.forgot_button}
|
||||||
>
|
>
|
||||||
Вспомнить пароль
|
Вспомнить парольF
|
||||||
</Button>
|
</Button>
|
||||||
</Group>
|
</Group>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -126,3 +126,10 @@ export const authAttachSocial = (token: string) => ({
|
||||||
type: AUTH_USER_ACTIONS.ATTACH_SOCIAL,
|
type: AUTH_USER_ACTIONS.ATTACH_SOCIAL,
|
||||||
token,
|
token,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const authLoginWithSocial = (token: string, username?: string, password?: string) => ({
|
||||||
|
type: AUTH_USER_ACTIONS.LOGIN_WITH_SOCIAL,
|
||||||
|
token,
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
});
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { api, configWithToken, errorMiddleware, resultMiddleware } from '~/utils
|
||||||
import { API } from '~/constants/api';
|
import { API } from '~/constants/api';
|
||||||
import { IMessage, INotification, IResultWithStatus } from '~/redux/types';
|
import { IMessage, INotification, IResultWithStatus } from '~/redux/types';
|
||||||
import { userLoginTransform } from '~/redux/auth/transforms';
|
import { userLoginTransform } from '~/redux/auth/transforms';
|
||||||
import { ISocialAccount, IUser } from './types';
|
import { ISocialAccount, IToken, IUser } from './types';
|
||||||
|
|
||||||
export const apiUserLogin = ({
|
export const apiUserLogin = ({
|
||||||
username,
|
username,
|
||||||
|
@ -129,3 +129,20 @@ export const apiAttachSocial = ({
|
||||||
.post(API.USER.ATTACH_SOCIAL, { token }, configWithToken(access))
|
.post(API.USER.ATTACH_SOCIAL, { token }, configWithToken(access))
|
||||||
.then(resultMiddleware)
|
.then(resultMiddleware)
|
||||||
.catch(errorMiddleware);
|
.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);
|
||||||
|
|
|
@ -30,6 +30,7 @@ export const AUTH_USER_ACTIONS = {
|
||||||
ADD_SOCIAL: 'ADD_SOCIAL',
|
ADD_SOCIAL: 'ADD_SOCIAL',
|
||||||
SET_SOCIALS: 'SET_SOCIALS',
|
SET_SOCIALS: 'SET_SOCIALS',
|
||||||
ATTACH_SOCIAL: 'ATTACH_SOCIAL',
|
ATTACH_SOCIAL: 'ATTACH_SOCIAL',
|
||||||
|
LOGIN_WITH_SOCIAL: 'LOGIN_WITH_SOCIAL',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const USER_ERRORS = {
|
export const USER_ERRORS = {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import {
|
||||||
authGetMessages,
|
authGetMessages,
|
||||||
authLoadProfile,
|
authLoadProfile,
|
||||||
authLoggedIn,
|
authLoggedIn,
|
||||||
|
authLoginWithSocial,
|
||||||
authOpenProfile,
|
authOpenProfile,
|
||||||
authPatchUser,
|
authPatchUser,
|
||||||
authRequestRestoreCode,
|
authRequestRestoreCode,
|
||||||
|
@ -33,6 +34,7 @@ import {
|
||||||
apiCheckRestoreCode,
|
apiCheckRestoreCode,
|
||||||
apiDropSocial,
|
apiDropSocial,
|
||||||
apiGetSocials,
|
apiGetSocials,
|
||||||
|
apiLoginWithSocial,
|
||||||
apiRequestRestoreCode,
|
apiRequestRestoreCode,
|
||||||
apiRestoreCode,
|
apiRestoreCode,
|
||||||
apiUpdateUser,
|
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() {
|
function* authSaga() {
|
||||||
yield takeEvery(REHYDRATE, checkUserSaga);
|
yield takeEvery(REHYDRATE, checkUserSaga);
|
||||||
yield takeLatest([REHYDRATE, AUTH_USER_ACTIONS.LOGGED_IN], startPollingSaga);
|
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.GET_SOCIALS, getSocials);
|
||||||
yield takeLatest(AUTH_USER_ACTIONS.DROP_SOCIAL, dropSocial);
|
yield takeLatest(AUTH_USER_ACTIONS.DROP_SOCIAL, dropSocial);
|
||||||
yield takeLatest(AUTH_USER_ACTIONS.ATTACH_SOCIAL, attachSocial);
|
yield takeLatest(AUTH_USER_ACTIONS.ATTACH_SOCIAL, attachSocial);
|
||||||
|
yield takeLatest(AUTH_USER_ACTIONS.LOGIN_WITH_SOCIAL, loginWithSocial);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default authSaga;
|
export default authSaga;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue