forked from cboard-org/cboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogin.actions.js
More file actions
204 lines (181 loc) · 5.72 KB
/
Copy pathLogin.actions.js
File metadata and controls
204 lines (181 loc) · 5.72 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import API from '../../../api';
import { LOGIN_SUCCESS, LOGOUT } from './Login.constants';
import {
changeVoice,
changePitch,
changeRate,
changeElevenLabsApiKey,
getVoices
} from '../../../providers/SpeechProvider/SpeechProvider.actions';
import {
disableTour,
setUnloggedUserLocation,
updateUnloggedUserLocation,
enableAllTours,
updateNavigationSettings
} from '../../App/App.actions';
import { getVoiceURI } from '../../../i18n';
import { isCordova, isElectron } from '../../../cordova-util';
import tts from '../../../providers/SpeechProvider/tts';
import { appInsights } from '../../../appInsights';
export function loginSuccess(payload) {
return dispatch => {
dispatch({
type: LOGIN_SUCCESS,
payload
});
if (payload.isFirstLogin) firstLoginActions(dispatch, payload);
if (isCordova() && !isElectron()) {
try {
window.FirebasePlugin.setUserId(payload.id);
} catch (err) {
console.error(err);
}
}
if (!isCordova() && typeof window?.gtag === 'function')
window.gtag('set', { user_id: payload.id });
try {
appInsights.setAuthenticatedUserContext(payload.id);
} catch (err) {
console.error(err);
}
};
}
async function firstLoginActions(dispatch, payload) {
try {
await API.updateUser({ ...payload, isFirstLogin: false });
} catch (err) {
console.error(err);
}
dispatch(enableAllTours());
}
export function logout() {
if (isCordova() && !isElectron())
try {
window.FirebasePlugin.setUserId(undefined);
} catch (err) {
console.error(err);
}
if (!isCordova() && typeof window?.gtag === 'function') {
window.gtag('set', { user_id: null });
}
try {
appInsights.clearAuthenticatedUserContext();
} catch (err) {
console.error(err);
}
return async dispatch => {
dispatch(updateNavigationSettings({ improvePhraseActive: false }));
dispatch(setUnloggedUserLocation(null));
dispatch(updateUnloggedUserLocation());
dispatch(logoutSuccess());
};
}
function logoutSuccess() {
return {
type: LOGOUT
};
}
export function login({ email, password, activatedData }, type = 'local') {
const setAVoice = async ({ loginData, dispatch, getState }) => {
const elevenLabsApiKey = loginData?.settings?.speech?.elevenLabsApiKey;
if (elevenLabsApiKey) {
dispatch(changeElevenLabsApiKey(elevenLabsApiKey));
tts.initElevenLabsInstance(elevenLabsApiKey);
await dispatch(getVoices());
}
const {
language: { lang: appLang },
speech: {
voices,
options: { lang: deviceVoiceLang, voiceURI: deviceVoiceUri }
}
} = getState(); //ATENTION speech options on DB is under Speech directly. on state is under options
const emptyVoiceString = 'empty voices';
const appLanguageCode = appLang?.substring(0, 2);
const deviceVoiceLanguageCode = deviceVoiceLang?.substring(0, 2);
if (voices.length) {
const uris = voices.map(v => {
return v.voiceURI;
});
if (loginData.settings?.speech) {
const userVoiceUri = loginData.settings.speech.voiceURI; //ATENTION speech options on DB is under Speech directly. on state is under options
const userVoiceLanguage = voices.filter(
voice => voice.voiceURI === userVoiceUri
)[0]?.lang;
const userVoiceLanguageCode = userVoiceLanguage?.substring(0, 2);
if (
userVoiceUri &&
appLanguageCode === userVoiceLanguageCode &&
uris.includes(userVoiceUri)
) {
if (userVoiceUri !== deviceVoiceUri) {
dispatch(changeVoice(userVoiceUri, userVoiceLanguage));
}
if (loginData.settings.speech.pitch) {
dispatch(changePitch(loginData.settings.speech.pitch));
}
if (loginData.settings.speech.rate) {
dispatch(changeRate(loginData.settings.speech.rate));
}
return;
}
}
if (
deviceVoiceUri &&
deviceVoiceLanguageCode === appLanguageCode &&
uris.includes(deviceVoiceUri)
) {
return;
}
const defaultVoiceUri = getVoiceURI(appLang, voices);
if (defaultVoiceUri === emptyVoiceString) {
dispatch(changeVoice(emptyVoiceString, ''));
return;
}
//if the api stored voice is unavailable. Set default voice
const defaultVoiceLanguage = voices.filter(
voice => voice.voiceURI === defaultVoiceUri
)[0]?.lang;
dispatch(changeVoice(defaultVoiceUri, defaultVoiceLanguage));
return;
}
if (deviceVoiceLang === null) {
dispatch(changeVoice(emptyVoiceString, ''));
return;
}
};
return async (dispatch, getState) => {
try {
const apiMethod = type === 'local' ? 'login' : 'oAuthLogin';
const loginData = activatedData
? activatedData
: await API[apiMethod](email, password);
const hasRemoteCommunicators =
loginData.communicators && loginData.communicators.length > 0;
const discardLocalChanges = hasRemoteCommunicators;
if (type === 'local') {
dispatch(
disableTour({
isRootBoardTourEnabled: false,
isUnlockedTourEnabled: false,
isSettingsTourEnabled: false,
isAnalyticsTourEnabled: false,
isSymbolSearchTourEnabled: false
})
);
}
dispatch(loginSuccess({ ...loginData, discardLocalChanges }));
await setAVoice({ loginData, dispatch, getState });
} catch (e) {
console.error(e);
if (e.response != null) {
return Promise.reject(e.response.data);
}
var disonnected = {
message: 'Unable to contact server. Try in a moment'
};
return Promise.reject(disonnected);
}
};
}