Skip to content

Code exchange

Following a successful registration or authentication operation, you will receive a code. This can be exchanged for a Principal, representing the outcome of the operation:

backend/login.ts
import { exchangeCode, isPrincipal } from '@passlock/node/pricipal';
const tenancyId = "myTenancyId";
const apiKey = "myApiKey";
// could be a Principal or an error
const result = await exchangeCode(code, { tenancyId, apiKey });
// use the type guard to ensure we have a Principal
if (isPrincipal(result)) {
console.log(result);
} else {
console.error(result.message);
}

Behind the scenes, exchangeCode makes a REST call to the Passlock API. If you prefer you can make the call yourself:

GET /{tenancyId}/principal/{code} HTTP/1.1
Host: https://api.passlock.dev
Accept: application/json
Authorization: Bearer {apiKey}