npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@virusxtech/vue-keycloak

v1.0.3

Published

Keycloak plugin for Vue3 and Composition API

Downloads

14

Readme

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 @virusxtech/vue-keycloak

Use plugin

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

import { vueKeycloak } from '@virusxtech/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/',
    realm: 'myrealm',
    clientId: 'myapp',
  },
})

Or use a JSON file with the configs.

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

Configuration

| Config | Type | Description | | ----------- | ------------------------------ | ---------------------------------------- | | initOptions | Keycloak.KeycloakInitOptions | initOptions is Keycloak init options. | | config | Keycloak.KeycloakConfig | config 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.

import axios from 'axios'
import { getToken } from '@virusxtech/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 '@virusxtech/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 '@virusxtech/vue-keycloak'

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

  // Functions
  hasRoles,
  hasResourceRoles,
} = useKeycloak()

| State | Type | Description | | --------------- | ------------------------------ | ------------------------------------------------------------------- | | isAuthenticated | Ref<boolean> | If true the user is authenticated. | | isPending | Ref<boolean> | If true the authentication request is still pending. | | hasFailed | Ref<boolean> | If true authentication request has failed. | | token | Ref<string> | token is the raw value of the JWT token. | | decodedToken | Ref<T> | decodedToken is the decoded value of the JWT token. | | username | Ref<string> | username the name of our user. | | roles | Ref<string[]> | roles is a list of the users roles. | | resourceRoles | Ref<Record<string, string[]> | resourceRoles is a list of the users roles in specific resources. | | keycloak | Keycloak | keycloak is the instance of the keycloak-js adapter. |

Functions

| Function | Type | Description | | ---------------- | ------------------------------------------------ | ---------------------------------------------------------------------------------- | | hasRoles | (roles: string[]) => boolean | hasRoles returns true if the user has all the given roles. | | hasResourceRoles | (roles: string[], resource: string) => boolean | hasResourceRoles returns true if the user has all the given roles in a resource. |

License

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