Extensions and other hosts
beginLogin and completeLogin assume a normal web page: sessionStorage and a single tab that leaves and comes back. Some environments are different:
- a browser extension, where the secret part lives in the background (service worker) and the user is in a popup;
- a native app that shows the embedded form in its own view but keeps the keys in the native process;
- any case where you want to keep the pending request somewhere you control.
For these the SDK offers the same flow in three separate steps.
ts
import { createLoginRequest, finishLoginResponse, embedLoginFrame, type PendingLogin } from '@novren/id-client/app'
// 1. Where the secret part lives (e.g. the extension's background): prepare the request.
const { url, pending } = await createLoginRequest({
accountUrl: 'https://account.novren.net',
clientId: 'cl-0123456789abcdef0123',
redirectUri: 'https://myapp.example/callback',
})
keepInMemory(pending) // a secret for the few seconds of the sign-in: never on disk
// 2. Where the user is (e.g. the popup): show the form for that URL.
const frame = embedLoginFrame({ loginUrl: url, container: document.querySelector<HTMLElement>('#login')! })
const returnUrl = await frame.returnUrl
frame.destroy()
// 3. Back where the request lives: finish with the return URL.
const login = await finishLoginResponse({ idServerUrl: 'https://id.novren.net', returnUrl, pending })
forget(pending)The pieces
createLoginRequestreturns{ url, pending }without storing anything.pendingholds the PKCE verifier and the ephemeral key's seed: a secret for the few seconds of the sign-in. Keep it in memory and drop it right after.embedLoginFrameshows the form for a URL already prepared and returns only the return address (returnUrl), because whoever shows the form doesn't have the keys.finishLoginResponsecompletes withreturnUrlandpending: checksstate, opens the envelope, exchanges the code.
Instead of the embedded form you can also open url in a tab or a window (for an extension, chrome.identity.launchWebAuthFlow) and pass the final address to finishLoginResponse.
Extension notes
- Register the callback URL your extension really receives. For Chrome's
launchWebAuthFlowthat ishttps://<extension-id>.chromiumapp.org/…. - Add
https://id.novren.net/*to the extension'shost_permissions, so the service worker can call/oauth/token. - The service worker can be stopped by the browser at any moment: if it is stopped during the sign-in,
pendingis lost and the sign-in must start again. Keep the sign-in short. - Never put
pendinginchrome.storage: it would land on disk.