1.5.0 • Published 3 years ago

@baloise/vue-keycloak v1.5.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 years ago

vue-keycloak

A small wrapper library for the Keycloak JavaScript adapter.

The library is made for Vue 3.x.x and the Composiotion API.

Instalation

Install the keycloak-js package , jwt-decode to decode the jwt token and our wrapper library with npm.

npm install keycloak-js jwt-decode @baloise/vue-keycloak

Use plugin

Import the library into your src/main.ts file or any other entry point.

import { vueKeycloak } from '@baloise/vue-keycloak'

Apply the library to the vue app instance.

const app = createApp(App)

app.use(vueKeycloak, {
  initOptions: {
    flow: 'standard', // default
    checkLoginIframe: false, // default
    onLoad: 'login-required', // default
  }
  config: {
    url: 'http://keycloak-server/auth',
    realm: 'myrealm',
    clientId: 'myapp'
  }
})

Or use a JSON file with the configs.

app.use(vueKeycloak, '/keycloak.json')

Configuration

ConfigTypeDescription
initOptionsKeycloak.KeycloakInitOptionsinitOptions is Keycloak init options.
configKeycloak.KeycloakConfigconfig are the Keycloak configuration.

Use the example below to generate dynamic Keycloak conifiguration.

app.use(vueKeycloak, async () => {
  return {
    config: {
      url: (await getAuthBaseUrl()) + '/auth',
      realm: 'myrealm',
      clientId: 'myapp',
    },
    initOptions: {
      onLoad: 'check-sso',
      silentCheckSsoRedirectUri: window.location.origin + '/assets/silent-check-sso.html',
    },
  }
})

It is also possible to access the keycloak instance with getKeycloak()

Use Token

We export two helper functions for the token.

getToken

This function checks if the token is still valid and will update it if it is expired.

Have a look at our vueAxios plugin.

import { $axios } from '@baloise/vue-axios'
import { getToken } from '@baloise/vue-keycloak'

const axiosApiInstance = $axios.create()

// Request interceptor for API calls
axiosApiInstance.interceptors.request.use(
  async config => {
    const token = await getToken()
    config.headers = {
      Authorization: `Bearer ${token}`,
    }
    return config
  },
  error => {
    Promise.reject(error)
  },
)

Composition API

import { computed, defineComponent } from 'vue'
import { useKeycloak } from '@baloise/vue-keycloak'

export default defineComponent({
  setup() {
    const { hasRoles, isPending } = useKeycloak()

    const hasAccess = computed(() => hasRoles(['RoleName']))

    return {
      hasAccess,
    }
  },
})

useKeycloak

The useKeycloak function exposes the following reactive state.

import { useKeycloak } from '@baloise/vue-keycloak'

const {
  isAuthenticated,
  isPending,
  hasFailed,
  token,
  decodedToken,
  username,
  roles,
  keycloak,

  // Functions
  hasRoles,
} = useKeycloak()
StateTypeDescription
isAuthenticatedRef<boolean>If true the user is authenticated.
isPendingRef<boolean>If true the authentication request is still pending.
hasFailedRef<boolean>If true authentication request has failed.
tokenRef<string>token is the raw value of the JWT token.
decodedTokenRef<T>decodedToken is the decoded value of the JWT token.
usernameRef<string>username the name of our user.
rolesRef<string[]>roles is a list of the users roles.
keycloakKeycloak.KeycloakInstancekeycloak is the instance of the keycloak-js adapter.

Functions

FunctionTypeDescription
hasRoles(roles: string[]) => booleanhasRoles returns true if the user has all the given roles.

License

Apache-2.0 Licensed | Copyright © 2021-present Gery Hirschfeld & Contributors

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago