@codeparticles/auth-sdk
v0.1.0
Published
Pre-configured Better Auth client for Code Particles apps integrating with accounts.codeparticles.ke
Downloads
23
Readme
@codeparticles/auth-sdk
Pre-configured Better Auth client for Code Particles apps that share the accounts.codeparticles.ke session cookie (via crossSubDomainCookies). If your app instead needs to integrate as a third-party OAuth client ("Login with Code Particles"), see docs/ in cp-auth instead -- this package is for first-party apps on a *.codeparticles.ke subdomain.
Install
pnpm add @codeparticles/auth-sdk5-minute integration
// src/lib/auth.ts
import { createAuthClient } from '@codeparticles/auth-sdk';
export const { useSession, signIn, signOut, useOrganization } = createAuthClient();
// In dev, point at your local CP Auth instance instead:
// createAuthClient({ baseURL: 'http://localhost:3000' })// src/components/Profile.tsx
import { useSession, signOut } from '../lib/auth';
export function Profile() {
const { data: session, isPending } = useSession();
if (isPending) return <p>Loading...</p>;
if (!session) return <p>Signed out</p>;
return (
<div>
<p>Signed in as {session.user.email}</p>
<button onClick={() => signOut()}>Sign out</button>
</div>
);
}// src/components/SignIn.tsx
import { signIn } from '../lib/auth';
export function SignIn() {
return (
<button onClick={() => signIn.email({ email: '[email protected]', password: '...' })}>
Sign in
</button>
);
}// src/components/CurrentOrg.tsx
import { useOrganization } from '../lib/auth';
export function CurrentOrg() {
const { data: org } = useOrganization();
return <span>{org?.name ?? 'No active organization'}</span>;
}That's it -- no manual type declarations needed. session.user is fully typed, including the workspaceId field CP Auth adds on top of Better Auth's defaults.
TanStack Query integration (optional)
If your app's data layer is entirely TanStack Query and you'd rather session state live in the same cache (shared invalidation, devtools) than in Better Auth's own reactive store:
import { createAuthClient, useSessionQuery } from '@codeparticles/auth-sdk';
const authClient = createAuthClient();
function Profile() {
const { data: session } = useSessionQuery(authClient);
// ...
}This is purely additive -- useSession works fine standalone, with or without a QueryClientProvider in the tree. Use useSessionQuery only if you specifically want session state to live in TanStack Query's cache.
What's included
createAuthClient(options?)-- factory, defaultsbaseURLtohttps://accounts.codeparticles.keuseSession,signIn,signOut,signUp-- Better Auth's own reactive hooks/actionsuseOrganization-- alias for Better Auth'suseActiveOrganizationuseListOrganizations-- list all orgs the current user belongs touseSessionQuery-- optional TanStack Query-backed alternative touseSession
What's not included
Provider-side plugins (2FA enrollment, passkeys, SSO, the OAuth provider itself, admin actions) are intentionally left out -- those are for building accounts.codeparticles.ke's own UI, not for consuming it. If your app needs one of those, talk to the CP Auth team before reaching for a lower-level Better Auth client directly.
