diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2022-05-12 14:21:15 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2022-05-13 10:49:25 +0200 |
| commit | 5c99fdee273b54a26224c79ef839fc3d9f615707 (patch) | |
| tree | 71d4c36555ed0de0184ec945a39f81f4c315ad2f /gui/src/renderer | |
| parent | 64f206b034c9480ba21fe89472724121bac321e0 (diff) | |
| download | mullvadvpn-5c99fdee273b54a26224c79ef839fc3d9f615707.tar.xz mullvadvpn-5c99fdee273b54a26224c79ef839fc3d9f615707.zip | |
Update to new device state rpc calls
Diffstat (limited to 'gui/src/renderer')
| -rw-r--r-- | gui/src/renderer/app.tsx | 112 | ||||
| -rw-r--r-- | gui/src/renderer/components/Account.tsx | 14 | ||||
| -rw-r--r-- | gui/src/renderer/containers/AccountPage.tsx | 2 | ||||
| -rw-r--r-- | gui/src/renderer/redux/account/actions.ts | 18 |
4 files changed, 81 insertions, 65 deletions
diff --git a/gui/src/renderer/app.tsx b/gui/src/renderer/app.tsx index d6911da317..886a83d8d7 100644 --- a/gui/src/renderer/app.tsx +++ b/gui/src/renderer/app.tsx @@ -7,11 +7,11 @@ import { AccountToken, BridgeSettings, BridgeState, + DeviceEvent, + DeviceState, IAccountData, IAppVersionInfo, IDevice, - IDeviceConfig, - IDeviceEvent, IDeviceRemoval, IDnsOptions, ILocation, @@ -100,8 +100,7 @@ export default class AppRenderer { private relayListPair!: IRelayListPair; private tunnelState!: TunnelState; private settings!: ISettings; - private deviceConfig?: IDeviceConfig; - private hasReceivedDeviceConfig = false; + private deviceState?: DeviceState; private guiSettings!: IGuiSettingsState; private loginState: LoginState = 'none'; private previousLoginState: LoginState = 'none'; @@ -136,9 +135,7 @@ export default class AppRenderer { }); IpcRendererEventChannel.account.listenDevice((deviceEvent) => { - const oldDeviceConfig = this.deviceConfig; - this.hasReceivedDeviceConfig = true; - this.handleAccountChange(deviceEvent, oldDeviceConfig?.accountToken); + this.handleDeviceEvent(deviceEvent); }); IpcRendererEventChannel.account.listenDevices((devices) => { @@ -213,12 +210,15 @@ export default class AppRenderer { this.setAccountExpiry(initialState.accountData?.expiry); this.setSettings(initialState.settings); this.setIsPerformingPostUpgrade(initialState.isPerformingPostUpgrade); - this.handleAccountChange( - { deviceConfig: initialState.deviceConfig }, - undefined, - initialState.navigationHistory !== undefined, - ); - this.hasReceivedDeviceConfig = initialState.hasReceivedDeviceConfig; + + if (initialState.deviceState) { + const deviceState = initialState.deviceState; + this.handleDeviceEvent( + { type: deviceState.type, deviceState } as DeviceEvent, + initialState.navigationHistory !== undefined, + ); + } + this.setAccountHistory(initialState.accountHistory); this.setTunnelState(initialState.tunnelState); this.updateBlockedState(initialState.tunnelState, initialState.settings.blockWhenDisconnected); @@ -363,8 +363,8 @@ export default class AppRenderer { IpcRendererEventChannel.account.updateData(); } - public getDevice = (): Promise<IDevice | undefined> => { - return IpcRendererEventChannel.account.getDevice(); + public getDeviceState = (): Promise<DeviceState> => { + return IpcRendererEventChannel.account.getDeviceState(); }; public fetchDevices = async (accountToken: AccountToken): Promise<Array<IDevice>> => { @@ -589,6 +589,10 @@ export default class AppRenderer { IpcRendererEventChannel.navigation.setScrollPositions(scrollPositions); } + private isLoggedIn(): boolean { + return this.deviceState?.type === 'logged in'; + } + // Make sure that the content height is correct and log if it isn't. This is mostly for debugging // purposes since there's a bug in Electron that causes the app height to be another value than // the one we have set. @@ -735,13 +739,13 @@ export default class AppRenderer { } private getNavigationBase(): RoutePath { - if (this.connectedToDaemon && this.hasReceivedDeviceConfig) { + if (this.connectedToDaemon && this.deviceState !== undefined) { const loginState = this.reduxStore.getState().account.status; const deviceRevoked = loginState.type === 'none' && loginState.deviceRevoked; if (deviceRevoked) { return RoutePath.deviceRevoked; - } else if (this.deviceConfig?.accountToken) { + } else if (this.isLoggedIn()) { return RoutePath.main; } else { return RoutePath.login; @@ -838,48 +842,54 @@ export default class AppRenderer { } } - private handleAccountChange( - newDeviceEvent: IDeviceEvent, - oldAccount?: string, - preventRedirectToConnect?: boolean, - ) { + private handleDeviceEvent(deviceEvent: DeviceEvent, preventRedirectToConnect?: boolean) { const reduxAccount = this.reduxActions.account; - this.deviceConfig = newDeviceEvent.deviceConfig; - const newAccount = newDeviceEvent.deviceConfig?.accountToken; - const newDevice = newDeviceEvent.deviceConfig?.device; + this.deviceState = deviceEvent.deviceState; - if (oldAccount && !newAccount) { - this.loginScheduler.cancel(); - if (!this.reduxStore.getState().account.loggingOut && newDeviceEvent.remote) { - reduxAccount.deviceRevoked(); - } else { - reduxAccount.loggedOut(); - } + switch (deviceEvent.type) { + case 'logged in': { + const accountToken = deviceEvent.deviceState.accountAndDevice.accountToken; + const device = deviceEvent.deviceState.accountAndDevice.device; - this.resetNavigation(); - } else if (newAccount !== undefined && newDevice !== undefined && oldAccount !== newAccount) { - switch (this.loginState) { - case 'none': - case 'logging in': - reduxAccount.loggedIn({ accountToken: newAccount, device: newDevice }); + switch (this.loginState) { + case 'none': + case 'logging in': + reduxAccount.loggedIn(accountToken, device); - if (this.previousLoginState === 'too many devices') { - this.resetNavigation(); - } else if (!preventRedirectToConnect) { - this.redirectToConnect(); - } - break; - case 'creating account': - reduxAccount.accountCreated( - { accountToken: newAccount, device: newDevice }, - new Date().toISOString(), - ); - break; + if (this.previousLoginState === 'too many devices') { + this.resetNavigation(); + } else if (!preventRedirectToConnect) { + this.redirectToConnect(); + } + break; + case 'creating account': + reduxAccount.accountCreated(accountToken, device, new Date().toISOString()); + break; + } + + if (this.loginState !== 'logging in' && this.loginState !== 'creating account') { + this.resetNavigation(); + } + break; } + case 'logged out': + this.loginScheduler.cancel(); + reduxAccount.loggedOut(); + this.resetNavigation(); + break; + case 'revoked': { + this.loginScheduler.cancel(); + + const oldAccountState = this.reduxStore.getState().account; + if (oldAccountState.loggingOut) { + reduxAccount.loggedOut(); + } else { + reduxAccount.deviceRevoked(); + } - if (this.loginState !== 'logging in' && this.loginState !== 'creating account') { this.resetNavigation(); + break; } } diff --git a/gui/src/renderer/components/Account.tsx b/gui/src/renderer/components/Account.tsx index af714273af..e0596a2b79 100644 --- a/gui/src/renderer/components/Account.tsx +++ b/gui/src/renderer/components/Account.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { formatDate, hasExpired } from '../../shared/account-expiry'; -import { AccountToken, IDevice } from '../../shared/daemon-rpc-types'; +import { AccountToken, DeviceState } from '../../shared/daemon-rpc-types'; import { messages } from '../../shared/gettext'; import { AccountContainer, @@ -39,7 +39,7 @@ interface IProps { onClose: () => void; onBuyMore: () => Promise<void>; updateAccountData: () => void; - getDevice: () => Promise<IDevice | undefined>; + getDeviceState: () => Promise<DeviceState | undefined>; } interface IState { @@ -189,10 +189,12 @@ export default class Account extends React.Component<IProps, IState> { this.setState({ logoutDialogState: 'checking-ports' }); this.props.prepareLogout(); - const device = await this.props.getDevice(); - if (device === undefined) { - this.onHideLogoutConfirmationDialog(); - } else if (device.ports !== undefined && device.ports.length > 0) { + const deviceState = await this.props.getDeviceState(); + if ( + deviceState?.type === 'logged in' && + deviceState.accountAndDevice.device?.ports !== undefined && + deviceState.accountAndDevice.device.ports.length > 0 + ) { this.setState({ logoutDialogState: 'confirm' }); } else { this.props.onLogout(); diff --git a/gui/src/renderer/containers/AccountPage.tsx b/gui/src/renderer/containers/AccountPage.tsx index a813b12002..a81676c48b 100644 --- a/gui/src/renderer/containers/AccountPage.tsx +++ b/gui/src/renderer/containers/AccountPage.tsx @@ -29,7 +29,7 @@ const mapDispatchToProps = (dispatch: ReduxDispatch, props: IHistoryProps & IApp }, onBuyMore: () => props.app.openLinkWithAuth(links.purchase), updateAccountData: () => props.app.updateAccountData(), - getDevice: () => props.app.getDevice(), + getDeviceState: () => props.app.getDeviceState(), }; }; diff --git a/gui/src/renderer/redux/account/actions.ts b/gui/src/renderer/redux/account/actions.ts index ba9d524cc0..cfa672a407 100644 --- a/gui/src/renderer/redux/account/actions.ts +++ b/gui/src/renderer/redux/account/actions.ts @@ -1,4 +1,4 @@ -import { AccountToken, IDevice, IDeviceConfig } from '../../../shared/daemon-rpc-types'; +import { AccountToken, IDevice } from '../../../shared/daemon-rpc-types'; interface IStartLoginAction { type: 'START_LOGIN'; @@ -107,11 +107,11 @@ function startLogin(accountToken: AccountToken): IStartLoginAction { }; } -function loggedIn(deviceConfig: IDeviceConfig): ILoggedInAction { +function loggedIn(accountToken: AccountToken, device?: IDevice): ILoggedInAction { return { type: 'LOGGED_IN', - accountToken: deviceConfig.accountToken, - deviceName: deviceConfig.device?.name, + accountToken, + deviceName: device?.name, }; } @@ -172,11 +172,15 @@ function createAccountFailed(error: Error): ICreateAccountFailed { }; } -function accountCreated(deviceConfig: IDeviceConfig, expiry: string): IAccountCreated { +function accountCreated( + accountToken: AccountToken, + device: IDevice | undefined, + expiry: string, +): IAccountCreated { return { type: 'ACCOUNT_CREATED', - accountToken: deviceConfig.accountToken, - deviceName: deviceConfig.device?.name, + accountToken: accountToken, + deviceName: device?.name, expiry, }; } |
