-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs: Sign in with Google auth recipe #6787
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
Draft
amsraman
wants to merge
3
commits into
main
Choose a base branch
from
docs/google-auth-recipe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+127
−0
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| ```python exec | ||
| import reflex as rx | ||
| ``` | ||
|
|
||
| # Sign in with Google | ||
|
|
||
| Google authentication is added with the `reflex-google-auth` package. Manage your OAuth2 credentials (`GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET`) in the [Google Cloud credentials console](https://console.developers.google.com/apis/credentials). | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install reflex-google-auth | ||
| ``` | ||
|
|
||
| ## Environment variables | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| - `GOOGLE_CLIENT_ID` | ||
| - `GOOGLE_CLIENT_SECRET` | ||
|
|
||
| You do not need to check the client ID or client secret yourself — set them in the environment. | ||
|
|
||
| ## Integrate with your Reflex app | ||
|
|
||
| Subclass `GoogleAuthState`. It provides a `token_is_valid` var that should be checked before returning any protected content, and a `tokeninfo` dict that contains the user's profile information. | ||
|
|
||
| ```python | ||
| from reflex_google_auth import GoogleAuthState | ||
|
|
||
|
|
||
| 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." | ||
| ``` | ||
|
|
||
| To uniquely identify a user, use `GoogleAuthState.tokeninfo['sub']`. | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| ## Displaying the login button | ||
|
|
||
| The `require_google_login` decorator wraps an existing component and shows 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: | ||
|
|
||
| ```python | ||
| from reflex_google_auth import require_google_login | ||
|
|
||
|
|
||
| @require_google_login | ||
| def protected_page(): | ||
| return rx.text("This content is only shown to authenticated users.") | ||
| ``` | ||
|
|
||
| The "Sign in with Google" button can also be displayed directly via `google_login()`, wrapped in a `google_oauth_provider`: | ||
|
|
||
| ```python | ||
| from reflex_google_auth import google_login, google_oauth_provider | ||
|
|
||
|
|
||
| def page(): | ||
| return rx.el.div( | ||
| google_oauth_provider( | ||
| google_login(), | ||
| ), | ||
| ) | ||
| ``` | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| ## API reference | ||
|
|
||
| The following API is provided by `reflex_google_auth`: | ||
|
|
||
| ```python | ||
| class TokenCredential(TypedDict, total=False): | ||
| iss: str | ||
| azp: str | ||
| aud: str | ||
| sub: str | ||
| hd: str | ||
| email: str | ||
| email_verified: bool | ||
| nbf: int | ||
| name: str | ||
| picture: str | ||
| given_name: str | ||
| family_name: str | ||
| iat: int | ||
| exp: int | ||
| jti: str | ||
|
|
||
|
|
||
| class GoogleAuthState(rx.State): | ||
| id_token_json: str = rx.LocalStorage() | ||
|
|
||
| @rx.var(cache=True) | ||
| def client_id(self) -> str: ... | ||
|
|
||
| @rx.var(cache=True) | ||
| def tokeninfo(self) -> TokenCredential: ... | ||
|
|
||
| @rx.event | ||
| def logout(self): ... | ||
|
|
||
| @rx.var(cache=False) | ||
| def token_is_valid(self) -> bool: ... | ||
|
|
||
| @rx.var(cache=True) | ||
| def user_name(self) -> str: ... | ||
|
|
||
| @rx.var(cache=True) | ||
| def user_email(self) -> str: ... | ||
|
|
||
|
|
||
| # Shows the "Sign in with Google" button. | ||
| def google_login(**props) -> rx.Component: ... | ||
|
|
||
|
|
||
| # Provides the Google OAuth context; wrap `google_login()` in this. | ||
| def google_oauth_provider(*children, **props) -> rx.Component: ... | ||
|
|
||
|
|
||
| # Decorator that gates a page/component behind Google login. | ||
| def require_google_login( | ||
| page: Callable[[], rx.Component] | None = None, | ||
| *, | ||
| button: rx.Component | None = None, | ||
| ) -> Callable: ... | ||
| ``` | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docs/app/reflex_docs/templates/docpage/sidebar/sidebar_items/learn.pyhard-codes the Authentication section with onlyauthentication.authentication_overview. The newgoogle.mdpage will be accessible by direct URL (the docgen pipeline auto-discovers all.mdfiles) but will not appear in the sidebar navigation. The PR description flags this as a pending TODO, but it blocks discoverability — the page is unreachable for users browsing the docs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Added
authentication.googleto the Authentication section's children inlearn.pyso the page appears in the sidebar nav (naming matches the auto-discovered module, same convention asauthentication.authentication_overview).