refactor: reorganize auth-client code structure#84
Conversation
There was a problem hiding this comment.
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
#keyfield and renamed#createOptionsto#options - Simplified
#hydrate()method to delegate restoration logic torestoreKey()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.
8b0c8e1 to
93529ff
Compare
f3a17c3 to
16e0390
Compare
93529ff to
d5392e7
Compare
16e0390 to
37bd12a
Compare
d5392e7 to
6b286ca
Compare
6832561 to
70ef0c7
Compare
6b286ca to
68b5449
Compare
70ef0c7 to
7001d3e
Compare
68b5449 to
3f03dcc
Compare
7001d3e to
f4de393
Compare
3f03dcc to
49a378d
Compare
f4de393 to
79d1bd5
Compare
49a378d to
741e482
Compare
d508482 to
a29a7c1
Compare
741e482 to
094df60
Compare
a29a7c1 to
cbb903d
Compare
094df60 to
6da0b83
Compare
cbb903d to
dbad5ab
Compare
2b03a6a to
ce81960
Compare
dbad5ab to
aa0e82b
Compare
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>
aa0e82b to
259e84f
Compare
pnpm audit is broken with workspace overrides. See pnpm/pnpm#11265 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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>
Reorganizes
auth-client.tsfor clarity:#keyfield#createOptionsto#options#hydrate()— delegates key restoration and migration torestoreKey()which callsmigrateFromLocalStorage()as a fallbacklocalStoragetry/catch wrappers (assumed always available)pnpm auditCI workflow due to pnpm/pnpm#11265Prev: #83 | Next: #85