diff --git a/FAQ.md b/FAQ.md index fda254b..4f6acf3 100644 --- a/FAQ.md +++ b/FAQ.md @@ -201,8 +201,8 @@ See the [full method list](https://core.telegram.org/methods) (just replace the A session file is created or resumed automatically on startup, and maintained up-to-date automatically throughout the session. That session file is incompatible with TLSharp so you cannot reuse a TLSharp .dat file. You'll need to create a new session. -You don't have to call methods Auth_SignIn/SignUp/.. manually anymore because all the login phase is handled automatically by calling `await client.LoginUserIfNeeded()` after creating the client. -Your Config callback just need to provide the various login answers if they are needed. +**DON'T** call methods Auth_SendCode/SignIn/SignUp/... because all the login phase is handled automatically by calling `await client.LoginUserIfNeeded()` after creating the client. +Your Config callback just need to provide the various login answers if they are needed (see [ReadMe](README.md)). In particular, it will detect and handle automatically the various login cases/particularity like: * Login not necessary (when a session is resumed with an already logged-in user) * 2FA password required (your Config needs to provide "password") diff --git a/src/Client.cs b/src/Client.cs index 6129b15..bdd35aa 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -912,6 +912,7 @@ namespace WTelegram } phone_number ??= Config("phone_number"); Auth_SentCode sentCode; +#pragma warning disable CS0618 // Auth_* methods are marked as obsolete try { sentCode = await this.Auth_SendCode(phone_number, _session.ApiId, _apiHash ??= Config("api_hash"), settings ??= new()); @@ -920,38 +921,46 @@ namespace WTelegram { sentCode = await this.Auth_SendCode(phone_number, _session.ApiId, _apiHash, settings); } - resent: - var timeout = DateTime.UtcNow + TimeSpan.FromSeconds(sentCode.timeout); - OnUpdate(sentCode); - Helpers.Log(3, $"A verification code has been sent via {sentCode.type.GetType().Name[17..]}"); Auth_AuthorizationBase authorization = null; - for (int retry = 1; authorization == null; retry++) - try - { - var verification_code = await ConfigAsync("verification_code"); - if (verification_code == "" && sentCode.next_type != 0) + try + { + resent: + var timeout = DateTime.UtcNow + TimeSpan.FromSeconds(sentCode.timeout); + OnUpdate(sentCode); + Helpers.Log(3, $"A verification code has been sent via {sentCode.type.GetType().Name[17..]}"); + for (int retry = 1; authorization == null; retry++) + try { - var mustWait = timeout - DateTime.UtcNow; - if (mustWait.Ticks > 0) + var verification_code = await ConfigAsync("verification_code"); + if (verification_code == "" && sentCode.next_type != 0) { - Helpers.Log(3, $"You must wait {(int)(mustWait.TotalSeconds + 0.5)} more seconds before requesting the code to be sent via {sentCode.next_type}"); - continue; + var mustWait = timeout - DateTime.UtcNow; + if (mustWait.Ticks > 0) + { + Helpers.Log(3, $"You must wait {(int)(mustWait.TotalSeconds + 0.5)} more seconds before requesting the code to be sent via {sentCode.next_type}"); + continue; + } + sentCode = await this.Auth_ResendCode(phone_number, sentCode.phone_code_hash); + goto resent; } - sentCode = await this.Auth_ResendCode(phone_number, sentCode.phone_code_hash); - goto resent; + authorization = await this.Auth_SignIn(phone_number, sentCode.phone_code_hash, verification_code); } - authorization = await this.Auth_SignIn(phone_number, sentCode.phone_code_hash, verification_code); - } - catch (RpcException e) when (e.Code == 401 && e.Message == "SESSION_PASSWORD_NEEDED") - { - var accountPassword = await this.Account_GetPassword(); - OnUpdate(accountPassword); - var checkPasswordSRP = await Check2FA(accountPassword, () => ConfigAsync("password")); - authorization = await this.Auth_CheckPassword(checkPasswordSRP); - } - catch (RpcException e) when (e.Code == 400 && e.Message == "PHONE_CODE_INVALID" && retry != 3) - { - } + catch (RpcException e) when (e.Code == 401 && e.Message == "SESSION_PASSWORD_NEEDED") + { + var accountPassword = await this.Account_GetPassword(); + OnUpdate(accountPassword); + var checkPasswordSRP = await Check2FA(accountPassword, () => ConfigAsync("password")); + authorization = await this.Auth_CheckPassword(checkPasswordSRP); + } + catch (RpcException e) when (e.Code == 400 && e.Message == "PHONE_CODE_INVALID" && retry != 3) + { + } + } + catch + { + await this.Auth_CancelCode(phone_number, sentCode.phone_code_hash); + throw; + } if (authorization is Auth_AuthorizationSignUpRequired signUpRequired) { var waitUntil = DateTime.UtcNow.AddSeconds(3); @@ -962,6 +971,7 @@ namespace WTelegram if (wait > TimeSpan.Zero) await Task.Delay(wait); // we get a FLOOD_WAIT_3 if we SignUp too fast authorization = await this.Auth_SignUp(phone_number, sentCode.phone_code_hash, first_name, last_name); } +#pragma warning restore CS0618 return LoginAlreadyDone(authorization); } diff --git a/src/TL.SchemaFuncs.cs b/src/TL.SchemaFuncs.cs index 7453bcf..8435e9e 100644 --- a/src/TL.SchemaFuncs.cs +++ b/src/TL.SchemaFuncs.cs @@ -97,6 +97,7 @@ namespace TL /// Application identifier (see App configuration) /// Application secret hash (see App configuration) /// Settings for the code type to send + [Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")] public static Task Auth_SendCode(this Client client, string phone_number, int api_id, string api_hash, CodeSettings settings) => client.Invoke(new Auth_SendCode { @@ -111,6 +112,7 @@ namespace TL /// SMS-message ID /// New user first name /// New user last name + [Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")] public static Task Auth_SignUp(this Client client, string phone_number, string phone_code_hash, string first_name, string last_name) => client.Invoke(new Auth_SignUp { @@ -124,6 +126,7 @@ namespace TL /// Phone number in the international format /// SMS-message ID, obtained from auth.sendCode /// Valid numerical code from the SMS-message + [Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")] public static Task Auth_SignIn(this Client client, string phone_number, string phone_code_hash, string phone_code) => client.Invoke(new Auth_SignIn { @@ -217,6 +220,7 @@ namespace TL /// Resend the login code via another medium, the phone code type is determined by the return value of the previous auth.sendCode/auth.resendCode: see login for more info. See Possible codes: 400,406 (details) /// The phone number /// The phone code hash obtained from auth.sendCode + [Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")] public static Task Auth_ResendCode(this Client client, string phone_number, string phone_code_hash) => client.Invoke(new Auth_ResendCode { @@ -227,6 +231,7 @@ namespace TL /// Cancel the login verification code See Possible codes: 400,406 (details) /// Phone number /// Phone code hash from auth.sendCode + [Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")] public static Task Auth_CancelCode(this Client client, string phone_number, string phone_code_hash) => client.Invoke(new Auth_CancelCode {