Revert "Revert "Fix Web Token login (again) (#2137)""

This reverts commit 9a9ea40166b7f0ad3af5650c353b155df028ce79.
This commit is contained in:
Alexander Zinchuk 2022-11-18 00:54:39 +04:00
parent e7d3de60bd
commit d6972c2a6f
7 changed files with 18 additions and 14 deletions

View File

@ -181,7 +181,7 @@ export default withGlobal(
authState: global.authState,
isScreenLocked: global.passcode?.isScreenLocked,
hasPasscode: global.passcode?.hasPasscode,
hasWebAuthTokenFailed: global.hasWebAuthTokenFailed,
hasWebAuthTokenFailed: global.hasWebAuthTokenFailed || global.hasWebAuthTokenPasswordRequired,
};
},
)(App);

View File

@ -54,10 +54,11 @@ export function onRequestCode(isCodeViaApp = false) {
});
}
export function onRequestPassword(hint?: string) {
export function onRequestPassword(hint?: string, noReset?: boolean) {
onUpdate({
...buildAuthStateUpdate('authorizationStateWaitPassword'),
hint,
noReset,
});
return new Promise<string>((resolve) => {

View File

@ -61,6 +61,7 @@ export type ApiUpdateAuthorizationState = {
authorizationState: ApiUpdateAuthorizationStateType;
isCodeViaApp?: boolean;
hint?: string;
noReset?: boolean;
qrCode?: { token: string; expires: number };
};

View File

@ -24,21 +24,21 @@ type OwnProps = {
isActive: boolean;
};
type StateProps = Pick<GlobalState, 'authState'>;
type StateProps = Pick<GlobalState, 'authState' | 'hasWebAuthTokenPasswordRequired'>;
const Auth: FC<OwnProps & StateProps> = ({
isActive, authState,
isActive, authState, hasWebAuthTokenPasswordRequired,
}) => {
const {
reset, initApi, returnToAuthPhoneNumber, goToAuthQrCode,
} = getActions();
useEffect(() => {
if (isActive) {
if (isActive && !hasWebAuthTokenPasswordRequired) {
reset();
initApi();
}
}, [isActive, reset, initApi]);
}, [isActive, reset, initApi, hasWebAuthTokenPasswordRequired]);
const isMobile = PLATFORM_ENV === 'iOS' || PLATFORM_ENV === 'Android';
@ -113,5 +113,5 @@ const Auth: FC<OwnProps & StateProps> = ({
};
export default memo(withGlobal<OwnProps>(
(global): StateProps => pick(global, ['authState']),
(global): StateProps => pick(global, ['authState', 'hasWebAuthTokenPasswordRequired']),
)(Auth));

View File

@ -114,6 +114,7 @@ function onUpdateAuthorizationState(update: ApiUpdateAuthorizationState) {
setGlobal({
...global,
authHint: update.hint,
hasWebAuthTokenPasswordRequired: update.noReset,
});
break;
case 'authorizationStateWaitQrCode':

View File

@ -141,6 +141,7 @@ export type GlobalState = {
appConfig?: ApiAppConfig;
canInstall?: boolean;
hasWebAuthTokenFailed?: boolean;
hasWebAuthTokenPasswordRequired?: boolean;
isChatInfoShown: boolean;
isStatisticsShown?: boolean;
isLeftColumnShown: boolean;

View File

@ -6,9 +6,9 @@ import { computeCheck as computePasswordSrpCheck } from '../Password';
export interface UserAuthParams {
phoneNumber: string | (() => Promise<string>);
webAuthTokenFailed: VoidFunction;
webAuthTokenFailed: () => void;
phoneCode: (isCodeViaApp?: boolean) => Promise<string>;
password: (hint?: string) => Promise<string>;
password: (hint?: string, noReset?: boolean) => Promise<string>;
firstAndLastNames: () => Promise<[string, string?]>;
qrCode: (qrCode: { token: Buffer; expires: number }) => Promise<void>;
onError: (err: Error) => void;
@ -87,11 +87,11 @@ async function signInUserWithWebToken(
throw new Error('SIGN_UP_REQUIRED');
}
} catch (err: any) {
authParams.webAuthTokenFailed();
client._log.error(`Failed to login with web token: ${err}`);
if (err.message === 'SESSION_PASSWORD_NEEDED') {
return signInWithPassword(client, apiCredentials, authParams);
return signInWithPassword(client, apiCredentials, authParams, true);
} else {
client._log.error(`Failed to login with web token: ${err}`);
authParams.webAuthTokenFailed();
return signInUserWithPreferredMethod(client, apiCredentials, {
...authParams,
webAuthToken: undefined,
@ -344,13 +344,13 @@ async function sendCode(
}
async function signInWithPassword(
client: TelegramClient, apiCredentials: ApiCredentials, authParams: UserAuthParams,
client: TelegramClient, apiCredentials: ApiCredentials, authParams: UserAuthParams, noReset = false,
): Promise<Api.TypeUser> {
// eslint-disable-next-line no-constant-condition
while (1) {
try {
const passwordSrpResult = await client.invoke(new Api.account.GetPassword());
const password = await authParams.password(passwordSrpResult.hint);
const password = await authParams.password(passwordSrpResult.hint, noReset);
if (!password) {
throw new Error('Password is empty');
}