UNPKG

@donation-alerts/auth

Version:

Authentication provider for Donation Alerts API with ability to automatically refresh user tokens.

44 lines (43 loc) 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getExpiryMilliseconds = getExpiryMilliseconds; exports.getTokenExpiryDate = getTokenExpiryDate; exports.isAccessTokenExpired = isAccessTokenExpired; /** * Calculates the expiration time of the access token in milliseconds since UNIX epoch. * * @remarks * This function computes the approximate time when the token will expire by adding * the `expiresIn` value (in seconds) to the `obtainmentTimestamp`. * * @param token The access token whose expiration time should be calculated. * * @returns The expiration time in milliseconds since UNIX epoch. */ function getExpiryMilliseconds(token) { return token.obtainmentTimestamp + token.expiresIn * 1000; } /** * Calculates the expiration date of the access token as a `Date` object. * * @param token The access token whose expiration date should be calculated. * * @returns A `Date` object representing the token expiration date. */ function getTokenExpiryDate(token) { return new Date(getExpiryMilliseconds(token)); } /** * Checks whether the given access token is expired. * * @remarks * To handle potential latency issues between the API and the client, * this function applies a one-minute grace period when determining expiry. * * @param token The access token to evaluate. * * @returns A boolean indicating whether the token has expired (`true`) or not (`false`). */ function isAccessTokenExpired(token) { return Date.now() >= getExpiryMilliseconds(token); }