1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 21:06:42 +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

@ -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;