Skip to content

Authentication

Token Signature

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({});
console.log(tokenSignResult.token);
const { BasaltToken } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({});
console.log(tokenSignResult.token);
node example.jseyJ1dWlkIjoiZDk4MTcwMjgtM2EzZS00YWYyLTgwNzYtOTExZGFlZThiMGIzIiwiZXhwIjoiMjAyNC0wMy0yMlQxMjowOTo0MS4yNThaIiwiaXNzdWVyIjoiQmFzYWx0LUlzc3VlciIsImF1ZGllbmNlIjoiQmFzYWx0LUF1ZGllbmNlIn0=.e30=.PJpP4zbxX4SaYD7hIj0wZGIqhSs8SbGdJ0W+o/Pr6WJyOhJTvRsIqQpf8sCvTFWWGyCx5ZGeczT0E1RHBE7GBg==

Token Signature with Payload

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({
    usename: 'User1',
});
console.log(tokenSignResult.token);
const { BasaltToken } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({
    usename: 'User1',
});
console.log(tokenSignResult.token);
node example.jseyJ1dWlkIjoiMzAzNjQ2ODMtYWYxMC00NjcyLTkwNTQtMmY2YWNmM2E1ZGNlIiwiZXhwIjoiMjAyNC0wMy0yMlQxMjoxMjo1NS41NjlaIiwiaXNzdWVyIjoiQmFzYWx0LUlzc3VlciIsImF1ZGllbmNlIjoiQmFzYWx0LUF1ZGllbmNlIn0=.eyJ1c2VuYW1lIjoiVXNlcjEifQ==.heBWyPtSqmwR/FTShaeqBm7NtCxxE2/faIRc1vQG80mMwOlTTzeWcPD8iNecwtkij/GW6RlEADex4uxCjULFAw==

Token Signature with Expiration Time

import { BasaltToken, type BasaltTokenSignResult, BasaltTokenExpiry } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
// you can use enum or number (default is 1 hour)
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({}, BasaltTokenExpiry.ONE_DAY);
console.log(tokenSignResult.token);
const { BasaltToken, BasaltTokenExpiry } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
// you can use enum or number (default is 1 hour)
const tokenSignResult = basaltToken.sign({}, BasaltTokenExpiry.ONE_DAY);
console.log(tokenSignResult.token);
node example.jseyJ1dWlkIjoiZTg5YjYzYTAtMjY0NS00ZjQwLWE5YzYtOGM1Yzg0NmIwNzNlIiwiZXhwIjoiMjAyNC0wMy0yM1QxMToxMDoyMC43NjVaIiwiaXNzdWVyIjoiQmFzYWx0LUlzc3VlciIsImF1ZGllbmNlIjoiQmFzYWx0LUF1ZGllbmNlIn0=.e30=.8f72CeURHH/lYENBqozW1PsmVAr3RLHtBm6hbk/TY1VoamD4WYm1TakCdVQhlUVCL3nYRaFzyQ8zhuS5b3vlCg==

Token Signature with Issuer and Audience

import { BasaltToken, type BasaltTokenSignResult, BasaltTokenExpiry } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
// default issuer is 'Basalt-Issuer' and default audience is 'Basalt-Audience'
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign(
    {}, BasaltTokenExpiry.ONE_HOUR, 'Basalt-issuer', 'Basalt-audience'
);
console.log(tokenSignResult.token);
const { BasaltToken, BasaltTokenExpiry } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
// default issuer is 'Basalt-Issuer' and default audience is 'Basalt-Audience'
const tokenSignResult = basaltToken.sign(
    {}, BasaltTokenExpiry.ONE_HOUR, 'Basalt-issuer', 'Basalt-audience'
);
console.log(tokenSignResult.token);
node example.jseyJ1dWlkIjoiNzBmYTFiZDEtOWEwNy00Mzk5LWI4Y2YtZTg3ZTNkNDVhNzU2IiwiZXhwIjoiMjAyNC0wMy0yMlQxMjoxMTozOS4zNDZaIiwiaXNzdWVyIjoiQmFzYWx0LWlzc3VlciIsImF1ZGllbmNlIjoiQmFzYWx0LWF1ZGllbmNlIn0=.e30=.3teaXCzVkSLI5+1Hr84le4l9FqqZFjLeHAYGqSDOzK9453tI276ysihygMA5oq8aabYSl4/Fn6Hm5T/p+88ZDQ==

Token Verification

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({});

try {
    basaltToken.verify(tokenSignResult.token, tokenSignResult.publicKey)
    console.log('Token is valid');
} catch (error) {
    console.error(error);
}
const { BasaltToken } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({});

try {
    basaltToken.verify(tokenSignResult.token, tokenSignResult.publicKey)
    console.log('Token is valid');
} catch (error) {
    console.error(error);
}
node example.jsToken is valid

Token UUID Retrieval

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({});

console.log(basaltToken.getTokenUuid(tokenSignResult.token));
const { BasaltToken, BasaltTokenExpiry } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({});

console.log(basaltToken.getTokenUuid(tokenSignResult.token));
node example.jsc4a198c4-2717-470d-886f-bc2921badd8f

Expiration Retrieval

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({});

console.log(basaltToken.getExpirationDate(tokenSignResult.token));
const { BasaltToken, BasaltTokenExpiry } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({});

console.log(basaltToken.getExpirationDate(tokenSignResult.token));
node example.js2024-03-22T12:03:38.266Z

Token Audience Retrieval

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({});

console.log(basaltToken.getAudience(tokenSignResult.token));
const { BasaltToken, BasaltTokenExpiry } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({});

console.log(basaltToken.getAudience(tokenSignResult.token));
node example.jsBasalt-Audience

Token Issuer Retrieval

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({});

console.log(basaltToken.getIssuer(tokenSignResult.token));
const { BasaltToken, BasaltTokenExpiry } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({});

console.log(basaltToken.getIssuer(tokenSignResult.token));
node example.jsBasalt-Issuer

Token Header Retrieval

import { BasaltToken, type BasaltTokenSignResult, type IBasaltTokenHeader } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({});

const tokenHeader: IBasaltTokenHeader = basaltToken.getHeader(tokenSignResult.token);
console.log(tokenHeader);
const { BasaltToken } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({});

const tokenHeader = basaltToken.getHeader(tokenSignResult.token);
console.log(tokenHeader);
node example.js{
uuid: '82e7fa41-44b4-4fa8-ac31-ae309234bb8b',
exp: '2024-03-22T12:19:58.187Z',
issuer: 'Basalt-Audience',
audience: 'Basalt-Audience'
}

Token Payload Retrieval

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({
    username: 'user1'
});
const tokenPayload: { username: string } = basaltToken.getPayload(tokenSignResult.token);
console.log(tokenPayload);
const { BasaltToken } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({
    username: 'user1'
});
const tokenPayload = basaltToken.getPayload(tokenSignResult.token);
console.log(tokenPayload);
node example.js{ username: 'user1' }

Check if the token has expired

import { BasaltToken, type BasaltTokenSignResult } from '@basalt-lab/basalt-auth';

const basaltToken: BasaltToken = new BasaltToken();
const tokenSignResult: BasaltTokenSignResult = basaltToken.sign({});
console.log(basaltToken.isExpired(tokenSignResult.token));
const { BasaltToken } = require('@basalt-lab/basalt-auth');

const basaltToken = new BasaltToken();
const tokenSignResult = basaltToken.sign({});
console.log(basaltToken.isExpired(tokenSignResult.token));
node example.jsfalse