-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathsaga.js
More file actions
65 lines (61 loc) · 2.2 KB
/
Copy pathsaga.js
File metadata and controls
65 lines (61 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { createAction } from '@reduxjs/toolkit';
import { call, put, takeLeading } from 'redux-saga/effects';
import { loginUrl as url } from '../../apiConfig';
import { loginSuccess } from '../userFarmSlice';
import history from '../../history';
import i18n from '../../locales/i18n';
import { axios } from '../saga';
import { ENTER_PASSWORD_PAGE } from '../CustomSignUp/constants';
import { enqueueErrorSnackbar } from '../Snackbar/snackbarSlice';
import { getLanguageFromLocalStorage } from '../../util/getLanguageFromLocalStorage';
import { setCustomSignUpErrorKey } from '../customSignUpSlice';
import { inlineErrors } from '../CustomSignUp/constants';
const loginUrl = () => `${url}/google`;
export const loginWithGoogle = createAction(`loginWithGoogleSaga`);
export function* loginWithGoogleSaga({ payload: google_id_token }) {
try {
const header = {
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + google_id_token,
},
};
const result = yield call(
axios.post,
loginUrl(),
{ language_preference: getLanguageFromLocalStorage() },
header,
);
const { id_token, user, isSignUp, isInvited } = result.data;
localStorage.setItem('id_token', id_token);
localStorage.setItem('litefarm_lang', user.language_preference);
const requestedLang = getLanguageFromLocalStorage();
const targetLang = i18n.options.supportedLngs?.includes(requestedLang) ? requestedLang : 'en';
if (i18n.language !== targetLang) {
i18n.changeLanguage(targetLang);
}
if (isInvited) {
yield put(setCustomSignUpErrorKey({ key: inlineErrors.invited }));
} else if (id_token === '') {
// The user has an account with a password
history.push(
{
pathname: '/',
},
{ component: ENTER_PASSWORD_PAGE, user },
);
} else {
yield put(loginSuccess(user));
if (isSignUp) {
history.push('/welcome');
} else {
history.push('/farm_selection');
}
}
} catch (e) {
yield put(enqueueErrorSnackbar(i18n.t('message:LOGIN.ERROR.LOGIN_FAIL')));
}
}
export default function* loginSaga() {
yield takeLeading(loginWithGoogle.type, loginWithGoogleSaga);
}