Primi passi
Il login in un sito web, dall’inizio alla fine: dalla creazione dell’app a un’API protetta. Una decina di minuti.
1. Crea l’app
- Entra su account.novren.net/console. La console chiede prima di attivare la 2FA: crea credenziali che aprono i dati dei tuoi utenti. Vedi Accesso, 2FA e sudo mode.
- Premi Nuova app e dalle un nome. I tuoi utenti vedono questo nome e il logo nella pagina di consenso.
- Ricevi un id dell’app come
app-0123456789abcdef0123. È l’auddei tuoi token.
2. Aggiungi un client
Un client è una porta d’ingresso della tua app: il sito, l’app desktop, il sito di staging. Ognuno ha la sua callback URL, dove l’utente torna dopo il login.
- Nell’app, apri Client → Nuovo client.
- Scrivi la callback URL del tuo sito, per esempio
https://miaapp.example/callback. In sviluppo va bene anchehttp://localhost:5173/callback. - Ricevi un id del client come
cl-0123456789abcdef0123.
Le regole delle callback URL sono in Creare app e client.
3. Installa l’SDK
npm install @novren/id-client @novren/id-verifier4. Aggiungi il pulsante di login
In una pagina qualsiasi del tuo sito:
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)
}beginLogin prepara la richiesta (PKCE, uno state casuale, una coppia di chiavi effimera), tiene la parte segreta in sessionStorage per questa scheda e restituisce l’indirizzo della pagina di login di Novren ID.
5. Completa il login nella pagina di callback
Nella pagina della tua callback URL:
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 controlla lo state, apre la chiave sigillata con la chiave effimera, scambia il codice monouso con i token e toglie tutto dalla barra degli indirizzi.
6. Proteggi la tua API
Crea un service token nella console (scheda Service token dell’app). Si vede una volta sola: mettilo nell’ambiente del tuo server come NOVREN_ID_SERVICE_TOKEN. Poi:
import { createTokenVerifier, TokenRejected } from '@novren/id-verifier'
const verifier = createTokenVerifier({
idServerUrl: 'https://id.novren.net',
product: 'app-0123456789abcdef0123', // your app id, from the console
serviceToken: process.env.NOVREN_ID_SERVICE_TOKEN!, // never in the browser
})
await verifier.start() // first read of the revocations, then every 5 secondsasync function handle(request: Request): Promise<Response> {
const header = request.headers.get('authorization') ?? ''
const token = header.startsWith('Bearer ') ? header.slice(7) : undefined
try {
const claims = await verifier.verify(token)
const userId = claims.sub // the Novren ID account id
return Response.json(await loadNotes(userId))
} catch (error) {
if (error instanceof TokenRejected) {
// invalid / revoked: the client refreshes or signs in again.
// unavailable: we could not read the revocations for 60 s; fail closed.
const status = error.reason === 'unavailable' ? 503 : 401
return new Response(null, { status, headers: { 'www-authenticate': 'Bearer' } })
}
throw error
}
}7. Provalo
- Apri il tuo sito e premi il pulsante: arrivi su
account.novren.net. - Crea un account o entra, poi premi Consenti.
- Sei di nuovo nella tua pagina di callback, con
accountId,email,tokenseproductKey. - Chiama la tua API con
Authorization: Bearer <access_token>.
Nella console, la scheda Log dell’app, sotto Accessi, mostra il consenso e lo scambio del codice appena fatti.
I passi successivi
- Tenere dentro gli utenti: refresh token e logout.
- Cifrare i dati degli utenti con
productKey. - Gli account cancellati: cancellare i dati quando un utente se ne va.