Skip to content

refactor: inject localStorage via Angular DI in JwtService#20

Open
devin-ai-integration[bot] wants to merge 2 commits intomainfrom
devin/1777676547-jwt-service-di-refactor
Open

refactor: inject localStorage via Angular DI in JwtService#20
devin-ai-integration[bot] wants to merge 2 commits intomainfrom
devin/1777676547-jwt-service-di-refactor

Conversation

@devin-ai-integration
Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot commented May 1, 2026

Summary

Refactors JwtService to receive localStorage through Angular's dependency injection system instead of accessing window.localStorage directly. This makes the service testable without monkey-patching window.

Changes:

  • New LOCAL_STORAGE InjectionToken (storage.token.ts): providedIn: 'root' with a factory that returns localStorage. No changes needed in app.config.ts.
  • JwtService now inject(LOCAL_STORAGE) and uses proper Storage API methods (getItem/setItem/removeItem) instead of bracket notation.
  • jwt.service.spec.ts rewritten to provide a Map-backed mock Storage via { provide: LOCAL_STORAGE, useValue: mockStorage } — no more Object.defineProperty(window, 'localStorage', ...).
  • Added zone.js dev dependency — was already imported in test setup but missing from package.json, causing all test suites to fail.
  • Pinned @angular/core back to 21.1.1 to match all other @angular/* packages (was accidentally bumped to 21.2.4 by a previous dependabot PR).

Review & Testing Checklist for Human

  • Verify getToken() now returns '' (empty string) instead of undefined when no token is stored — all existing callers use truthiness checks so this is backward-compatible, but worth confirming no downstream code relies on undefined specifically
  • Run bun run test to confirm all 179 tests pass
  • Smoke-test login/logout flow in the browser to verify token persistence works end-to-end

Notes

  • The user.service.spec.ts already mocked JwtService entirely via { provide: JwtService, useValue: ... }, so it required no changes.

Link to Devin session: https://app.devin.ai/sessions/6c4b7428c4f54351919bb91211361338
Requested by: @WesternConcrete


Devin Review

Status Commit
⚪ Not started

Run Devin Review

💡 Connect your GitHub account to enable automatic code reviews.

Open in Devin Review (Staging)
Open in Devin Review

- Create LOCAL_STORAGE InjectionToken backed by localStorage
- Refactor JwtService to inject Storage via the token instead of
  accessing window.localStorage directly
- Use proper Storage API (getItem/setItem/removeItem) instead of
  bracket notation
- Rewrite tests to provide a mock Storage through TestBed DI rather
  than monkey-patching window.localStorage
- Add zone.js dev dependency (required by test setup but was missing)

Co-Authored-By: Wes Convery <2wconvery@gmail.com>
@devin-ai-integration
Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

devin-ai-integration[bot]

This comment was marked as resolved.

Reverts an accidental version mismatch where @angular/core was at
21.2.4 while all other @angular/* packages remained at 21.1.1.

Co-Authored-By: Wes Convery <2wconvery@gmail.com>
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