From 2fb370f3e1d134c6aa0c10428c76fc4bd9d26128 Mon Sep 17 00:00:00 2001 From: ladosha Date: Sun, 20 Aug 2023 17:48:00 +0400 Subject: [PATCH] feat: add email and password change functionality commit f1b7f2b2c8840a1f04e3cfc8a8bb6f32dfccc5e4 Author: Asif Arman Rahman Date: Sun Aug 20 19:26:29 2023 +0600 docs(auth): guide update * change_email * change_password commit 31df27d1d63e7baa40f0dd9d350f6494c48e5e74 Author: Vladislav Dashtu Date: Tue Aug 15 22:47:43 2023 +0400 mistake in change_email description commit 44092c26309058779954d765d72260eefc34068e Author: Vladislav Dashtu Date: Tue Aug 15 22:43:12 2023 +0400 added change_email and change_password methods resolves #20 --- docs/guide/authentication.rst | 26 ++++++++++++++++ firebase/auth/__init__.py | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/docs/guide/authentication.rst b/docs/guide/authentication.rst index 8f71092..9145049 100644 --- a/docs/guide/authentication.rst +++ b/docs/guide/authentication.rst @@ -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 ------- diff --git a/firebase/auth/__init__.py b/firebase/auth/__init__.py index 5219a11..97ff87e 100644 --- a/firebase/auth/__init__.py +++ b/firebase/auth/__init__.py @@ -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).