import { InferType } from "yup"; import { accessTokenPayloadSchema } from "./schema-fields"; //#region src/sessions.d.ts type AccessTokenPayload = InferType; declare class AccessToken { readonly token: string; static createIfValid(token: string): AccessToken | null; private constructor(); get payload(): { exp?: number | undefined; selected_team_id: string | null; is_anonymous: boolean; sub: string; is_restricted: boolean; restricted_reason: { type: "anonymous" | "email_not_verified" | "restricted_by_administrator"; } | null; email: string | null; requires_totp_mfa: boolean; name: string | null; project_id: string; refresh_token_id: string; branch_id: string; iat: number; iss: string; aud: string; role: "authenticated"; email_verified: boolean; signed_up_at: number; }; get expiresAt(): Date; get issuedAt(): Date; /** * @returns The number of milliseconds until the access token expires, or 0 if it has already expired. */ get expiresInMillis(): number; get issuedMillisAgo(): number; isExpired(): boolean; } declare class RefreshToken { readonly token: string; constructor(token: string); } /** * An InternalSession represents a user's session, which may or may not be valid. It may contain an access token, a refresh token, or both. * * A session never changes which user or session it belongs to, but the tokens in it may change over time. */ declare class InternalSession { private readonly _options; /** * Each session has a session key that depends on the tokens inside. If the session has a refresh token, the session key depends only on the refresh token. If the session does not have a refresh token, the session key depends only on the access token. * * Multiple Session objects may have the same session key, which implies that they represent the same session by the same user. Furthermore, a session's key never changes over the lifetime of a session object. * * This is useful for caching and indexing sessions. */ readonly sessionKey: string; /** * An access token that is not known to be invalid (ie. may be valid, but may have expired). */ private _accessToken; private readonly _refreshToken; /** * Whether the session as a whole is known to be invalid (ie. both access and refresh tokens are invalid). Used as a cache to avoid making multiple requests to the server (sessions never go back to being valid after being invalidated). * * It is possible for the access token to be invalid but the refresh token to be valid, in which case the session is * still valid (just needs a refresh). It is also possible for the access token to be valid but the refresh token to * be invalid, in which case the session is also valid (eg. if the refresh token is null because the user only passed * in an access token, eg. in a server-side request handler). */ private _knownToBeInvalid; private _refreshPromise; constructor(_options: { refreshAccessTokenCallback(refreshToken: RefreshToken): Promise; refreshToken: string | null; accessToken?: string | null; }); static calculateSessionKey(ofTokens: { refreshToken: string | null; accessToken?: string | null; }): string; isKnownToBeInvalid(): boolean; /** * Marks the session object as invalid, meaning that the refresh and access tokens can no longer be used. There is no * way out of this state, and the session object will never return valid tokens again. */ markInvalid(): void; onInvalidate(callback: () => void): { unsubscribe: () => void; }; getRefreshToken(): RefreshToken | null; /** * Returns the access token if it is found in the cache and not expired yet, or null otherwise. Never fetches new tokens. */ getAccessTokenIfNotExpiredYet(minMillisUntilExpiration: number, maxMillisSinceIssued: number | null): AccessToken | null; /** * Returns the access token if it is found in the cache, fetching it otherwise. * * This is usually the function you want to call to get an access token. Either set `minMillisUntilExpiration` to a reasonable value, or catch errors that occur if it expires, and call `markAccessTokenExpired` to mark the token as expired if so (after which a call to this function will always refetch the token). * * @returns null if the session is known to be invalid, cached tokens if they exist in the cache and the access token hasn't expired yet (the refresh token might still be invalid), or new tokens otherwise. */ getOrFetchLikelyValidTokens(minMillisUntilExpiration: number, maxMillisSinceIssued: number | null): Promise<{ accessToken: AccessToken; refreshToken: RefreshToken | null; } | null>; /** * Fetches new tokens that are, at the time of fetching, guaranteed to be valid. * * The newly generated tokens are short-lived, so it's good practice not to rely on their validity (if possible). However, this function is useful in some cases where you only want to pass access tokens to a service, and you want to make sure said access token has the longest possible lifetime. * * In most cases, you should prefer `getOrFetchLikelyValidTokens`. * * @returns null if the session is known to be invalid, or new tokens otherwise (which, at the time of fetching, are guaranteed to be valid). */ fetchNewTokens(): Promise<{ accessToken: AccessToken; refreshToken: RefreshToken | null; } | null>; /** * Manually mark the access token as expired, even if the date on its payload may still be valid. * * You don't usually have to call this function anymore, but you may want to call suggestAccessTokenExpired * to hint that the access token should be refreshed as its data may have changed, if possible. */ markAccessTokenExpired(accessToken?: AccessToken): void; /** * Strongly suggests that the access token should be refreshed as its data may have changed, although it's up to this * implementation to decide whether or when the access token will be refreshed. * * This is particularly useful when the data associated with the access token may have changed for example due to an * update to the user's profile. * * The current implementation marks the access token as expired if and only if a refresh token is available (regardless of * whether the refresh token is actually valid or not), although this is not a guarantee and subject to change. * * If you need a stronger guarantee of revoking an access token, use markAccessTokenExpired instead. */ suggestAccessTokenExpired(): void; startRefreshingAccessToken(minMillisUntilExpiration: number, maxMillisSinceIssued: number | null): { unsubscribe: () => void; }; /** * Note that a callback invocation with `null` does not mean the session has been invalidated; the access token may just have expired. Use `onInvalidate` to detect invalidation. */ onAccessTokenChange(callback: (newAccessToken: AccessToken | null) => void): { unsubscribe: () => void; }; /** * @returns An access token, which may be expired or expire soon, or null if it is known to be invalid. */ private _getPotentiallyInvalidAccessTokenIfAvailable; /** * You should prefer `_getOrFetchPotentiallyInvalidAccessToken` in almost all cases. * * @returns A newly fetched access token (never read from cache), or null if the session either does not represent a user or the session is invalid. */ private _getNewlyFetchedAccessToken; private _refreshAndSetRefreshPromise; } //#endregion export { AccessToken, AccessTokenPayload, InternalSession, RefreshToken }; //# sourceMappingURL=sessions.d.ts.map