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, authState: global.authState,
isScreenLocked: global.passcode?.isScreenLocked, isScreenLocked: global.passcode?.isScreenLocked,
hasPasscode: global.passcode?.hasPasscode, hasPasscode: global.passcode?.hasPasscode,
hasWebAuthTokenFailed: global.hasWebAuthTokenFailed, hasWebAuthTokenFailed: global.hasWebAuthTokenFailed || global.hasWebAuthTokenPasswordRequired,
}; };
}, },
)(App); )(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({ onUpdate({
...buildAuthStateUpdate('authorizationStateWaitPassword'), ...buildAuthStateUpdate('authorizationStateWaitPassword'),
hint, hint,
noReset,
}); });
return new Promise<string>((resolve) => { return new Promise<string>((resolve) => {

View File

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

View File

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

View File

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

View File

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

View File

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