Skip to content

Studio: Refactor AuthProvider into Redux slice#2778

Open
katinthehatsite wants to merge 19 commits intotrunkfrom
fix/refactor-auth-provider-in-redux-slice
Open

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

Conversation

@katinthehatsite
Copy link
Copy Markdown
Contributor

@katinthehatsite katinthehatsite commented Mar 12, 2026

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
@@ -1,4 +1,4 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
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.

This is not related directly to changes in this PR. This is something that my linter flagged that needed to be fixed and was present on trunk

@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
Copy link
Copy Markdown
Contributor

@fredrikekelund fredrikekelund left a comment

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 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.

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
Copy link
Copy Markdown
Collaborator

wpmobilebot commented Mar 19, 2026

📊 Performance Test Results

Comparing 706793e vs trunk

app-size

Metric trunk 706793e Diff Change
App Size (Mac) 1252.04 MB 1252.05 MB +0.01 MB ⚪ 0.0%

site-editor

Metric trunk 706793e Diff Change
load 1574 ms 1575 ms +1 ms ⚪ 0.0%

site-startup

Metric trunk 706793e Diff Change
siteCreation 9104 ms 9121 ms +17 ms ⚪ 0.0%
siteStartup 4153 ms 4172 ms +19 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. 🙇

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.

3 participants