Skip to content

Commit

Permalink
Associate phone code hash with phone (so phone can change)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Jan 8, 2018
1 parent c12af5e commit 01820c9
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions telethon/telegram_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ def __init__(self, session, api_id, api_hash,
**kwargs
)

# Some fields to easy signing in
self._phone_code_hash = None
# Some fields to easy signing in. Let {phone: hash} be
# a dictionary because the user may change their mind.
self._phone_code_hash = {}
self._phone = None

# endregion
Expand All @@ -167,18 +168,19 @@ def send_code_request(self, phone, force_sms=False):
Information about the result of the request.
"""
phone = utils.parse_phone(phone) or self._phone
phone_hash = self._phone_code_hash.get(phone)

if not self._phone_code_hash:
if not phone_hash:
result = self(SendCodeRequest(phone, self.api_id, self.api_hash))
self._phone_code_hash = result.phone_code_hash
self._phone_code_hash[phone] = phone_hash = result.phone_code_hash
else:
force_sms = True

self._phone = phone

if force_sms:
result = self(ResendCodeRequest(phone, self._phone_code_hash))
self._phone_code_hash = result.phone_code_hash
result = self(ResendCodeRequest(phone, phone_hash))
self._phone_code_hash[phone] = result.phone_code_hash

return result

Expand Down Expand Up @@ -218,7 +220,9 @@ def sign_in(self, phone=None, code=None,
return self.send_code_request(phone)
elif code:
phone = utils.parse_phone(phone) or self._phone
phone_code_hash = phone_code_hash or self._phone_code_hash
phone_code_hash = \
phone_code_hash or self._phone_code_hash.get(phone, None)

if not phone:
raise ValueError(
'Please make sure to call send_code_request first.'
Expand Down Expand Up @@ -274,7 +278,7 @@ def sign_up(self, code, first_name, last_name=''):
"""
result = self(SignUpRequest(
phone_number=self._phone,
phone_code_hash=self._phone_code_hash,
phone_code_hash=self._phone_code_hash.get(self._phone, ''),
phone_code=code,
first_name=first_name,
last_name=last_name
Expand Down

0 comments on commit 01820c9

Please sign in to comment.