@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:
<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
Name | Type | Description |
---|---|---|
bus | BusService | A BusService instance |
ls | LocalStorageService | A LocalStorageService instance |
cron | MiliCron | A MiliCron instance |
api | Axios | An Axios instance |
tokenRefresh | TokenRefreshCallback | A token refresh callback |
tokenRefreshBuffer | number | The amount of time in milliseconds before the token expires before a token is considered refreshable (default: 5 minutes) |
Returns
Defined in
Accessors
authenticated
• get
authenticated(): ComputedRef
<boolean
>
Whether or not the visitor is authenticated
Returns
ComputedRef
<boolean
>
Defined in
booted
• get
booted(): Ref
<boolean
>
Whether or not the Identity service has booted
Returns
Ref
<boolean
>
Defined in
identified
• get
identified(): ComputedRef
<boolean
>
Whether or not the visitor is identified
Returns
ComputedRef
<boolean
>
Defined in
refreshable
• get
refreshable(): ComputedRef
<boolean
>
Whether or not the token is refreshable
Returns
ComputedRef
<boolean
>
Defined in
ttl
• get
ttl(): Ref
<undefined
| number
>
The time until the authentication expires
Returns
Ref
<undefined
| number
>
Defined in
user
• get
user(): ComputedRef
<undefined
| UserIdentity
>
The user's identity
Returns
ComputedRef
<undefined
| UserIdentity
>
Defined in
Methods
boot
▸ boot(): void
Boot the Identity service
Returns
void
Defined in
login
▸ login(bearer
, expiration
, identity
): void
Save the bearer token, expiration, and user identity to the LocalStorageService and update the authentication & identification state
Parameters
Name | Type | Description |
---|---|---|
bearer | string | The bearer token |
expiration | string | The expiration time of the token |
identity | UserIdentity | The user's identity |
Returns
void
void
Defined in
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
shutdown
▸ shutdown(): void
Shutdown the Identity service
Returns
void