Skip to content

feat: identity provider functions #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 20, 2023
Merged

Conversation

Salakar
Copy link
Member

@Salakar Salakar commented Apr 18, 2023

Testing:

Deployed the following sample:

from firebase_functions import identity_fn


@identity_fn.before_user_created(
    id_token=True,
    access_token=True,
    refresh_token=True,
)
def beforeusercreated(
    event: identity_fn.AuthBlockingEvent
) -> identity_fn.BeforeCreateResponse | None:
    print(event)
    if not event.data.email:
        return None
    if "@cats.com" in event.data.email:
        return identity_fn.BeforeCreateResponse(display_name="Meow!",)
    if "@dogs.com" in event.data.email:
        return identity_fn.BeforeCreateResponse(display_name="Woof!",)
    return None


@identity_fn.before_user_signed_in(
    id_token=True,
    access_token=True,
    refresh_token=True,
)
def beforeusersignedin(
    event: identity_fn.AuthBlockingEvent
) -> identity_fn.BeforeSignInResponse | None:
    print(event)
    if not event.data.email:
        return None

    if "@cats.com" in event.data.email:
        return identity_fn.BeforeSignInResponse(session_claims={"emoji": "🐈"})

    if "@dogs.com" in event.data.email:
        return identity_fn.BeforeSignInResponse(session_claims={"emoji": "🐕"})

    return None

image
image


created a user with email "hello123@dogs.com" and function triggered:

image

image


signed in a user with email "hello123@cats.com" and function triggered:

image

image

@Salakar Salakar force-pushed the identity branch 4 times, most recently from f6ed550 to 686604a Compare April 19, 2023 14:42
@Salakar Salakar marked this pull request as ready for review April 19, 2023 15:17
The time the event was triggered."""


class BeforeCreateResponse(_typing.TypedDict, total=False):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: What's the difference between typedict vs dataclass 🤔

should we prefer one style over another here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answer: typedict allows us to distinguish undefined vs null (in js)

options = _options.BlockingOptions(**kwargs)

def before_user_signed_in_decorator(func: BeforeUserSignedInCallable):
from firebase_functions.private._identity_fn import event_type_before_sign_in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: is lazy importing intentional? is there a rule of thumb you are using to choose lazy import vs top level import?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answer: to avoid circular imports

@taeold taeold merged commit 9a011eb into firebase:main Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants