UNPKG

@aws-amplify/core

Version:
70 lines (68 loc) 2.58 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthClass = exports.isTokenExpired = void 0; function isTokenExpired({ expiresAt, clockDrift, }) { const currentTime = Date.now(); return currentTime + clockDrift > expiresAt; } exports.isTokenExpired = isTokenExpired; class AuthClass { constructor() { } /** * Configure Auth category * * @internal * * @param authResourcesConfig - Resources configurations required by Auth providers. * @param authOptions - Client options used by library * * @returns void */ configure(authResourcesConfig, authOptions) { this.authConfig = authResourcesConfig; this.authOptions = authOptions; } async fetchAuthSession(options = {}) { let tokens; let credentialsAndIdentityId; let userSub; // Get tokens will throw if session cannot be refreshed (network or service error) or return null if not available tokens = await this.getTokens(options); if (tokens) { userSub = tokens.accessToken?.payload?.sub; // getCredentialsAndIdentityId will throw if cannot get credentials (network or service error) credentialsAndIdentityId = await this.authOptions?.credentialsProvider?.getCredentialsAndIdentityId({ authConfig: this.authConfig, tokens, authenticated: true, forceRefresh: options.forceRefresh, }); } else { // getCredentialsAndIdentityId will throw if cannot get credentials (network or service error) credentialsAndIdentityId = await this.authOptions?.credentialsProvider?.getCredentialsAndIdentityId({ authConfig: this.authConfig, authenticated: false, forceRefresh: options.forceRefresh, }); } return { tokens, credentials: credentialsAndIdentityId?.credentials, identityId: credentialsAndIdentityId?.identityId, userSub, }; } async clearCredentials() { if (this.authOptions?.credentialsProvider) { return await this.authOptions.credentialsProvider.clearCredentialsAndIdentityId(); } } async getTokens(options) { return ((await this.authOptions?.tokenProvider?.getTokens(options)) ?? undefined); } } exports.AuthClass = AuthClass; //# sourceMappingURL=index.js.map