Skip to main content

useAsgardeo()

The useAsgardeo() composable of the WSO2 Identity Server Vue SDK (@asgardeo/vue) returns the authentication state and methods that <AsgardeoProvider> manages.

Overview​

Call useAsgardeo() in the setup of any component rendered inside <AsgardeoProvider>. It returns an object with:

  • Reactive state as read-only refs: isSignedIn, isLoading, isInitialized, user and organization. Templates unwrap them; in scripts, read them with .value.
  • Methods to sign users in and out, read tokens and send authenticated requests.
  • Configuration values passed to <AsgardeoProvider>, such as clientId and baseUrl.

Usage​

src/components/Header.vue
<script setup>
import { useAsgardeo } from '@asgardeo/vue'

const { isSignedIn, isLoading, user, signIn, signOut } = useAsgardeo()
</script>

<template>
<p v-if="isLoading">Loading...</p>
<div v-else-if="isSignedIn">
<p>Signed in as {{ user?.userName }}</p>
<button @click="signOut()">Sign out</button>
</div>
<button v-else @click="signIn()">Sign in</button>
</template>

To react to changes in a script, watch the refs:

Inside <script setup>
import { watch } from 'vue'
import { useAsgardeo } from '@asgardeo/vue'

const { isSignedIn, getAccessToken } = useAsgardeo()

watch(isSignedIn, async (signedIn) => {
if (signedIn) {
const accessToken = await getAccessToken()
// Use the access token.
}
}, { immediate: true })

Return value​

useAsgardeo() returns the following values, grouped by purpose.

State​

PropertyTypeDescription
isSignedInReadonly<Ref<boolean>>Whether the user is signed in.
isLoadingReadonly<Ref<boolean>>Whether the SDK is busy, for example initializing, completing a sign-in or loading the user. Starts as true.
isInitializedReadonly<Ref<boolean>>Whether the SDK has finished initializing.
userReadonly<Ref<any>>The signed-in user, or null. Holds the attributes of the user's WSO2 Identity Server profile (SCIM 2.0), or the ID token claims if the profile can't be read.
organizationReadonly<Ref<Organization>>The organization the user is signed in to, with id, name and orgHandle taken from the ID token.

Methods​

MethodDescription
signIn(options?)Redirects the user to WSO2 Identity Server to sign in. options adds parameters to the authorization request, such as { prompt: 'login' }. Throws an AsgardeoRuntimeError if sign-in fails.
signOut()Clears the session in the app and redirects the user to WSO2 Identity Server to end their session there. WSO2 Identity Server then redirects to afterSignOutUrl.
signUp()Opens the WSO2 Identity Server sign-up page.
signInSilently(options?)Signs the user in without a redirect if they already have a session with WSO2 Identity Server, using a prompt=none request in a hidden iframe. Resolves to the user, or false if there is no session.
getAccessToken()Returns a promise that resolves to the access token.
getIdToken()Returns a promise that resolves to the raw ID token.
getDecodedIdToken()Returns a promise that resolves to the decoded payload of the ID token.
http.request(config)Sends an HTTP request with the user's access token. See Accessing protected APIs.
http.requestAll(configs)Sends several HTTP requests in parallel and resolves when all of them complete.
switchOrganization(organization)Switches the session to another organization with an organization_switch token exchange, then reloads the user.
exchangeToken(config)Runs a custom token exchange grant.
reInitialize(config)Updates the SDK configuration with the given values. Resolves to true on success.
clearSession()Clears the session stored in the app without contacting WSO2 Identity Server.

Configuration values​

PropertyTypeDescription
clientIdstringThe clientId passed to <AsgardeoProvider>.
baseUrlstringThe baseUrl passed to <AsgardeoProvider>.
afterSignInUrlstringThe URL that WSO2 Identity Server redirects to after sign-in.
applicationIdstringThe applicationId passed to <AsgardeoProvider>.
organizationHandlestringThe organizationHandle passed to <AsgardeoProvider>.
signInUrlstringThe signInUrl passed to <AsgardeoProvider>.
signUpUrlstringThe signUpUrl passed to <AsgardeoProvider>.
signInOptionsSignInOptionsThe signInOptions passed to <AsgardeoProvider>.
storagestringThe storage passed to <AsgardeoProvider>.
instanceIdnumberThe ID of the SDK instance.

Errors​

If you call useAsgardeo() in a component that isn't rendered inside <AsgardeoProvider>, it throws:

[Asgardeo] useAsgardeo() was called outside of <AsgardeoProvider>. Make sure to install the AsgardeoPlugin or wrap your app with <AsgardeoProvider>.
tip

The component whose template renders <AsgardeoProvider> is outside the provider. Move code that calls useAsgardeo() into a child component.

Was this page helpful?