Skip to content

Conversation

@rafalkrupinski
Copy link

@rafalkrupinski rafalkrupinski commented Nov 11, 2025

Description

Small cleanup of social login, so that it's easier to understand and port to litestar-fullstack

  • removed unused code
  • extracted auth completion - the differences between github and google are abstracted

Summary by Sourcery

Refactor social login flows to eliminate duplicate code and unify GitHub and Google login completion by introducing a shared helper that handles the OAuth2 flow and user registration.

Enhancements:

  • Extract common OAuth2 completion logic into a shared _auth_complete method
  • Simplify GitHub and Google login endpoints to delegate to the shared completion helper
  • Remove unused services and import dependencies from social login controllers
  • Abstract provider-specific differences behind a BaseOAuth2 interface

@sourcery-ai
Copy link

sourcery-ai bot commented Nov 11, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors social login flows by extracting the common OAuth2 completion steps into a shared helper, removing outdated role-assignment code, and standardizing client typing and logging for both GitHub and Google providers.

Sequence diagram for unified OAuth2 social login completion

sequenceDiagram
actor User
participant "Request"
participant "RegistrationController"
participant "OAuth2 Client (GitHub/Google)"
participant "UserService"
participant "Dashboard"
User->>Request: Initiate social login (GitHub/Google)
Request->>RegistrationController: Complete login
RegistrationController->>"OAuth2 Client (GitHub/Google)": get_id_email(token)
"OAuth2 Client (GitHub/Google)"-->>RegistrationController: id, email
RegistrationController->>UserService: get_or_upsert(email)
UserService-->>RegistrationController: user, created
RegistrationController->>Request: set_session(user_id)
RegistrationController->>Request: log auth request
alt New user
    RegistrationController->>Request: log user creation
    RegistrationController->>Request: flash welcome message
end
RegistrationController->>Dashboard: Redirect to dashboard
Loading

Class diagram for updated RegistrationController social login methods

classDiagram
class RegistrationController {
  +github_complete(request, access_token_state, users_service) : InertiaRedirect
  +google_complete(request, access_token_state, users_service) : InertiaRedirect
  +_auth_complete(request, access_token_state, users_service, oauth_client) : InertiaRedirect
}
class BaseOAuth2 {
  +get_id_email(token)
  +name
}
class UserService {
  +get_or_upsert(match_fields, email, is_verified, is_active)
  +to_schema(user, schema_type)
}
RegistrationController --> BaseOAuth2 : uses
RegistrationController --> UserService : uses
Loading

File-Level Changes

Change Details Files
Extract shared OAuth2 completion steps into a new helper
  • Added static _auth_complete method to encapsulate token exchange, user upsert, session set, flash, and redirect
  • Updated github_complete to delegate to _auth_complete
  • Updated google_complete to delegate to _auth_complete
app/domain/accounts/controllers.py
Remove unused role and OAuth account logic
  • Dropped roles_service and UserOAuthAccountService parameters
  • Removed slugify-based role lookup and role assignment code
app/domain/accounts/controllers.py
Unify OAuth2 client interface
  • Imported BaseOAuth2 for generic typing of OAuth clients
  • Replaced concrete client types in method signatures with BaseOAuth2
app/domain/accounts/controllers.py
Standardize logging and metadata
  • Consolidated provider-specific logging into a single log message with provider name
  • Removed redundant log messages
app/domain/accounts/controllers.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • The removal of roles_service means you’ve dropped default role assignment—ensure this logic is intentionally moved or reimplemented in the new _auth_complete flow.
  • By removing oauth_account_service, the OAuth account linking step is lost—confirm that users_service.get_or_upsert now handles linking or re-add that logic.
  • You’re logging with oauth_client.name—verify that BaseOAuth2.client provides a name property, or explicitly pass the provider identifier to avoid undefined values.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The removal of roles_service means you’ve dropped default role assignment—ensure this logic is intentionally moved or reimplemented in the new _auth_complete flow.
- By removing oauth_account_service, the OAuth account linking step is lost—confirm that users_service.get_or_upsert now handles linking or re-add that logic.
- You’re logging with oauth_client.name—verify that BaseOAuth2.client provides a `name` property, or explicitly pass the provider identifier to avoid undefined values.

## Individual Comments

### Comment 1
<location> `app/domain/accounts/controllers.py:198` </location>
<code_context>
         )
         request.set_session({"user_id": user.email})
-        request.logger.info("google auth request", id=_id, email=email)
+        request.logger.info("auth request complete", id=id_, email=email, provider=oauth_client.name)
         if created:
             request.logger.info("created a new user", id=user.id)
</code_context>

<issue_to_address>
**issue (bug_risk):** Provider name is logged using oauth_client.name, which may not be present on all BaseOAuth2 implementations.

Verify that all BaseOAuth2 subclasses define the 'name' attribute, or use getattr with a default value to prevent runtime errors.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@rafalkrupinski rafalkrupinski changed the title Social login cleanup refactor: social login cleanup Nov 11, 2025
@rafalkrupinski
Copy link
Author

I'd be happy to add oauth2 state + PKCE - it's simple enough with a session, but it's out of scope in a cleanup PR and I don't really want to set up GCP to test it (GitHub would be simpler)

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.

1 participant