Skip to main content

google-auth

Sign in with Google.

Installation

pip install reflex-google-auth

Usage

Create Google OAuth2 Client ID

Head over to https://console.developers.google.com/apis/credentials and sign in with the Google account that should manage the app and credential tokens.

  • Click "Create Project" and give it a name. After creation the new project should be selected.
  • Click "Configure Consent Screen", Choose "External", then Create.
    • Enter App Name and User Support Email -- these will be shown to users when logging in
    • Scroll all the way down to "Developer contact information" and add your email address, click "Save and Continue"
    • Click "Add or Remove Scopes"
      • Select "Email", "Profile", and "OpenID Connect"
      • Click "Update", then "Save and Continue"
    • Add any test users that should be able to log in during development.
  • From the "Credentials" page, click "+ Create Credentials", then "OAuth client ID"
    • Select Application Type: "Web Application"
    • Add Authorized Javascript Origins: http://localhost, http://localhost:3000, https://example.com (prod domain must be HTTPS)
    • If using custom button, add the same origins to "Authorized redirect URIs"
    • Click "Save"
  • Copy the OAuth "Client ID" and "Client Secret" and save it for later. Mine looks like 309209880368-4uqd9e44h7t4alhhdqn48pvvr63cc5j5.apps.googleusercontent.com

https://github.com/reflex-dev/reflex-examples/assets/1524005/af2499a6-0bda-4d60-b52b-4f51b7322fd5

Configure Environment Variables

Set the following environment variables based on your deployment.

export GOOGLE_CLIENT_ID="309209880368-4uqd9e44h7t4alhhdqn48pvvr63cc5j5.apps.googleusercontent.com"
export GOOGLE_CLIENT_SECRET="your_client_secret"
export GOOGLE_REDIRECT_URI="http://localhost:3000"

Integrate with Reflex app

The GoogleAuthState provided by this component has a token_is_valid var that should be checked before returning any protected content.

Additionally the GoogleAuthState.tokeninfo dict contains the user's profile information.

from reflex_google_auth import GoogleAuthState, require_google_login


class State(GoogleAuthState):
    @rx.var(cache=True)
    def protected_content(self) -> str:
        if self.token_is_valid:
            return f"This content can only be viewed by a logged in User. Nice to see you {self.tokeninfo['name']}"
        return "Not logged in."

The convenience decorator, require_google_login, can wrap an existing component, and show the "Sign in with Google" button if the user is not already authenticated. It can be used on a page function or any subcomponent function of the page.

The "Sign in with Google" button can also be displayed via google_login():

from reflex_google_auth import google_login, google_oauth_provider

def page():
    return rx.div(
        google_oauth_provider(
            google_login(),
        ),
    )

To uniquely identify a user, the GoogleAuthState.tokeninfo['sub'] field can be used.

See the example in masenf/rx_shout for how to integrate an authenticated Google user with other app-specific user data.

Customizing the Button

If you want to use your own login button, you may use whatever component you like, as long as it is wrapped in a reflex_google_auth.google_oauth_provider component and the on_click triggers reflex_google_auth.handle_google_login(). Note that this cannot be combined with other event handlers.

This functionality is also exposed in the require_google_auth decorator, which accepts a button keyword argument.

When using a custom button, the returned auth-code must be validated on the backend, which is handled by this library, but requires additionally setting GOOGLE_CLIENT_SECRET and GOOGLE_REDIRECT_URI environment variables. These can be configured in the Google Cloud Console as described above.

from reflex_google_auth import handle_google_login, require_google_login, GoogleAuthState


@require_google_login(button=rx.button("Google Login 🚀", on_click=handle_google_login()))
def custom_button() -> rx.Component:
    return rx.vstack(
        f"{GoogleAuthState.tokeninfo['email']} clicked a custom button to login, nice",
    )

Requesting Scopes

By default, only the basic profile scopes are requested. To request additional scopes for accessing other Google APIs in the context of the authenticated user:

  1. Update your app registration in the Google Cloud Console to add the additional scopes you want to request.
  2. Pass the scope parameter to handle_google_login event handler. NOTE: scopes can only be requested with the auth-code flow, so you must set GOOGLE_CLIENT_SECRET and GOOGLE_REDIRECT_URI environment variables as described above.

See the custom_scope example in google_auth_demo.py for how to request Drive scopes and and then use Google Drive API to store and retrieve app data.

Changelog

See CHANGELOG.md.

Contributing

Every pull request that changes custom_components/ needs a news fragment — a short markdown file under news/, named <pr-number>.<type>.md, written for someone reading release notes:

uvx --from "reflex-release @ git+https://github.com/reflex-dev/reflex@1613d415c8d443cdd81d097fd08ae02213dfe04f#subdirectory=packages/reflex-release" \
  reflex-release create 123.bugfix.md

The types are breaking, deprecation, feature, bugfix, performance, docs and misc. Before the PR number is known, name the file +something.bugfix.md and rename it later. The skip-changelog label waives the requirement for changes that are not user-facing.

Do not edit CHANGELOG.md by hand: a new version heading on main is what triggers a publish, so CI rejects headings that were not written by the release workflow.

Releasing

Releases are changelog-driven, via reflex-release.

  1. Run Dispatch release from the Actions tab and pick an action (release-patch/-minor/-major, release-post, or one of the prerelease actions). It runs towncrier over news/, writes the new CHANGELOG.md section, and opens a pull request.
  2. Merge that pull request. The push to main is what publishes: Release from changelog finds the changelog version that has no git tag, builds it, and waits for approval on the pypi environment before uploading.
  3. After a successful upload the tag and GitHub release are created, and Deploy demo app redeploys google_auth_demo against the new version.

Because tags are only created after a successful upload, a failed release is retried by pushing a fix on top of the changelog bump — there is nothing to clean up.

Upgrading the release tooling is a cli-command bump in pyproject.toml followed by reflex-release sync; the pull-request check fails while the generated workflows are out of date.

Metadata

Release files for reflex-google-auth 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reflex-google-auth 0.2.1
File Size Uploaded Provenance Actions
reflex_google_auth-0.2.1.tar.gz 25.9 kB Without provenance Details for reflex_google_auth-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for reflex-google-auth 0.2.1
File Interpreter ABI Platform Actions
reflex_google_auth-0.2.1-py3-none-any.whl Python 3 none any Details for reflex_google_auth-0.2.1-py3-none-any.whl

Total release size: 34.2 kB

Release files / reflex_google_auth-0.2.1.tar.gz

Download URL reflex_google_auth-0.2.1.tar.gz
Size 25.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6abe62663b1ec9dfdc0df882adc42725d9a6849db7d8e116cf1e0baa3c99c2ca
BLAKE2b-256 checksum
How to use checksums
0b2f81edce035e49b30a95c14ab98df383490187f8552fe0b7238105fc4354dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / reflex_google_auth-0.2.1-py3-none-any.whl

Download URL reflex_google_auth-0.2.1-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8b47e8990e30f729ca6ef25e94d3f17aa52f05a769914a20a443bc36b2d126e
BLAKE2b-256 checksum
How to use checksums
255165bca7ba8425a75dc2c4dadad40fcc2f955e14da4a6c8c48fddfb5888125
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for reflex-google-auth 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page