Sign-in on a website
The redirect flow: the user leaves your page for account.novren.net and comes back to your callback URL. It works with any front end (plain JavaScript, Vue, React, Svelte…) because it only needs fetch, sessionStorage and Web Crypto.
You need: an app and a client whose callback URL is a page of your site. See Quickstart.
1. Start the sign-in
import { beginLogin } from '@novren/id-client/app'
async function signIn() {
const { url } = await beginLogin({
accountUrl: 'https://account.novren.net',
clientId: 'cl-0123456789abcdef0123', // your client, from the console
redirectUri: 'https://myapp.example/callback', // registered on that client
})
location.assign(url)
}| Parameter | Required | Meaning |
|---|---|---|
accountUrl | yes | The account page: https://account.novren.net. |
clientId | yes | The client id from the console (cl-…). |
redirectUri | yes | One of that client's callback URLs, written exactly as registered (scheme, host, port, path). |
locale | no | The language of the sign-in page. See Language of the form. |
beginLogin stores the pending request in sessionStorage under the key novren-id/pending-login, then returns { url }. Navigate there with location.assign(url).
2. Complete it on the callback page
import { completeLogin, LoginRejected } from '@novren/id-client/app'
async function onCallbackPage() {
// The user pressed "Deny" on the consent page.
const params = new URLSearchParams(location.search)
if (params.get('error') === 'access_denied') {
history.replaceState(null, '', location.pathname)
return showMessage('Sign-in cancelled.')
}
try {
const login = await completeLogin({
idServerUrl: 'https://id.novren.net',
clientId: 'cl-0123456789abcdef0123',
redirectUri: 'https://myapp.example/callback', // exactly the one used in beginLogin
})
// login.accountId the user's Novren ID (stable: use it as your user id)
// login.email to show in the UI; it never reached any server
// login.tokens access token (5 minutes) + refresh token
// login.productKey your app's key for this user (non-extractable CryptoKey)
startSession(login)
} catch (error) {
if (error instanceof LoginRejected) return showMessage('Sign-in failed, please try again.')
throw error // network error or Novren ID unavailable: offer a retry
}
}completeLogin:
- reads and deletes the pending request from
sessionStorage; - removes
?code…#key…from the address bar withhistory.replaceState, so the fragment doesn't stay in the history; - checks that
clientIdandredirectUrimatch the pending request, thenstate; - opens the sealed key and exchanges the code.
It returns a LoginResult.
The user presses Deny
The callback receives ?error=access_denied&state=…, with no code and no fragment. Check for it before calling completeLogin: otherwise it throws LoginRejected because the answer is incomplete, and the pending request is consumed anyway.
Errors
| Error | When | What to do |
|---|---|---|
LoginRejected | state doesn't match, no pending request in this tab, incomplete answer, the code was refused (expired after 60 s, already used) | Show "sign-in failed" and a button to start again. |
TypeError from fetch | network down | Offer a retry: start again with beginLogin. |
OperationError (Web Crypto) | the envelope doesn't open: it was made for another request | Treat it like LoginRejected. |
The message of LoginRejected is meant for logs, not for your users.
3. After sign-in
- The user id is
login.accountId. Create or find your user with it; the email can change, the id can't. - Keep the tokens to call your API: see Keep users signed in.
- Keep
productKeyin memory to encrypt and decrypt: see Encrypt user data.
Single-page apps
The callback URL can be a route of your single-page app (/callback). Two things to check:
- the route must call
completeLoginonce. In development, React's strict mode runs effects twice: the second call finds no pending request and throws. Guard it with a module-level flag; - after
completeLogin, navigate with your router to the page the user wanted. Keep that page insessionStoragebeforebeginLoginif you need it.
Checklist
- [ ] The callback URL in the code is exactly the one registered on the client.
- [ ]
?error=access_deniedis handled beforecompleteLogin. - [ ]
completeLoginruns once per sign-in. - [ ] The refresh token is stored after every refresh.
- [ ] Your server verifies tokens with
@novren/id-verifier.