@studiohyperdrive/ngx-auth
Version:
A library of core authentication functionality used with @studiohyperdrive/types-auth.
104 lines (103 loc) • 4.41 kB
TypeScript
import { Observable } from 'rxjs';
import { AuthenticationResponse } from '@studiohyperdrive/types-auth';
import { NgxAuthenticationResponseFeature } from '../types';
/**
* An abstract service used by the directives, guards and other components of @studiohyperdrive/ngx-auth
*
* @template AuthenticationResponseType - The type of authentication response
* @template SignInDataType - The data type used to sign in a user
* @template SignoutDataType - The data type used to sign out a user
* @template SignOutResponseType - The data type you get when signing out a user
*/
export declare abstract class NgxAuthenticationAbstractService<AuthenticationResponseType extends AuthenticationResponse<unknown> = AuthenticationResponse<any>, SignInDataType = any, SignoutDataType = any, SignOutResponseType = void> {
/**
* A subject to store the authentication response if no other state implementation was provided
*/
private readonly authenticationResponseSubject;
/**
* A subject to store whether we've authenticated already
*/
private readonly authenticationStatusSubject;
/**
* A subject to store global features that are available for all users, regardless of their authenticated state
*/
private readonly globalFeaturesSubject;
/**
* Whether an authentication attempt has been made
*/
readonly hasAuthenticated$: Observable<boolean>;
/**
* Whether the user is authenticated
*/
readonly isAuthenticated$: Observable<boolean>;
/**
* The call required to sign in a user
*
* @param signInData - The data needed to sign in a user
*/
protected abstract signInUser(signInData: SignInDataType): Observable<AuthenticationResponseType>;
/**
* The call required to sign out a user
*
* @param signoutDataType - Optional data needed to sign out a user
*/
protected abstract signOutUser(signoutDataType?: SignoutDataType): Observable<SignOutResponseType>;
/**
* Stores the authentication response in the state
*
* @param response - The authentication response
*/
protected storeAuthenticationResponse(response: AuthenticationResponseType): void;
/**
* Returns the authentication response from the state
*/
protected getAuthenticationResponse(): Observable<AuthenticationResponseType>;
/**
* The authenticated user
*/
get user$(): Observable<AuthenticationResponseType['user']>;
/**
* The session of the authenticated user
*/
get session$(): Observable<AuthenticationResponseType['session']>;
/**
* The metadata of the authenticated user
*/
get metadata$(): Observable<AuthenticationResponseType['metadata']>;
/**
* Signs in a user and stores the authentication response
*
* @param signInData - The data needed to sign in a user
*/
signIn(signInData: SignInDataType): Observable<void>;
/**
* Signs out a user and removes the stored authentication response
*
* @param signoutDataType - Optional data needed to sign out a use
*/
signOut(signoutDataType?: SignoutDataType): Observable<SignOutResponseType>;
/**
* Returns whether the user has the required features.
*
* @param requiredFeatures - An array of required features
* @param shouldHaveAllFeatures - Whether all features in the array are required, by default true
*/
hasFeature(requiredFeatures: NgxAuthenticationResponseFeature<AuthenticationResponseType>[], shouldHaveAllFeatures?: boolean): Observable<boolean>;
/**
* Sets a set of global features that are always present, regardless of the authenticated state of the user
*
* @param features - A list of features
*/
setGlobalFeatures(features: NgxAuthenticationResponseFeature<AuthenticationResponseType>[]): void;
/**
* Returns whether the user has the required permissions.
*
* @param requiredPermissions - An array of required permissions
* @param shouldHaveAllPermissions - Whether all permissions in the array are required, by default true
*/
hasPermission(requiredPermissions: AuthenticationResponseType['session']['permissions'], shouldHaveAllPermissions?: boolean): Observable<boolean>;
/**
* Returns a session or an empty session depending on the authenticated state
*/
private getSession;
}