Skip to content

refactor: reorganize auth-client code structure#84

Merged
sea-snake merged 3 commits intomainfrom
sea-snake/cleanup
Apr 16, 2026
Merged

refactor: reorganize auth-client code structure#84
sea-snake merged 3 commits intomainfrom
sea-snake/cleanup

Conversation

@sea-snake
Copy link
Copy Markdown
Contributor

@sea-snake sea-snake commented Apr 7, 2026

Reorganizes auth-client.ts for clarity:

  • Class moved before free functions (public API is the first thing you see)
  • Within the class: constructor → public methods → private methods
  • Free functions ordered: key helpers → chain helpers → storage helpers → migration
  • Removed unused #key field
  • Renamed #createOptions to #options
  • Simplified #hydrate() — delegates key restoration and migration to restoreKey() which calls migrateFromLocalStorage() as a fallback
  • Cleaned up JSDoc throughout
  • Removed localStorage try/catch wrappers (assumed always available)
  • Disabled pnpm audit CI workflow due to pnpm/pnpm#11265

Prev: #83 | Next: #85

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors the auth-client.ts file for improved code clarity and maintainability. The changes reorganize the AuthClient class structure, moving the class before free functions and reordering code within the class (constructor → public methods → private methods). The PR also simplifies the hydration logic by delegating key restoration to a unified restoreKey() function, removes localStorage error handling (assuming it's always available), and cleans up JSDoc throughout. The test file is significantly simplified with many complex tests removed or refactored to use real timers instead of fake timers.

Changes:

  • Reorganized code structure: class before functions, logical ordering within class and function groups
  • Removed unused #key field and renamed #createOptions to #options
  • Simplified #hydrate() method to delegate restoration logic to restoreKey() with fallback migration
  • Removed localStorage try/catch wrappers (assumes localStorage always available)
  • Cleaned up JSDoc comments for brevity and clarity
  • Simplified tests: removed complex idle/actor tests, refactored others to use real timers

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/client/auth-client.ts Reorganized class structure with simplified hydration logic and removed error handling for localStorage
tests/client/auth-client.test.ts Significant test simplification with many complex tests removed and real timers used for async testing

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/client/auth-client.ts
Comment thread src/client/auth-client.ts Outdated
Comment thread src/client/auth-client.ts Outdated
Comment thread src/client/auth-client.ts Outdated
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from 8b0c8e1 to 93529ff Compare April 8, 2026 08:13
@sea-snake sea-snake force-pushed the sea-snake/cleanup branch from f3a17c3 to 16e0390 Compare April 8, 2026 08:13
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from 93529ff to d5392e7 Compare April 8, 2026 08:16
@sea-snake sea-snake force-pushed the sea-snake/cleanup branch from 16e0390 to 37bd12a Compare April 8, 2026 08:16
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from d5392e7 to 6b286ca Compare April 8, 2026 08:25
@sea-snake sea-snake force-pushed the sea-snake/cleanup branch 2 times, most recently from 6832561 to 70ef0c7 Compare April 8, 2026 08:50
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from 6b286ca to 68b5449 Compare April 8, 2026 08:50
@sea-snake sea-snake force-pushed the sea-snake/cleanup branch from 70ef0c7 to 7001d3e Compare April 8, 2026 09:25
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from 68b5449 to 3f03dcc Compare April 8, 2026 09:25
@sea-snake sea-snake force-pushed the sea-snake/cleanup branch from 7001d3e to f4de393 Compare April 8, 2026 09:30
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from 3f03dcc to 49a378d Compare April 15, 2026 15:01
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from 49a378d to 741e482 Compare April 15, 2026 15:09
@sea-snake sea-snake force-pushed the sea-snake/cleanup branch 2 times, most recently from d508482 to a29a7c1 Compare April 15, 2026 15:13
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from 741e482 to 094df60 Compare April 15, 2026 15:13
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch from 094df60 to 6da0b83 Compare April 15, 2026 15:22
@sea-snake sea-snake force-pushed the sea-snake/one-click-openid-v2 branch 2 times, most recently from 2b03a6a to ce81960 Compare April 15, 2026 15:33
sea-snake added a commit that referenced this pull request Apr 15, 2026
Adds support for one-click sign-in via OpenID providers. When
`openIdProvider` is set on `AuthClientCreateOptions`, the identity
provider URL includes an `openid` search param, allowing the user to
skip the Internet Identity authentication method screen and authenticate
via Google, Apple, or Microsoft directly.

Supported providers:
- `'google'` → `https://accounts.google.com`
- `'apple'` → `https://appleid.apple.com`
- `'microsoft'` → `https://login.microsoftonline.com/{tid}/v2.0`

```typescript
const authClient = new AuthClient({
  openIdProvider: 'google',
});
await authClient.login();
```

---
Prev: #82 | Next: #84

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Base automatically changed from sea-snake/one-click-openid-v2 to main April 15, 2026 16:23
sea-snake and others added 2 commits April 16, 2026 07:34
pnpm audit is broken with workspace overrides.
See pnpm/pnpm#11265

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sea-snake sea-snake merged commit 58de68d into main Apr 16, 2026
10 checks passed
@sea-snake sea-snake deleted the sea-snake/cleanup branch April 16, 2026 05:46
sea-snake added a commit that referenced this pull request Apr 17, 2026
Adds a `requestAttributes` method to `AuthClient` that requests signed
user attributes from the identity provider via the signer's JSON-RPC
interface. Attributes can be requested in parallel with sign-in.

The method requires a canister-issued `nonce` to prevent replay attacks
and bind attributes to a specific backend action. The signed attribute
bundle also includes `implicit:origin` and
`implicit:issued_at_timestamp_ns` for the backend canister to verify.

```typescript
const authClient = new AuthClient();

const nonce: Uint8Array = await backend.registerBegin();
const signInPromise = authClient.signIn();
const attributesPromise = authClient.requestAttributes({ keys: ['email'], nonce });

await signInPromise;
const { data, signature } = await attributesPromise;
```

The method sends an `ii-icrc3-attributes` JSON-RPC request with the
requested attribute keys and the provided nonce, and returns the
base64-decoded `data` and `signature` as `Uint8Array`. Throws on error
responses or invalid results.

## Breaking changes

- `AuthClient.login()` is renamed to `AuthClient.signIn()`.
- `AuthClientLoginOptions` is renamed to `AuthClientSignInOptions`.

---
Prev: #84

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.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.

3 participants