@@ -41,6 +41,7 @@ export const CONNECTION_SUCCESS = 'connections/CONNECTION_SUCCESS'
4141export const DISCONNECTION_SUCCESS = 'connections/DISCONNECTION_SUCCESS'
4242export const LOST_CONNECTION = 'connections/LOST_CONNECTION'
4343export const UPDATE_CONNECTION_STATE = 'connections/UPDATE_CONNECTION_STATE'
44+ export const UPDATE_RETAIN_CREDENTIALS = NAME + '/UPDATE_RETAIN_CREDENTIALS'
4445
4546export const DISCONNECTED_STATE = 0
4647export const CONNECTED_STATE = 1
@@ -78,7 +79,13 @@ export function getActiveConnection (state) {
7879}
7980
8081export function getActiveConnectionData ( state ) {
81- return state [ NAME ] . activeConnection ? state [ NAME ] . connectionsById [ state [ NAME ] . activeConnection ] : null
82+ if ( ! state [ NAME ] . activeConnection ) return null
83+ let data = state [ NAME ] . connectionsById [ state [ NAME ] . activeConnection ]
84+ if ( data . username && data . password ) return data
85+ if ( ! ( data . username && data . password ) && ( memoryUsername && memoryPassword ) ) { // No retain state
86+ return { ...data , username : memoryUsername , password : memoryPassword }
87+ }
88+ return data
8289}
8390
8491const addConnectionHelper = ( state , obj ) => {
@@ -134,6 +141,11 @@ const mergeConnectionHelper = (state, connection) => {
134141 )
135142}
136143
144+ // Local vars
145+ let memoryUsername = ''
146+ let memoryPassword = ''
147+
148+ // Reducer
137149export default function ( state = initialState , action ) {
138150 state = hydrate ( initialState , state )
139151
@@ -210,11 +222,20 @@ export const connectionLossFilter = (action) => {
210222 return notLostCodes . indexOf ( action . error . code ) < 0
211223}
212224
225+ export const setRetainCredentials = ( shouldRetain ) => {
226+ return {
227+ type : UPDATE_RETAIN_CREDENTIALS ,
228+ shouldRetain
229+ }
230+ }
231+
213232// Epics
214233export const connectEpic = ( action$ , store ) => {
215234 return action$ . ofType ( CONNECT )
216235 . mergeMap ( ( action ) => {
217236 if ( ! action . $$responseChannel ) return Rx . Observable . of ( null )
237+ memoryUsername = ''
238+ memoryPassword = ''
218239 return bolt . openConnection ( action , { encrypted : getEncryptionMode ( ) } , onLostConnection ( store . dispatch ) )
219240 . then ( ( res ) => ( { type : action . $$responseChannel , success : true } ) )
220241 . catch ( ( [ e ] ) => ( { type : action . $$responseChannel , success : false , error : e } ) )
@@ -324,3 +345,26 @@ export const checkSettingsForRoutingDriver = (action$, store) => {
324345 return { type : 'NOOP' }
325346 } )
326347}
348+
349+ export const retainCredentialsSettingsEpic = ( action$ , store ) => {
350+ return action$ . ofType ( UPDATE_RETAIN_CREDENTIALS )
351+ . do ( ( action ) => {
352+ const connection = getActiveConnectionData ( store . getState ( ) )
353+ if ( ! action . shouldRetain && ( connection . username || connection . password ) ) {
354+ memoryUsername = connection . username
355+ memoryPassword = connection . password
356+ connection . username = ''
357+ connection . password = ''
358+ return store . dispatch ( updateConnection ( connection ) )
359+ }
360+ if ( action . shouldRetain && memoryUsername && memoryPassword ) {
361+ connection . username = memoryUsername
362+ connection . password = memoryPassword
363+ memoryUsername = ''
364+ memoryPassword = ''
365+ return store . dispatch ( updateConnection ( connection ) )
366+ }
367+ return
368+ } )
369+ . mapTo ( { type : 'NOOP' } )
370+ }
0 commit comments