@circles-market/auth
v0.1.0
Published
Authentication client for the Market API (Safe-based SIWE) that stores a JWT in an `AuthContext`.
Readme
@circles-market/auth
Authentication client for the Market API (Safe-based SIWE) that stores a JWT in an AuthContext.
Install
pnpm add @circles-market/auth @circles-market/core @circles-market/session @circles-market/signersQuickstart
import { FetchHttpTransport } from "@circles-market/core";
import { InMemoryAuthContext } from "@circles-market/session";
import { SignersClientImpl } from "@circles-market/signers";
import { AuthClientImpl } from "@circles-market/auth";
async function signIn(marketApiBase: string, ethereum: any, avatar: string) {
const http = new FetchHttpTransport();
const session = new InMemoryAuthContext();
const signers = new SignersClientImpl();
const auth = new AuthClientImpl(marketApiBase, http, session, signers);
await auth.signInWithAvatar({ avatar, ethereum, chainId: 100 });
return session.getMeta();
}Reference
API
AuthClientImpl.signInWithAvatar({ avatar, ethereum, chainId? })→ stores a JWT in the providedAuthContextAuthClientImpl.signOut()→ clears theAuthContextAuthClientImpl.getAuthMeta()→ reads auth metadata from theAuthContext(only if token is still valid)
Return values
signInWithAvatar(...)
Returns:
type SignInResult = { address: string; chainId: number };Fields:
address: authenticated avatar address (stored lowercased byInMemoryAuthContext)chainId: chain id used for login (default:100)
Side effects:
- stores
{ token, expiresAt, address, chainId }in the providedAuthContext
getAuthMeta()
Returns:
type AuthMeta = { address: string; chainId: number } | null;Fields (when not null):
address: authenticated avatar addresschainId: chain id associated with the token
signOut()
Returns: void (clears token + metadata from the AuthContext).
Notes
Safe-based SIWE flow:
- request a challenge (
/api/auth/challenge) - sign challenge bytes via Safe
SafeMessage(bytes) - verify (
/api/auth/verify) and store JWT
Common errors
Wrong chain. Expected ... got ...→ wallet is on the wrong network.No EOA account unlocked in wallet→eth_requestAccountsreturned no accounts.Not authenticatedlater inorders/cart→ token expired or you never called sign-in.
Related packages
@circles-market/sessionfor token storage@circles-market/ordersand@circles-market/cartrequire a valid session@circles-market/sdkwires everything together for you
