Skip to content

Desktop: Bring app to foreground after OAuth redirect - #5787

Closed
beastoin wants to merge 1 commit into
mainfrom
fix/desktop-oauth-refocus
Closed

beastoin wants to merge 1 commit into
mainfrom
fix/desktop-oauth-refocus

Conversation

@beastoin

Copy link
Copy Markdown
Collaborator

Summary

  • After OAuth login via Safari, the desktop app stayed in the background while Safari displayed "Authentication Successful"
  • Adds NSApp.activate(ignoringOtherApps: true) after handling the OAuth callback URL in handleGetURLEvent
  • App now immediately comes to foreground after the auth redirect

Fixes #5784 (friction #7)

How it works

When macOS receives the custom URL scheme callback (omidesktop://auth/callback), handleGetURLEvent fires. After passing the URL to AuthService.handleOAuthCallback(), the app now calls NSApp.activate(ignoringOtherApps: true) to bring itself to the foreground — the same pattern used throughout the app (27 existing call sites).

Test plan

  • Sign out → sign in via Google OAuth → verify app comes to foreground after Safari redirect
  • Verify Safari "Authentication Successful" page doesn't stay in front

by AI for @beastoin

🤖 Generated with Claude Code

After the OAuth redirect from Safari, the desktop app stayed in the
background while Safari displayed "Authentication Successful". Now
NSApp.activate(ignoringOtherApps:true) brings the app to foreground
immediately after receiving the callback URL.

Fixes #5784 (friction #7)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@greptile-apps

greptile-apps Bot commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a UX friction point where the desktop app remained in the background after completing an OAuth login via Safari. A single line — NSApp.activate(ignoringOtherApps: true) — is added inside the existing Task { @MainActor in } block in handleGetURLEvent, immediately after AuthService.shared.handleOAuthCallback(url:) returns.

Key observations:

  • The fix is minimal and correct for the primary flow: app receives its own OAuth callback → processes it → comes to foreground.
  • handleOAuthCallback is synchronous and @MainActor, so the activation call is correctly sequenced after the callback is fully processed.
  • The change follows the existing pattern used at 27 other call sites in the codebase.
  • Edge case: handleOAuthCallback has a forwarding path (lines 512–520 in AuthService.swift) for when the callback is destined for a different bundle identifier (e.g., a parallel dev build). In that path, forwardOAuthCallback launches/activates the correct target app asynchronously via NSWorkspace.shared.open(_:withApplicationAt:configuration:). Because NSApp.activate is called unconditionally after the synchronous return, the current (wrong) app briefly steals focus before the target app is eventually activated. While the correct app ultimately wins focus, there is a visible transient flash of the wrong app coming to the front. Returning a Bool from handleOAuthCallback to indicate whether the callback was locally consumed would cleanly prevent this.

Confidence Score: 4/5

  • Safe to merge for the primary OAuth flow; minor unconditional activation issue only affects the rare multi-build forwarding edge case.
  • The change is a single, well-understood AppKit call that follows an established pattern in the codebase. It correctly fixes the described issue for all standard users. The only concern is the unconditional call when handleOAuthCallback silently forwards the URL to a different bundle — this causes a brief focus flash in an edge case that only affects developers running multiple builds side-by-side, not production users.
  • desktop/Desktop/Sources/OmiApp.swift — review the unconditional NSApp.activate in the context of the forwarding path in AuthService.handleOAuthCallback.

Important Files Changed

Filename Overview
desktop/Desktop/Sources/OmiApp.swift Adds NSApp.activate(ignoringOtherApps: true) inside handleGetURLEvent after the OAuth callback is processed. Fix is correct for the primary case but activates unconditionally, including when the callback is forwarded to a different bundle ID.

Sequence Diagram

sequenceDiagram
    participant Safari
    participant macOS
    participant AppDelegate (OmiApp)
    participant AuthService

    Safari->>macOS: Open omidesktop://auth/callback?code=...
    macOS->>AppDelegate (OmiApp): handleGetURLEvent(_:withReplyEvent:)
    AppDelegate (OmiApp)->>AppDelegate (OmiApp): Task { @MainActor in }
    AppDelegate (OmiApp)->>AuthService: handleOAuthCallback(url:)
    alt callback is for this bundle
        AuthService->>AuthService: resume oauthContinuation
        AuthService-->>AppDelegate (OmiApp): return (handled)
        AppDelegate (OmiApp)->>AppDelegate (OmiApp): NSApp.activate(ignoringOtherApps: true) ✅
    else callback is for a different bundle
        AuthService->>macOS: NSWorkspace.open(url, withApplicationAt: targetApp) [async, activates: true]
        AuthService-->>AppDelegate (OmiApp): return (forwarded)
        AppDelegate (OmiApp)->>AppDelegate (OmiApp): NSApp.activate(ignoringOtherApps: true) ⚠️ wrong app activated
        macOS-->>macOS: [later] target app activates and takes focus
    end
Loading

Last reviewed commit: "Activate app after O..."

Comment on lines 1037 to 1041
Task { @MainActor in
AuthService.shared.handleOAuthCallback(url: url)
// Bring app to foreground after OAuth redirect — Safari stays in front otherwise
NSApp.activate(ignoringOtherApps: true)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Unconditional activation on forwarded OAuth callbacks

NSApp.activate(ignoringOtherApps: true) is called unconditionally even when handleOAuthCallback detects the callback is for a different bundle identifier and forwards it (via forwardOAuthCallback, which already sets config.activates = true on the target app).

The call sequence in that scenario becomes:

  1. handleOAuthCallback dispatches NSWorkspace.shared.open(_:withApplicationAt:configuration:) asynchronously to activate the target bundle.
  2. handleOAuthCallback returns synchronously.
  3. NSApp.activate(ignoringOtherApps: true) fires immediately — bringing the wrong app to the front.
  4. Later, the async NSWorkspace.open completion activates the correct target bundle, displacing focus again.

This causes a visible flash of the wrong app and an unnecessary focus steal, even though the forwarding path ultimately wins. Consider having handleOAuthCallback return a Bool indicating whether this instance actually consumed the callback, and only activate when true:

Task { @MainActor in
    let handled = AuthService.shared.handleOAuthCallback(url: url)
    if handled {
        NSApp.activate(ignoringOtherApps: true)
    }
}

@github-actions

Copy link
Copy Markdown
Contributor

Hey @beastoin 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our roadmap and vision

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community! 💜

@undivisible
undivisible deleted the fix/desktop-oauth-refocus branch August 20, 2026 11:34
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.

Desktop app: onboarding and local dev friction points

1 participant