2020-10-11 20:15:53 +03:00
|
|
|
import type { Account } from 'app/components/accounts/reducer';
|
2024-12-17 23:11:39 +01:00
|
|
|
import logger from 'app/services/logger';
|
2020-10-11 20:15:53 +03:00
|
|
|
|
2016-11-13 16:47:56 +02:00
|
|
|
import AbstractState from './AbstractState';
|
2020-01-17 23:37:52 +03:00
|
|
|
import { AuthContext } from './AuthFlow';
|
2016-11-13 16:47:56 +02:00
|
|
|
import LoginState from './LoginState';
|
|
|
|
import CompleteState from './CompleteState';
|
|
|
|
|
|
|
|
export default class ChooseAccountState extends AbstractState {
|
2020-05-24 02:08:24 +03:00
|
|
|
enter(context: AuthContext): Promise<void> | void {
|
|
|
|
const { auth } = context.getState();
|
2018-02-17 21:59:35 +02:00
|
|
|
|
2020-05-24 02:08:24 +03:00
|
|
|
if (auth.oauth) {
|
|
|
|
context.navigate('/oauth/choose-account');
|
|
|
|
} else {
|
|
|
|
context.navigate('/choose-account');
|
|
|
|
}
|
2016-11-13 16:47:56 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 13:07:23 +02:00
|
|
|
// This method might be called with an empty object to mention that user wants to login into a new account.
|
|
|
|
// Currently, I can't correctly provide typing since there is no type for an empty object.
|
|
|
|
// So if there is no `id` property, it's an empty object
|
|
|
|
resolve(context: AuthContext, payload: Account): Promise<void> | void {
|
2020-05-24 02:08:24 +03:00
|
|
|
if (payload.id) {
|
2024-12-17 23:11:39 +01:00
|
|
|
return (
|
|
|
|
context
|
|
|
|
.run('authenticate', payload)
|
|
|
|
.then(() => context.run('setAccountSwitcher', false))
|
|
|
|
.then(() => context.setState(new CompleteState()))
|
|
|
|
// By default, this error must cause a BSOD. But by I don't know why reasons it shouldn't,
|
|
|
|
// because somebody somewhere catches an invalid authentication result and routes the user
|
|
|
|
// to the password entering form. To keep this behavior we catch all errors, log it and suppress
|
|
|
|
.catch((err) => err.errors || logger.warn('Error choosing an account', err))
|
|
|
|
);
|
2020-05-24 02:08:24 +03:00
|
|
|
}
|
2020-10-23 01:23:59 +03:00
|
|
|
|
|
|
|
// log in to another account
|
|
|
|
context.navigate('/login');
|
|
|
|
context.run('setLogin', null);
|
|
|
|
context.setState(new LoginState());
|
2016-11-13 16:47:56 +02:00
|
|
|
}
|
2016-11-19 14:19:23 +02:00
|
|
|
|
2020-05-24 02:08:24 +03:00
|
|
|
reject(context: AuthContext): void {
|
|
|
|
context.run('logout');
|
|
|
|
}
|
2016-11-13 16:47:56 +02:00
|
|
|
}
|