Skip to content

Commit

Permalink
refactor: usage of TwoFactorAuth status
Browse files Browse the repository at this point in the history
  • Loading branch information
gorjan5sk committed Jul 26, 2021
1 parent 311f50d commit 1f75df8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
} from '../../components';
import { Switch } from '../../../components/Switch';
import { observer } from 'mobx-react-lite';
import { TwoFactorAuth } from './model';
import {
is2FAActivation,
is2FADisabled,
is2FAEnabled,
TwoFactorAuth,
} from './model';
import { TwoFactorDisabledView } from './TwoFactorDisabledView';
import { TwoFactorEnabledView } from './TwoFactorEnabledView';
import { TwoFactorActivationView } from './TwoFactorActivationView';
Expand All @@ -25,24 +30,24 @@ export const TwoFactorAuthView: FunctionComponent<{
</Text>
</div>
<Switch
checked={auth.enabled !== false}
checked={!is2FADisabled(auth.status)}
onChange={() => auth.toggle2FA()}
/>
</div>
</PreferencesSegment>
<PreferencesSegment>
{auth.enabled !== false && (
{is2FAEnabled(auth.status) && (
<TwoFactorEnabledView
secretKey={auth.enabled.secretKey}
authCode={auth.enabled.authCode}
secretKey={auth.status.secretKey}
authCode={auth.status.authCode}
/>
)}

{auth.activation !== false && (
<TwoFactorActivationView activation={auth.activation} />
{is2FAActivation(auth.status) && (
<TwoFactorActivationView activation={auth.status} />
)}

{auth.enabled === false && <TwoFactorDisabledView />}
{!is2FAEnabled(auth.status) && <TwoFactorDisabledView />}
</PreferencesSegment>
</PreferencesGroup>
));
37 changes: 18 additions & 19 deletions app/assets/javascripts/preferences/panes/two-factor-auth/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class TwoFactorActivation {
}

export class TwoFactorEnabled {
public readonly type = 'enabled' as const;
public readonly type = 'two-factor-enabled' as const;
private _secretKey: string;
private _authCode: string;

Expand All @@ -122,11 +122,22 @@ export class TwoFactorEnabled {
}
}

type TwoFactorStatus =
| TwoFactorEnabled
| TwoFactorActivation
| 'two-factor-disabled';

export const is2FADisabled = (s: TwoFactorStatus): s is 'two-factor-disabled' =>
s === 'two-factor-disabled';

export const is2FAActivation = (s: TwoFactorStatus): s is TwoFactorActivation =>
(s as any).type === 'two-factor-activation';

export const is2FAEnabled = (s: TwoFactorStatus): s is TwoFactorEnabled =>
(s as any).type === 'two-factor-enabled';

export class TwoFactorAuth {
private _status:
| TwoFactorEnabled
| TwoFactorActivation
| 'two-factor-disabled' = 'two-factor-disabled';
private _status: TwoFactorStatus = 'two-factor-disabled';

constructor() {
makeAutoObservable<TwoFactorAuth, '_status'>(this, {
Expand All @@ -151,19 +162,7 @@ export class TwoFactorAuth {
else this.deactivate2FA();
}

get enabled() {
return (
(this._status instanceof TwoFactorEnabled &&
(this._status as TwoFactorEnabled)) ||
false
);
}

get activation() {
return (
(this._status instanceof TwoFactorActivation &&
(this._status as TwoFactorActivation)) ||
false
);
get status() {
return this._status;
}
}

0 comments on commit 1f75df8

Please sign in to comment.