Skip to content

Commit 5a76058

Browse files
authored
Added snippets for email action links API (#264)
* Added snippets for email action links API * Fixing a lint error; Fixing a regression caused by googleapis/google-auth-library-python#324 * Fixing order of exported methods * Applying feedback from docs team; Updating snippets based on code review comments
1 parent e48e2b8 commit 5a76058

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

firebase_admin/auth.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
'create_session_cookie',
5353
'create_user',
5454
'delete_user',
55-
'generate_password_reset_link',
5655
'generate_email_verification_link',
56+
'generate_password_reset_link',
5757
'generate_sign_in_with_email_link',
5858
'get_user',
5959
'get_user_by_email',
@@ -464,7 +464,7 @@ def generate_password_reset_link(email, action_code_settings=None, app=None):
464464
passed in the deep link.
465465
app: An App instance (optional).
466466
Returns:
467-
link: The password reset link created by API
467+
link: The password reset link created by the API
468468
469469
Raises:
470470
ValueError: If the provided arguments are invalid
@@ -488,7 +488,7 @@ def generate_email_verification_link(email, action_code_settings=None, app=None)
488488
passed in the deep link.
489489
app: An App instance (optional).
490490
Returns:
491-
link: The email verification link created by API
491+
link: The email verification link created by the API
492492
493493
Raises:
494494
ValueError: If the provided arguments are invalid
@@ -512,7 +512,7 @@ def generate_sign_in_with_email_link(email, action_code_settings, app=None):
512512
passed in the deep link.
513513
app: An App instance (optional).
514514
Returns:
515-
link: The email sign in link created by API
515+
link: The email sign-in link created by the API
516516
517517
Raises:
518518
ValueError: If the provided arguments are invalid

snippets/auth/index.py

+48
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,54 @@ def import_without_password():
587587
print('Error importing users:', error)
588588
# [END import_without_password]
589589

590+
def init_action_code_settings():
591+
# [START init_action_code_settings]
592+
action_code_settings = auth.ActionCodeSettings(
593+
url='https://www.example.com/checkout?cartId=1234',
594+
handle_code_in_app=True,
595+
ios_bundle_id='com.example.ios',
596+
android_package_name='com.example.android',
597+
android_install_app=True,
598+
android_minimum_version='12',
599+
dynamic_link_domain='coolapp.page.link',
600+
)
601+
# [END init_action_code_settings]
602+
return action_code_settings
603+
604+
def password_reset_link():
605+
action_code_settings = init_action_code_settings()
606+
# [START password_reset_link]
607+
608+
link = auth.generate_password_reset_link(email, action_code_settings)
609+
# Construct password reset email from a template embedding the link, and send
610+
# using a custom SMTP server.
611+
send_custom_email(email, link)
612+
# [END password_reset_link]
613+
614+
def email_verification_link():
615+
action_code_settings = init_action_code_settings()
616+
# [START email_verification_link]
617+
618+
link = auth.generate_email_verification_link(email, action_code_settings)
619+
# Construct email from a template embedding the link, and send
620+
# using a custom SMTP server.
621+
send_custom_email(email, link)
622+
# [END email_verification_link]
623+
624+
def sign_in_with_email_link():
625+
action_code_settings = init_action_code_settings()
626+
# [START sign_in_with_email_link]
627+
628+
link = auth.generate_sign_in_with_email_link(email, action_code_settings)
629+
# Construct email from a template embedding the link, and send
630+
# using a custom SMTP server.
631+
send_custom_email(email, link)
632+
# [END sign_in_with_email_link]
633+
634+
def send_custom_email(email, link):
635+
del email
636+
del link
637+
590638
initialize_sdk_with_service_account()
591639
initialize_sdk_with_application_default()
592640
#initialize_sdk_with_refresh_token()

tests/test_token_gen.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def test_sign_with_iam(self):
207207
iam_resp = '{{"signature": "{0}"}}'.format(signature)
208208
_overwrite_iam_request(app, testutils.MockRequest(200, iam_resp))
209209
custom_token = auth.create_custom_token(MOCK_UID, app=app).decode()
210-
assert custom_token.endswith('.' + signature)
210+
assert custom_token.endswith('.' + signature.rstrip('='))
211211
self._verify_signer(custom_token, 'test-service-account')
212212
finally:
213213
firebase_admin.delete_app(app)
@@ -241,7 +241,7 @@ def test_sign_with_discovered_service_account(self):
241241
request.response = testutils.MockResponse(
242242
200, '{{"signature": "{0}"}}'.format(signature))
243243
custom_token = auth.create_custom_token(MOCK_UID, app=app).decode()
244-
assert custom_token.endswith('.' + signature)
244+
assert custom_token.endswith('.' + signature.rstrip('='))
245245
self._verify_signer(custom_token, 'discovered-service-account')
246246
assert len(request.log) == 2
247247
assert request.log[0][1]['headers'] == {'Metadata-Flavor': 'Google'}

0 commit comments

Comments
 (0)