Skip to content

refactor: introduce AuthClient abstraction (Phase 1 of #64) - #65

Open
xiaomi7732 wants to merge 2 commits into
mainfrom
feature/auth-abstraction
Open

refactor: introduce AuthClient abstraction (Phase 1 of #64)#65
xiaomi7732 wants to merge 2 commits into
mainfrom
feature/auth-abstraction

Conversation

@xiaomi7732

Copy link
Copy Markdown
Owner

Summary

Phase 1 of #64 — refactor MSAL auth behind an AuthClient interface so future providers (e.g. Google Identity Services for the Sheets backend) can plug in without touching consumers.

Strict refactor. Zero behavior change. Microsoft remains the only provider. This is the matching auth-side companion to the storage abstraction in #63.

Architecture

lib/
  auth/
    types.ts                          # AuthClient interface, AuthProvider, AuthUser
    useAuthClient.ts                  # Hook returning the active client
    microsoft/
      MicrosoftAuthClient.ts          # Wraps MSAL

Interface

export interface AuthClient {
  readonly provider: AuthProvider;     // 'microsoft' today
  readonly isAuthenticated: boolean;
  readonly busy: boolean;
  acquireToken(options?: { silentOnly?: boolean }): Promise<string>;
  login(): Promise<void>;
  logout(): Promise<void>;
  getUser(): AuthUser | null;
}

Implementation notes

  • acquireToken reads accounts fresh from instance.getAllAccounts() rather than from useMsal-captured state. This eliminates the post-loginPopup race that PR refactor: introduce storage abstraction layer behind TodoStore #63 worked around with a one-shot store. The hack is gone — app/page.tsx now calls store.listBooks() directly after auth.login().

  • acquireToken({ silentOnly: true }) for background checks. Replaces the instance.acquireTokenSilent direct call in app/page.tsx's availability check. Never opens a popup from a useEffect.

  • getUser() sources displayName/email from MSAL account info instead of calling /me on Graph. Books page no longer makes a round-trip just to render the welcome banner.

Removed

  • lib/hooks/useGraphToken.ts — logic absorbed into MicrosoftAuthClient.
  • getUserInfo + UserInfo from graphService.ts — no longer used.

Changed consumers

All pages and hooks that previously imported useGraphToken now use useAuthClient:

  • app/page.tsx (uses silentOnly for background check)
  • app/books/page.tsx (uses auth.getUser() and auth.logout())
  • app/matrix/page.tsx, app/scrum/page.tsx, app/cancelled/page.tsx
  • lib/hooks/useBookId.ts, lib/store/useStore.ts
  • components/HamburgerMenu.tsx (uses auth.logout() instead of instance.logoutPopup)

Build + lint

  • npm run build
  • npm run lint shows no new errors/warnings (pre-existing ones unchanged)

Out of scope

  • No new providers, no Google Identity Services integration — that's Phase 2.
  • No UI changes.
  • No new dependencies.

Smoke test checklist (manual)

  • Sign in with existing Microsoft account — works as before
  • Existing ?bookId=... URLs still resolve
  • CRUD operations on Matrix / Scrum / Cancelled pages
  • Session sweep still runs
  • Sign out — landing page returns to unauthenticated state
  • Sign back in — same account loads books

Refactor MSAL auth behind an AuthClient interface so future providers
(e.g. Google Identity Services) can plug in without touching consumers.
Strict refactor — zero behavior change. Microsoft remains the only
provider.

Architecture:
- lib/auth/types.ts: AuthClient interface (acquireToken with silentOnly
  option, login, logout, getUser, isAuthenticated, busy)
- lib/auth/microsoft/MicrosoftAuthClient.ts: wraps MSAL (loginPopup,
  acquireTokenSilent+Popup, logoutPopup, accounts[0])
- lib/auth/useAuthClient.ts: hook returning the active client (today
  always Microsoft)

Implementation notes:
- acquireToken reads accounts fresh from instance.getAllAccounts() rather
  than from useMsal-captured state, eliminating the post-loginPopup race
  identified in PR #63. The one-shot store hack in app/page.tsx is no
  longer needed.
- getUser sources displayName and email from MSAL account info instead
  of calling Graph /me. Removes a wasted round-trip on the books page.

Removed:
- lib/hooks/useGraphToken.ts (logic absorbed into MicrosoftAuthClient)
- getUserInfo + UserInfo type from graphService.ts (no longer used)

Other:
- All pages and useBookId now use useAuthClient instead of useGraphToken.
- HamburgerMenu signout uses auth.logout() instead of instance.logoutPopup
  directly.
- app/page.tsx silent background check uses acquireToken({ silentOnly: true })
  instead of constructing a one-shot MultiBackendStore.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 16, 2026 04:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Introduces an AuthClient abstraction around the existing MSAL authentication flow as Phase 1 of #64, keeping Microsoft as the only provider while preparing consumers for future auth providers.

Changes:

  • Adds AuthClient types and a Microsoft MSAL-backed implementation.
  • Replaces useGraphToken usage across pages, hooks, store creation, and menu logout.
  • Removes direct Graph /me user info access and deleted the old token hook.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/arrange-v4/lib/auth/types.ts Defines the new auth abstraction contract.
src/arrange-v4/lib/auth/useAuthClient.ts Exposes the active auth client hook.
src/arrange-v4/lib/auth/microsoft/MicrosoftAuthClient.ts Implements AuthClient using MSAL.
src/arrange-v4/lib/hooks/useGraphToken.ts Removes the old MSAL token hook.
src/arrange-v4/lib/store/useStore.ts Injects token acquisition through AuthClient.
src/arrange-v4/lib/hooks/useBookId.ts Uses auth abstraction for authentication state.
src/arrange-v4/lib/graphService.ts Removes user-info Graph helper.
src/arrange-v4/components/HamburgerMenu.tsx Routes sign-out through AuthClient.
src/arrange-v4/app/page.tsx Uses AuthClient for login and silent availability checks.
src/arrange-v4/app/books/page.tsx Uses AuthClient for login/logout and user display.
src/arrange-v4/app/matrix/page.tsx Uses AuthClient in matrix page auth flow.
src/arrange-v4/app/scrum/page.tsx Uses AuthClient in scrum page auth flow.
src/arrange-v4/app/cancelled/page.tsx Uses AuthClient in cancelled page auth flow.

Comment thread src/arrange-v4/lib/auth/microsoft/MicrosoftAuthClient.ts Outdated
The previous getUser() captured useMsal's accounts at hook render time,
so a caller doing await auth.login(); auth.getUser() would get stale
pre-login state for one render cycle. Read from instance.getAllAccounts()
fresh — same fix as acquireToken.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated no new comments.

@xiaomi7732

Copy link
Copy Markdown
Owner Author

PR Review Loop Summary

  • 2 review rounds completed
  • 1 issue raised — fixed:
    • Round 1: getUser() captured useMsal's accounts at hook render time, reintroducing the same stale-state race acquireToken() avoids. Fixed to read instance.getAllAccounts() fresh.
    • Round 2: Clean — zero unresolved comments ✅
  • Final status: All review threads resolved

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.

2 participants