Skip to content

@jakguru/vueprint / @jakguru/vueprint/services/identity / IdentityService

Class: IdentityService

@jakguru/vueprint/services/identity.IdentityService

The Identity service is used to share the authentication state and user information across the application and across all instances of the application in the same browser, as well as to authenticate any API request made on behalf of the user.

Remarks

Accessing the Identity Service

The Identity Service is both injectable and accessible from the global Vue instance:

vue

<script lang="ts">
import { defineComponent, inject } from 'vue'
import type { IdentityService } from '@jakguru/vueprint'
export default defineComponent({
    setup() {
        const identity = inject<IdentityService>('identity')
        return {}
    }
    mounted() {
        const identity: IdentityService = this.config.globalProperties.$identity
    }
})
</script>

Using the Identity Service

Determining Authentication State and Current User

The IdentityService.authenticated accessor provides an easy way to determine if the visitor has been authenticated. It does not determine if the current user has been identified (meaning that information about the user has been saved to the local storage), however in most cases that information will be available very soon after the value of IdentityService.authenticated.value is true.

The IdentityService.identified accessor provides an easy way to determine if the visitor has been identified. This means that the IdentityService.user object has been created with at least 1 parameter.

Setting the authentication state

Because VuePrint does not provide any forms, it provides a method for a form to store the authentication information in the appropriate locations in the local storage. This method is IdentityService.login, and it accepts 3 arguments:

  • The bearer token which will be used to make authenticated API calls
  • The expiration timestamp of the bearer token
  • An object which is used as the user identifier

Deauthenticating a visitor

To remove the authentication and identity information of the currently logged in user, simply call the IdentityService.logout method. It accepts no arguments.

Constructors

constructor

new IdentityService(bus, ls, cron, api, tokenRefresh, tokenRefreshBuffer?): IdentityService

Create a new Identity instance

Parameters

NameTypeDescription
busBusServiceA BusService instance
lsLocalStorageServiceA LocalStorageService instance
cronMiliCronA MiliCron instance
apiAxiosAn Axios instance
tokenRefreshTokenRefreshCallbackA token refresh callback
tokenRefreshBuffernumberThe amount of time in milliseconds before the token expires before a token is considered refreshable (default: 5 minutes)

Returns

IdentityService

Defined in

src/services/identity.ts:112

Accessors

authenticated

get authenticated(): ComputedRef<boolean>

Whether or not the visitor is authenticated

Returns

ComputedRef<boolean>

Defined in

src/services/identity.ts:191


booted

get booted(): Ref<boolean>

Whether or not the Identity service has booted

Returns

Ref<boolean>

Defined in

src/services/identity.ts:184


identified

get identified(): ComputedRef<boolean>

Whether or not the visitor is identified

Returns

ComputedRef<boolean>

Defined in

src/services/identity.ts:198


refreshable

get refreshable(): ComputedRef<boolean>

Whether or not the token is refreshable

Returns

ComputedRef<boolean>

Defined in

src/services/identity.ts:212


ttl

get ttl(): Ref<undefined | number>

The time until the authentication expires

Returns

Ref<undefined | number>

Defined in

src/services/identity.ts:219


user

get user(): ComputedRef<undefined | UserIdentity>

The user's identity

Returns

ComputedRef<undefined | UserIdentity>

Defined in

src/services/identity.ts:205

Methods

boot

boot(): void

Boot the Identity service

Returns

void

Defined in

src/services/identity.ts:274


login

login(bearer, expiration, identity): void

Save the bearer token, expiration, and user identity to the LocalStorageService and update the authentication & identification state

Parameters

NameTypeDescription
bearerstringThe bearer token
expirationstringThe expiration time of the token
identityUserIdentityThe user's identity

Returns

void

void

Defined in

src/services/identity.ts:230


logout

logout(): void

Remove the bearer token, expiration, and user identity from the LocalStorageService and update the authentication & identification state

Returns

void

void

Defined in

src/services/identity.ts:258


shutdown

shutdown(): void

Shutdown the Identity service

Returns

void

Defined in

src/services/identity.ts:339

Vueprint is a commercial work product released under the MIT License and is provided as-is with no warranty or guarantee of support.