Sign users up and in
This page shows how to use the Qoni SDK to let your app's users sign up and sign in through GenAuth's hosted page, and get their user ID and session (Access Token). Obtaining a delegation token for the user and letting the Agent work for them both start from here.
Version
The examples on this page use npm 0.10.1. An app with an existing GenAuth/OIDC sign-in integration can keep using it to obtain the user ID and Access Token, then read the user with currentUser() / genauth.userInfo(); see Qoni SDK.
Sign-up and sign-in both happen on Qoni's hosted page through the standard OIDC authorization code flow: your server only builds the redirect URL and exchanges the code, so the user's password never touches your server.
Before you start
- Node.js 20.11 or later.
- A Qoni AccessKey ID and Secret bound to a business app. The Qoni Console AccessKey list shows
App: <App ID>under each AccessKey. - A sign-in callback URL registered on that business app: in the Qoni Console open GenAuth, go to the self-built applications, open the app with the same App ID, and add the callback URL, for example
https://app.example.com/auth/qoni/callback, to its login callback URLs. Separate several URLs with commas. Without it,createLoginUrlreturns400 eak.hosted_login.redirect_uri_not_allowed. The app whose authentication domain containsdlgis the one Qoni uses for the consent page; do not change its callback URL.
Install the SDK and create the client on your app server:
npm install @qoniai/qoni@0.10.1import { Qoni } from '@qoniai/qoni'
// Create the client on your app server only; never ship the AccessKey to a browser or phone
const qoni = new Qoni({
accessKey: process.env.QONI_ACCESS_KEY!,
secretKey: process.env.QONI_SECRET_KEY!,
})1. Redirect to the hosted page
When the user taps "Sign up" or "Sign in", create the hosted page URL and redirect the browser to it:
const login = await qoni.genauth.auth.createLoginUrl({
redirectUri: 'https://app.example.com/auth/qoni/callback',
state: '<csrf-state>', // generated and stored by your app, checked on callback
})
console.log(login.url) // redirect the browser to this URLredirectUrimust exactly match the callback URL registered in the Console, or the hosted page refuses to send the user back.- Your app generates
state, keeps it in the user's session and checks it on callback to prevent cross-site request forgery. - The hosted page offers email verification-code sign-in: a new email is registered on its first sign-in, so there is no separate sign-up step.
2. Exchange the code on callback
After the user signs up or signs in, Qoni returns to redirectUri with code and state. Check state first, then exchange code for the user's identity:
const session = await qoni.genauth.auth.exchangeLoginCode({
code: '<callback-code>',
redirectUri: 'https://app.example.com/auth/qoni/callback',
})
const user = session.user // user.id is this user's ID in GenAuth
console.log(user.id, user.isNewUser, session.expiresIn)session.user.idis the user's ID in GenAuth; delegation and audit identify the user by it.session.accessTokenrepresents the user's current session; read the user withqoni.currentUser({ accessToken }). Keep it with the user ID in your server-side session, and never send it to the browser.- When
user.isNewUseristrue, the user just signed up, which is a good moment to guide them through first-time setup. - A
codecan be exchanged only once and expires quickly. If the exchange fails, send the user through step 1 again. - If the callback carries
errorinstead ofcode, do not callexchangeLoginCode; show the error and send the user through step 1 again. A new email's first sign-in occasionally returnserror=access_denied(cannot attribute the registration to a sign-in window); the account is already registered, so going through step 1 once more signs the user in.
Checkpoint: you have user.id and session.accessToken, and state matches what you stored in step 1.
Next steps
- Read Personal Agent to obtain a delegation token for a signed-in user and let the Agent work for them.
- Read Your first delegation in 30 minutes for the full delegation token flow.