diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2017-11-29 17:58:01 +0100 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2017-12-06 13:36:25 +0100 |
| commit | 841dabe013f278161298d4781ff29be125b25fad (patch) | |
| tree | 368e0b426289f9c0b3fb811e9824cc9318e2db2b | |
| parent | 07a007976410a5fbaafe1e893125cbb9b25f993f (diff) | |
| download | mullvadvpn-841dabe013f278161298d4781ff29be125b25fad.tar.xz mullvadvpn-841dabe013f278161298d4781ff29be125b25fad.zip | |
Convert Backend.login() to asyc
| -rw-r--r-- | app/lib/backend.js | 58 |
1 files changed, 30 insertions, 28 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js index af559c3c16..3fb2992c22 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -185,42 +185,44 @@ export class Backend { } } - login(accountToken: AccountToken): Promise<void> { + async login(accountToken: AccountToken): Promise<void> { log.debug('Attempting to login with account number', accountToken); this._store.dispatch(accountActions.startLogin(accountToken)); - return this._ensureAuthenticated() - .then( () => { - return this._ipc.getAccountData(accountToken) - .then( response => { - log.debug('Account exists', response); + try { + await this._ensureAuthenticated(); - return this._ipc.setAccount(accountToken) - .then( () => response ); + const accountData = await this._ipc.getAccountData(accountToken); - }).then( accountData => { - log.info('Log in complete'); + log.debug('Account exists', accountData); - this._store.dispatch(accountActions.loginSuccessful(accountData.expiry)); - return this.fetchRelaySettings(); - }) - .then( () => { - // Redirect the user after some time to allow for - // the 'Login Successful' screen to be visible - setTimeout(() => { - this._store.dispatch(push('/connect')); - log.debug('Autoconnecting...'); - this.connect(); - }, 1000); - }).catch(e => { - log.error('Failed to log in,', e.message); + await this._ipc.setAccount(accountToken); - // TODO: This is not true. If there is a communication link failure the promise will be rejected too - const err = new BackendError('INVALID_ACCOUNT'); - this._store.dispatch(accountActions.loginFailed(err)); - }).then(() => this._updateAccountHistory()); - }); + log.info('Log in complete'); + + this._store.dispatch( + accountActions.loginSuccessful(accountData.expiry) + ); + await this.fetchRelaySettings(); + + // Redirect the user after some time to allow for + // the 'Login Successful' screen to be visible + setTimeout(() => { + this._store.dispatch(push('/connect')); + log.debug('Autoconnecting...'); + this.connect(); + }, 1000); + + await this._updateAccountHistory(); + + } catch(e) { + log.error('Failed to log in,', e.message); + + // TODO: This is not true. If there is a communication link failure the promise will be rejected too + const err = new BackendError('INVALID_ACCOUNT'); + this._store.dispatch(accountActions.loginFailed(err)); + } } async autologin() { |
