Skip to content

AuthService Service

The AuthService class handles the authentication process, including login, token management, and user session handling.

Importing the Service

To use the AuthService service, import it as follows:

typescript
import { authentication } from '@modular-rest/client';

Public Properties

PropertyDescription
userThe currently authenticated user, or null if no user is authenticated.
isLoginA boolean indicating if the user is currently logged in.

login()

Logs in the user with the provided credentials.

Arguments

ArgumentTypeDescription
identityIdentityTypeThe identity of the user (e.g., username or email).
passwordstringThe user's password.
optionsLoginOptionsTypeAdditional login options.

Return and Throw

ReturnsDescription
Promise<LoginResponseType>The login response data.
ThrowsError if the login fails.

Example

typescript
authentication.login('user@example.com', 'password123', { rememberMe: true })
    .then(response => {
        console.log('Login successful:', response);
    })
    .catch(error => {
        console.error('Login failed:', error);
    });

loginWithLastSession()

Logs in with the last session if you pass allowSave=true in the last login.

Arguments

ArgumentTypeDescription
tokenstringThe token for the last session (optional).

Return and Throw

ReturnsDescription
Promise<User>The logged-in user data.
ThrowsError if the login fails.

Example

typescript
authentication.loginWithLastSession()
    .then(user => {
        console.log('Logged in with last session:', user);
    })
    .catch(error => {
        console.error('Login with last session failed:', error);
    });

loginAsAnonymous()

Logs in as an anonymous user and retrieves a token.

Return and Throw Table:

ReturnsDescription
Promise<LoginResponseType>The login response data containing the token.
ThrowsError if the login fails.

Example

typescript
authService.loginAsAnonymous()
    .then(response => {
        console.log('Anonymous login successful:', response);
    })
    .catch(error => {
        console.error('Anonymous login failed:', error);
    });

logout()

Logs out the current user and clears the session.

Return and Throw

ReturnsDescription
voidNo return value.
ThrowsNone

Example

typescript
authentication.logout();

verifyToken()

Verifies the provided token.

Arguments

ArgumentTypeDescription
tokenstringThe token to verify.

Return and Throw

ReturnsDescription
Promise<VerifyTokenResponseType>The token verification response data.
ThrowsError if the token verification fails.

Example

typescript
authentication.verifyToken('some-jwt-token')
    .then(response => {
        console.log('Token verification successful:', response);
    })
    .catch(error => {
        console.error('Token verification failed:', error);
    });

registerIdentity()

Registers a user identity, the first step for creating a new account.

Arguments

ArgumentTypeDescription
identityIdentityTypeThe identity of the user.

Return and Throw

ReturnsDescription
Promise<BaseResponseType>The registration response data.
ThrowsError if the registration fails.

Example

typescript
authentication.registerIdentity({ idType: 'email', id: 'user@example.com' })
    .then(response => {
        console.log('Identity registered:', response);
    })
    .catch(error => {
        console.error('Identity registration failed:', error);
    });

validateCode()

Validates the provided code.

Arguments

ArgumentTypeDescription
codestringThe code to validate.

Return and Throw

ReturnsDescription
Promise<ValidateCodeResponseType>The validation response data.
ThrowsError if the validation fails.

Example

typescript
authentication.validateCode('123456')
    .then(response => {
        console.log('Code validation successful:', response);
    })
    .catch(error => {
        console.error('Code validation failed:', error);
    });

submitPassword()

Submits a password, the third step for creating a new account.

Arguments

ArgumentTypeDescription
optionsobjectThe password submission options.
options.idstringThe user identity.
options.passwordstringThe user's password.
options.codestringThe verification code.

Return and Throw

ReturnsDescription
Promise<BaseResponseType>The password submission response data.
ThrowsError if the submission fails.

Example

typescript
authentication.submitPassword({ id: 'user@example.com', password: 'newpassword', code: '123456' })
    .then(response => {
        console.log('Password submitted successfully:', response);
    })
    .catch(error => {
        console.error('Password submission failed:', error);
    });

changePassword()

Changes the user's password.

Arguments

ArgumentTypeDescription
optionsobjectThe password change options.
options.idstringThe user identity.
options.passwordstringThe new password.
options.codestringThe verification code.

Return and Throw

ReturnsDescription
Promise<BaseResponseType>The password change response data.
ThrowsError if the change fails.

Example

typescript
authentication.changePassword({ id: 'user@example.com', password: 'newpassword', code: '123456' })
    .then(response => {
        console.log('Password changed successfully:', response);
    })
    .catch(error => {
        console.error('Password change failed:', error);
    });