import { SpecifyCurrentUser } from '../../httpApi/index.js'; import { SpecifyError, specifyErrors } from '../../errors/index.js'; export class Authentication { #currentUser: SpecifyCurrentUser | undefined; #personalAccessToken: string | undefined; get isAuthenticated() { return this.#currentUser !== undefined; } get credentials() { if (!this.#currentUser || !this.#personalAccessToken) { throw new SpecifyError({ publicMessage: 'No current user authenticated. Please authenticate first', errorKey: specifyErrors.AUTHENTICATION_UNEXPECTED_ERROR.errorKey, }); } return { personalAccessToken: this.#personalAccessToken, currentUser: this.#currentUser, currentOrganization: this.#currentUser.organizations[0], }; } get personalAccessToken() { return this.credentials.personalAccessToken; } set(personalAccessToken: string, currentUser: SpecifyCurrentUser) { this.#currentUser = currentUser; this.#personalAccessToken = personalAccessToken; } reset() { this.#currentUser = undefined; this.#personalAccessToken = undefined; } }