-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsaga.ts
139 lines (117 loc) · 3.82 KB
/
saga.ts
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
import getDeepProperty from 'lodash.get';
import { takeLatest, put, call, all } from 'redux-saga/effects';
import { SagaActionTypes, setDisplayLogoutModal, setUser } from '.';
import {
nonceOrAuthorize as nonceOrAuthorizeApi,
fetchCurrentUser,
clearSession as clearSessionApi,
emailLogin as apiEmailLogin,
} from './api';
import { clearChannelsAndConversations } from '../channels-list/saga';
import { clearUsers } from '../users/saga';
import { clearMessages } from '../messages/saga';
import { Events, getAuthChannel } from './channels';
import { getHistory } from '../../lib/browser';
import { completePendingUserProfile } from '../registration/saga';
import { closeUserProfile } from '../user-profile/saga';
import { clearLastActiveConversation } from '../../lib/last-conversation';
import { clearLastActiveTab } from '../../lib/last-tab';
import { clearRewards } from '../rewards/saga';
export const currentUserSelector = () => (state) => {
return getDeepProperty(state, 'authentication.user.data', null);
};
export function* nonceOrAuthorize(action) {
const { signedWeb3Token } = action.payload;
const { nonceToken: nonce = undefined } = yield call(nonceOrAuthorizeApi, signedWeb3Token);
if (nonce) {
yield put(setUser({ nonce, data: null }));
} else {
yield call(completeUserLogin);
}
return { nonce };
}
export function* completeUserLogin(user = null) {
if (!user) {
user = yield call(fetchCurrentUser);
}
if (user.isPending) {
yield call(completePendingUserProfile, user);
return;
}
yield put(setUser({ data: user }));
yield call(publishUserLogin, user);
}
export function* terminate(isAccountChange = false) {
yield put(setUser({ data: null, nonce: null }));
try {
yield call(clearSessionApi);
} catch {
/* No operation, if user is unauthenticated deleting the cookie fails */
}
yield call(clearUserState);
yield call(redirectUnauthenticatedUser, isAccountChange);
yield call(publishUserLogout);
}
export function* getCurrentUser() {
try {
const user = yield call(fetchCurrentUser);
if (!user) {
return { success: false, error: 'unauthenticated' };
}
yield completeUserLogin(user);
return { success: true };
} catch (e) {
return { success: false, error: 'critical' };
}
}
export function* authenticateByEmail(email, password) {
const result = yield call(apiEmailLogin, { email, password });
if (!result.success) {
return result;
}
yield call(completeUserLogin);
return result;
}
export function* clearUserState() {
// Note: This should probably all live in the appropriate areas and listen to the logout event
yield all([
call(clearChannelsAndConversations),
call(clearMessages),
call(clearUsers),
call(closeUserProfile),
]);
}
export function* saga() {
yield takeLatest(SagaActionTypes.Logout, logoutRequest);
yield takeLatest(SagaActionTypes.ForceLogout, forceLogout);
yield takeLatest(SagaActionTypes.CloseLogoutModal, closeLogoutModal);
}
export function* logoutRequest() {
yield put(setDisplayLogoutModal(true));
}
export function* closeLogoutModal() {
yield put(setDisplayLogoutModal(false));
}
export function* forceLogout() {
yield closeLogoutModal();
yield call(clearLastActiveConversation);
yield call(clearLastActiveTab);
yield call(clearRewards);
yield call(terminate);
}
export function* publishUserLogin(user) {
const channel = yield call(getAuthChannel);
yield put(channel, { type: Events.UserLogin, userId: user.id });
}
export function* publishUserLogout() {
const channel = yield call(getAuthChannel);
yield put(channel, { type: Events.UserLogout });
}
export function* redirectUnauthenticatedUser(isAccountChange: boolean) {
const history = getHistory();
if (isAccountChange) {
history.replace({ pathname: '/' });
return;
}
yield history.replace({ pathname: '/login' });
}