-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.js
73 lines (63 loc) · 2.2 KB
/
auth.js
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
import { call, select, take } from 'redux-saga/effects';
import { LOGIN, LOGOUT } from '../actions/actions';
import { getAccountErrorMessage, getCurrentAccount } from '../reducers/account';
import { AsureWsWalletProvider } from '../utils/asure-ws-wallet-provider';
import { getCurrentEthereumNetwork, getProtocol } from '../reducers/network';
import { AsureHttpWalletProvider } from '../utils/asure-http-wallet-provider';
// Override currentProvider from Mist/Metamask with our asure wallet for now
// This is picked up by drizzle.
function setWeb3Provider(mnemonic, protocol, currentEthereumNetwork) {
window.web3 = {};
const url = currentEthereumNetwork.urls[protocol];
switch (protocol) {
case 'http':
window.web3.currentProvider = new AsureHttpWalletProvider(mnemonic, url);
break;
case 'ws':
window.web3.currentProvider = new AsureWsWalletProvider(mnemonic, url);
break;
default:
throw new Error(
`Unsupported provider protocol "${protocol}". Please make sure that REACT_APP_PROVIDER_PROTOCOL is either "http" or "ws"`
);
}
}
export function* authFlowSaga() {
yield take('persist/REHYDRATE');
let account = yield select(getCurrentAccount);
let currentEthereumNetwork = yield select(getCurrentEthereumNetwork);
let protocol = yield call(getProtocol);
let isAuthenticated = !!account;
// In case the user in already logged in, we have to set the web3 provider.
if (isAuthenticated) {
yield call(
setWeb3Provider,
account.mnemonic,
protocol,
currentEthereumNetwork
);
}
while (true) {
if (!isAuthenticated) {
yield take(LOGIN);
isAuthenticated = !(yield select(getAccountErrorMessage));
if (!isAuthenticated) {
console.log('User login failed.');
continue;
}
console.log('User logged in.');
account = yield select(getCurrentAccount);
currentEthereumNetwork = yield select(getCurrentEthereumNetwork);
protocol = yield call(getProtocol);
yield call(
setWeb3Provider,
account.mnemonic,
protocol,
currentEthereumNetwork
);
}
yield take(LOGOUT);
isAuthenticated = false;
window.location.hash = '/';
}
}