Skip to content

Commit

Permalink
feat: add email and password change functionality
Browse files Browse the repository at this point in the history
commit f1b7f2b
Author: Asif Arman Rahman <asifarmanrahman@gmail.com>
Date:   Sun Aug 20 19:26:29 2023 +0600

    docs(auth): guide update

    * change_email
    * change_password

commit 31df27d
Author: Vladislav Dashtu <ladpost@mail.ru>
Date:   Tue Aug 15 22:47:43 2023 +0400

    mistake in change_email description

commit 44092c2
Author: Vladislav Dashtu <ladpost@mail.ru>
Date:   Tue Aug 15 22:43:12 2023 +0400

    added change_email and change_password methods

resolves #20
  • Loading branch information
ladosha authored Aug 20, 2023
1 parent 25451c3 commit 2fb370f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/guide/authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,32 @@ Update stored information or add information into the user's account.
..

change_email
--------------

Change the email associated with the user's account.

.. code-block:: python
# change user's email
auth.change_email(user['idToken'], email='iam@ironman.com')
..

change_password
--------------

Change the password associated with the user's account.

.. code-block:: python
# change user's password
auth.change_password(user['idToken'], password='iLoveYou3000')
..

refresh
-------

Expand Down
56 changes: 56 additions & 0 deletions firebase/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,62 @@ def _token_from_auth_url(self, url):
'type': 'id_token',
'value': request_object.json()['id_token'],
}

def change_email(self, id_token, email):
""" Changes a user's email
| For more details:
| `Firebase Auth REST API | section-change-email`_
.. _Firebase Auth REST API | section-change-email: https://firebase.google.com/docs/reference/rest/auth#section-change-email
:type id_token: str
:param id_token: A Firebase Auth ID token for the user.
:type email: str
:param email: User's new email.
:return: UserInfo and Firebase Auth Tokens.
:rtype: dict
"""

request_ref = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key={0}".format(self.api_key)

headers = {"content-type": "application/json; charset=UTF-8"}
data = json.dumps({"idToken": id_token, "email": email, "returnSecureToken": True})
request_object = self.requests.post(request_ref, headers=headers, data=data)

raise_detailed_error(request_object)

return request_object.json()

def change_password(self, id_token, password):
""" Changes a user's password
| For more details:
| `Firebase Auth REST API | section-change-password`_
.. _Firebase Auth REST API | section-change-password: https://firebase.google.com/docs/reference/rest/auth#section-change-password
:type id_token: str
:param id_token: A Firebase Auth ID token for the user.
:type password: str
:param password: User's new password.
:return: UserInfo and Firebase Auth Tokens.
:rtype: dict
"""

request_ref = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key={0}".format(self.api_key)

headers = {"content-type": "application/json; charset=UTF-8"}
data = json.dumps({"idToken": id_token, "password": password, "returnSecureToken": True})
request_object = self.requests.post(request_ref, headers=headers, data=data)

raise_detailed_error(request_object)

return request_object.json()

def update_profile(self, id_token, display_name=None, photo_url=None, delete_attribute=None):
""" Update a user's profile (display name / photo URL).
Expand Down

0 comments on commit 2fb370f

Please sign in to comment.