Skip to content

Studio: Refactor AuthProvider into Redux slice#2778

Closed
katinthehatsite wants to merge 32 commits into
trunkfrom
fix/refactor-auth-provider-in-redux-slice
Closed

Studio: Refactor AuthProvider into Redux slice#2778
katinthehatsite wants to merge 32 commits into
trunkfrom
fix/refactor-auth-provider-in-redux-slice

Conversation

@katinthehatsite

@katinthehatsite katinthehatsite commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

Related issues

Fixes https://linear.app/a8c/issue/STU-1379/studio-refactor-authprovider-into-redux-slice

How AI was used in this PR

I used it in the plan mode to plan the refactor, then gave feedback on the plan and used it for implementation, making further manual corrections.

Proposed Changes

This PR refactors AuthProvider into Redux slice.

Testing Instructions

  • Pull the changes from this branch
  • Start Studio with npm start
  • Log out of Studio and confirm that you are seeing logged out view
  • Log in and confirm it works as expected
  • Confirm you are correctly seeing the features that should be available with WP.com logged in view
  • Confirm tests are passing
  • Close the app while logged in
  • Open ~/Library/Application Support/Studio/appdata-v1.json
  • Delete the authToken
  • Save the changes
  • Start the app with again
  • Confirm that you can see logged out view

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@katinthehatsite katinthehatsite self-assigned this Mar 12, 2026
@katinthehatsite katinthehatsite marked this pull request as draft March 12, 2026 13:20
Comment thread apps/studio/src/stores/provider-constants-slice.ts Outdated
@katinthehatsite katinthehatsite marked this pull request as ready for review March 12, 2026 15:06
@katinthehatsite katinthehatsite requested a review from a team March 12, 2026 15:15

@fredrikekelund fredrikekelund left a comment

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.

Thanks for tackling this, @katinthehatsite.

The end goal with this refactor should be to remove AuthProvider altogether. That, and doing something about setWpcomClient from src/stores/wpcom-api are the two big changes needed here, IMO.

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.

We should remove this context provider altogether. Components can easily consume Redux state and dispatch actions. There's no point in keeping this provider as a thin wrapper around the Redux slice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should be done ✅

Comment thread apps/studio/src/stores/auth-slice.ts Outdated
Comment on lines +189 to +190
export const selectIsAuthenticated = ( state: RootState ) => state.auth.isAuthenticated;
export const selectUser = ( state: RootState ) => state.auth.user ?? undefined;

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.

Suggested change
export const selectIsAuthenticated = ( state: RootState ) => state.auth.isAuthenticated;
export const selectUser = ( state: RootState ) => state.auth.user ?? undefined;
export const authThunks = {
handleInvalidToken,
initializeAuth,
authTokenReceived,
authLogout,
};
export const authSelectors = {
selectIsAuthenticated: ( state: RootState ) => state.auth.isAuthenticated,
selectUser: ( state: RootState ) => state.auth.user ?? undefined,
};

We've followed this pattern in some other Redux slices. I think it's helpful to namespace selectors, actions, and thunks this way.

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.

This isn't exactly a blocker, but I still think it's good practice.

Comment thread apps/studio/src/stores/auth-slice.ts Outdated
Comment thread apps/studio/src/stores/auth-slice.ts Outdated
Comment thread apps/studio/src/components/auth-provider.tsx Outdated
Comment thread apps/studio/src/components/auth-provider.tsx Outdated
@wpmobilebot

wpmobilebot commented Mar 19, 2026

Copy link
Copy Markdown
Collaborator

📊 Performance Test Results

Comparing f2c6f44 vs trunk

app-size

Metric trunk f2c6f44 Diff Change
App Size (Mac) 1451.96 MB 1907.45 MB +455.48 MB 🔴 31.4%

site-editor

Metric trunk f2c6f44 Diff Change
load 1815 ms 1810 ms 5 ms ⚪ 0.0%

site-startup

Metric trunk f2c6f44 Diff Change
siteCreation 8079 ms 8088 ms +9 ms ⚪ 0.0%
siteStartup 4948 ms 4960 ms +12 ms ⚪ 0.0%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff)

Kateryna Kodonenko added 11 commits March 19, 2026 15:25
Port removal of noisy Sentry.captureException calls from auth-provider.tsx
to auth-slice.ts (handleInvalidToken, authLogout token revoke, authLogout
clearAuthenticationToken), keeping the intentional deletion of auth-provider.tsx.

Made-with: Cursor
@katinthehatsite katinthehatsite requested review from a team and fredrikekelund April 8, 2026 09:59
@katinthehatsite

Copy link
Copy Markdown
Contributor Author

I think all the comments have now been addressed so I am requesting another review on this. 🙇

@fredrikekelund fredrikekelund left a comment

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.

Sorry for the delayed review, @katinthehatsite. This is looking pretty good overall! 👍

The biggest remaining question to me is use-auth.ts, which I argue we should also remove. Beyond that, I left a couple of comments about plumbing/wiring and raised a few comments from my previous review that you didn't address or push back on.

Comment thread apps/studio/src/hooks/use-auth.ts Outdated

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.

Same as with apps/studio/src/components/auth-provider.tsx, I argue we should remove this file. Components can easily consume the selectors used in this hook directly, and dispatch the authLogout action directly, too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I went ahead and removed it 👍

Comment thread apps/studio/src/stores/auth-slice.ts Outdated
Comment on lines +189 to +190
export const selectIsAuthenticated = ( state: RootState ) => state.auth.isAuthenticated;
export const selectUser = ( state: RootState ) => state.auth.user ?? undefined;

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.

This isn't exactly a blocker, but I still think it's good practice.

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.

Could we make the wpcomClient part of the auth slice? Either part of the state or keep using the current logic, but export getWpcomClient. This is what I was getting at in #2778 (comment), too

Comment thread apps/studio/src/stores/auth-slice.ts Outdated
Comment thread apps/studio/src/stores/index.ts Outdated
Comment thread apps/studio/src/stores/auth-slice.ts Outdated
Comment thread apps/studio/src/stores/tests/auth-slice.test.ts Outdated
Comment thread apps/studio/src/stores/auth-slice.ts Outdated
@katinthehatsite

Copy link
Copy Markdown
Contributor Author

Thanks so much for the review @fredrikekelund 🙇

I am planning to take a closer look today or tomorrow and address the rest of the comments 😃

@katinthehatsite

Copy link
Copy Markdown
Contributor Author

I think this should be ready for another review @fredrikekelund

@wojtekn

wojtekn commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

@katinthehatsite should we merge trunk in and solve conflicts?

@fredrikekelund

Copy link
Copy Markdown
Contributor

@katinthehatsite, given that we are cutting over to the agentic UI soon and that landing this would still require a non-trivial amount of work, I suggest abandoning this PR and closing the related issue.

@katinthehatsite

Copy link
Copy Markdown
Contributor Author

@katinthehatsite, given that we are cutting over to the agentic UI soon and that landing this would still require a non-trivial amount of work, I suggest abandoning this PR and closing the related issue.

This sounds good to me 👍 I will proceed with closing

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.

4 participants