@seek-services/auth-js
v0.2.0
Published
Browser SDK of SeekAuth: OpenID Connect logins with PKCE, DPoP-bound tokens, the embedded login, passkeys and the account API
Maintainers
Readme
@seek-services/auth-js
The browser core of the SeekAuth SDK: logins
through the hosted page or on your own page, tokens bound to a key that never
leaves the browser (DPoP),
storage and refresh of the session across tabs, passkeys, and the account API.
It has no runtime dependencies and works with any framework. For React, use
@seek-services/auth-react,
which is built on it.
npm install @seek-services/auth-jsIt is an ES module with its TypeScript types, for browsers that have Web
Crypto, IndexedDB and fetch (every current one). It can be imported during
server rendering, but logs in only in a browser.
A login in a few lines
You need an app in the SeekAuth dashboard. Its
development environment comes with a single-page app client, and its
Quickstart page shows the issuer and the client id. Development accepts
any page on http://localhost:<port>; in production, list your page under
the client's allowed origins and redirect URIs.
import { AuthClient } from '@seek-services/auth-js'
const auth = new AuthClient({
issuer: 'https://your-app-dev.auth.seek.services',
clientId: 'your-app-dev-spa',
redirectUri: window.location.origin + '/',
})
// On page load: finish a login that came back, or pick the stored session up.
let user = auth.hasRedirectResponse() ? await auth.handleRedirectCallback() : await auth.restore()
auth.subscribe((next) => {
user = next
})
document.querySelector('#login')?.addEventListener('click', () => auth.loginWithRedirect())
document.querySelector('#logout')?.addEventListener('click', () => auth.logout())
// Calls to your API carry the access token and a fresh DPoP proof.
const orders = await auth.authFetch('https://api.your-app.com/orders', { resource: 'orders-api' })loginWithRedirect() goes to the environment's hosted login page and back.
beginLogin() runs the same login on your own page instead (the embedded
login): it returns a challenge client that says what to ask for next.
What is in it
AuthClient: the session of a single-page app. The access token lives in memory and the refresh token in IndexedDB, both bound to the device's DPoP key; refreshes are serialized across tabs with the Web Locks API.ChallengeClient: the embedded login's steps (email code, password, passkeys, wallets, two-step login, terms).client.passkeys,client.twoStep,client.identities: the account API for an app's settings page.- PKCE, DPoP proofs, ID token checks, and the errors the service answers with
(
AuthError,StepUpRequiredError).
Documentation
@seek-services/auth-js and @seek-services/auth-react are released
together and always carry the same version.
