1+ /*---------------------------------------------------------------------------------------------
2+ * Copyright (c) EGAAS S.A. All rights reserved.
3+ * See LICENSE in the project root for license information.
4+ *--------------------------------------------------------------------------------------------*/
5+
6+ import { Epic } from 'modules' ;
7+ import { Observable } from 'rxjs/Observable' ;
8+ import { discoverNetwork , initialize } from 'modules/engine/actions' ;
9+ import { connect } from '../actions' ;
10+ import keyring from 'lib/keyring' ;
11+
12+ const initConnectEpic : Epic = ( action$ , store , { api, defaultKey } ) => action$ . ofType ( discoverNetwork . done . type , initialize . done . type )
13+ . filter ( ( ) => ! ! store . getState ( ) . engine . guestSession )
14+ . flatMap ( action => {
15+ const state = store . getState ( ) ;
16+ const network = state . storage . networks . find ( n => n . uuid === state . engine . guestSession . network . uuid ) ;
17+
18+ if ( ! network ) {
19+ return Observable . empty < never > ( ) ;
20+ }
21+
22+ const publicKey = keyring . generatePublicKey ( defaultKey ) ;
23+ const client = api ( {
24+ apiHost : state . engine . guestSession . network . apiHost
25+ } ) ;
26+
27+ return Observable . from ( client . getUid ( ) )
28+ . flatMap ( uid => client . authorize ( uid . token ) . login ( {
29+ publicKey,
30+ signature : keyring . sign ( uid . uid , defaultKey )
31+ } ) )
32+ . flatMap ( loginResult =>
33+ Observable . from ( client . authorize ( loginResult . token ) . getConfig ( {
34+ name : 'centrifugo'
35+
36+ } ) ) . map ( centrifugo => connect . started ( {
37+ wsHost : network . socketUrl || centrifugo ,
38+ session : loginResult . token ,
39+ socketToken : loginResult . notify_key ,
40+ timestamp : loginResult . timestamp ,
41+ userID : loginResult . key_id
42+ } ) )
43+ )
44+ . catch ( ( e : any ) => Observable . empty < never > ( ) ) ;
45+ } ) ;
46+
47+ export default initConnectEpic ;
0 commit comments