import Service from '@ember/service'; import type Transition from '@ember/routing/transition'; import type EsaBaseSessionStore from '../session-stores/base'; type RouteOrCallback = string | (() => void); type InternalSessionMock = { isAuthenticated: boolean; content: Data; store: unknown; attemptedTransition: null; on: (event: 'authenticationSucceeded' | 'invalidationSucceeded', cb: () => void) => void; authenticate: (authenticator: string, ...args: any[]) => Promise; invalidate: (...args: any[]) => Promise; requireAuthentication: (transition: Transition, routeOrCallback: RouteOrCallback) => boolean; prohibitAuthentication: (routeOrCallback: RouteOrCallback) => boolean; restore: () => Promise; set(key: string, value: any): void; setRedirectTarget: EsaBaseSessionStore['setRedirectTarget']; getRedirectTarget: EsaBaseSessionStore['getRedirectTarget']; clearRedirectTarget: EsaBaseSessionStore['clearRedirectTarget']; }; export type ExtraAuthenticationArgs = { redirectTarget?: string; [key: string]: unknown; }; export type DefaultDataShape = { authenticated: { authenticator: string; [key: string]: string; }; }; /** __The session service provides access to the current session as well as methods to authenticate it, invalidate it, etc.__ It is the main interface for the application to Ember Simple Auth's functionality. ```js // app/components/login-form.js import Component from '@ember/component'; import { service } from '@ember/service'; export default class LoginFormComponent extends Component { @service session; } ``` @class SessionService @extends Service @public */ export default class SessionService extends Service { session: InternalSessionMock; constructor(owner: any); /** * Says whether the service was correctly initialized by the {#linkplain SessionService.setup} */ _setupIsCalled: boolean; /** Returns whether the session is currently authenticated. @memberof SessionService @property isAuthenticated @type Boolean @readOnly @default false @public */ isAuthenticated: boolean; /** The current session data as a plain object. The `authenticated` key inside {@linkplain SessionService.data} holds the session data that the authenticator resolved with when the session was authenticated (see {@linkplain BaseAuthenticator.authenticate}) and that will be cleared when the session is invalidated. This data cannot be written. All other session data is writable and will not be cleared when the session is invalidated. @memberof SessionService @property data @type Object @readOnly @default { authenticated: {} } @public */ data: Data; /** The session store. @memberof SessionService @property store @type BaseStore @readOnly @default null @public */ store: unknown; /** A previously attempted but intercepted transition (e.g. by the {@linkplain SessionService.requireAuthentication} If an attempted transition is present it will be retried. This is an `in-memory` property, see {@linkplain SessionService.setRedirectTarget}, {@linkplain SessionService.getRedirectTarget} for a persistent redirect mechanism. `attemptedTransition` is used _first_ if set. @memberof SessionService @property attemptedTransition @type Transition @default null @public */ attemptedTransition: null | Transition; get redirectTargetKey(): string | null; set(key: any, value: any): any; _setupHandlers(): void; /** __Authenticates the session with an `authenticator`__ and appropriate arguments. The authenticator implements the actual steps necessary to authenticate the session (see {@linkplain BaseAuthenticator.authenticate} and returns a promise after doing so. The session handles the returned promise and when it resolves becomes authenticated, otherwise remains unauthenticated. All data the authenticator resolves with will be accessible via the {@Linkplain SessionService.data} session data's `authenticated` property. This method returns a promise. A resolving promise indicates that the session was successfully authenticated while a rejecting promise indicates that authentication failed and the session remains unauthenticated. The promise does not resolve with a value; instead, the data returned from the authenticator is available via the {@linkplain SessionService.data} property. When authentication succeeds this will trigger the {@linkplain SessionService.authenticationSucceeded} event. @memberof SessionService @method authenticate @param {String} authenticator The authenticator to use to authenticate the session @param {Any} [...args] The arguments to pass to the authenticator; depending on the type of authenticator these might be a set of credentials, a Facebook OAuth Token, etc. @return {Promise} A promise that resolves when the session was authenticated successfully and rejects otherwise @public */ authenticate(authenticator: string, ...args: any[]): Promise; /** __Invalidates the session with the authenticator it is currently authenticated with__ (see {@linkplain SessionService.authenticate}). This invokes the authenticator's {@linkplain BaseAuthenticator.invalidate} method and handles the returned promise accordingly. This method returns a promise. A resolving promise indicates that the session was successfully invalidated while a rejecting promise indicates that invalidation failed and the session remains authenticated. Once the session is successfully invalidated it clears all of its authenticated data (see {@linkplain SessionService.data}). When invalidation succeeds this will trigger the {@linkplain SessionService.invalidationSucceeded} event. When calling the {@linkplain BaseAuthenticator.invalidate} on an already unauthenticated session, the method will return a resolved Promise immediately. @memberof SessionService @method invalidate @param {Array} ...args arguments that will be passed to the authenticator @return {Promise} A promise that resolves when the session was invalidated successfully and rejects otherwise @public */ invalidate(...args: any[]): Promise; /** Checks whether the session is authenticated and if it is not, transitions to the specified route or invokes the specified callback. If a transition is in progress and is aborted, this method will save it in the session service's {@linkplain SessionService.attemptedTransition} property so that it can be retried after the session is authenticated. If the transition is aborted in Fastboot mode, the transition's target URL will be saved in a `ember_simple_auth-redirectTarget` cookie for use by the browser after authentication is complete. Accepts an optional object with `redirectTarget` property. Related to {@linkplain SessionService.setRedirectTarget}, {@linkplain SessionService.getRedirectTarget} @example // your-route.js this.session.requireAuthentication(transition, 'login', { redirectTarget: '/alternative-to-transition.intent.url' }) @memberof SessionService @method requireAuthentication @param {Transition} transition A transition that triggered the authentication requirement or null if the requirement originated independently of a transition @param {String|Function} routeOrCallback The route to transition to in case that the session is not authenticated or a callback function to invoke in that case @return {Boolean} true when the session is authenticated, false otherwise @public */ requireAuthentication(transition: Transition, routeOrCallback: RouteOrCallback, extraArgs?: ExtraAuthenticationArgs): any; /** Checks whether the session is authenticated and if it is, transitions to the specified route or invokes the specified callback. @memberof SessionService @method prohibitAuthentication @param {String|Function} routeOrCallback The route to transition to in case that the session is authenticated or a callback function to invoke in that case @return {Boolean} true when the session is not authenticated, false otherwise @public */ prohibitAuthentication(routeOrCallback: RouteOrCallback): boolean; /** This method is called whenever the session goes from being unauthenticated to being authenticated. If there is a transition that was previously intercepted by the {@linkplain SessionService.requireAuthentication}, it will retry it. If there is no such transition, the `ember_simple_auth-redirectTarget` cookie will be checked for a url that represents an attemptedTransition that was aborted in Fastboot mode, otherwise this action transitions to the specified routeAfterAuthentication. @memberof SessionService @method handleAuthentication @param {String} routeAfterAuthentication The route to transition to @public */ handleAuthentication(routeAfterAuthentication: string): void; /** This method is called whenever the session goes from being authenticated to not being authenticated. __It reloads the Ember.js application__ by redirecting the browser to the specified route so that all in-memory data (such as Ember Data stores etc.) gets cleared. If the Ember.js application will be used in an environment where the users don't have direct access to any data stored on the client (e.g. [cordova](http://cordova.apache.org)) this action can be overridden to e.g. simply transition to the index route. @memberof SessionService @method handleInvalidation @param {String} routeAfterInvalidation The route to transition to @public */ handleInvalidation(routeAfterInvalidation: string): void; /** Sets up the session service. This method must be called when the application starts up, usually as the first thing in the `application` route's `beforeModel` method. @memberof SessionService @method setup @public */ setup(): Promise; /** Stores the `redirectTarget` in both `globalThis.sessionStorage` and the configured `session-store`. Key is computed based on the `session-store:application` `key` or `cookieName` property. This method is internally called by {@linkplain SessionService.requireAuthentication}. @memberof SessionService @method setRedirectTarget @public */ setRedirectTarget(url: string): void; /** Retrieves the `redirectTarget` from `globalThis.sessionStorage` first, falls back to `session-store:application` when nothing's found. This method is internally called by {@linkplain SessionService.handleAuthentication}. @memberof SessionService @method getRedirectTarget @public */ getRedirectTarget(): string | null; /** Clears the `redirectTarget` from `globalThis.sessionStorage` and the `session-store:application`. This method is internally called by {@linkplain SessionService.handleAuthentication}. @memberof SessionService @method clearRedirectTarget @public */ clearRedirectTarget(): void; } export {};