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

@josempgon/vue-keycloak

v3.0.1

Published

Keycloak plugin for Vue 3 with Composition API

Downloads

3,247

Readme

vue-keycloak

NPM Version npm bundle size NPM Downloads

A small Vue wrapper library for the Keycloak JavaScript adapter.

This library is made for Vue 3 with the Composition API.

Instalation

Install the library with npm.

npm install @josempgon/vue-keycloak

Use Plugin

Import the library into your Vue app entry point.

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

Apply the library to the Vue app instance.

const app = createApp(App)

app.use(vueKeycloak, {
  config: {
    url: 'http://keycloak-server/auth',
    realm: 'myrealm',
    clientId: 'myapp',
  }
})

Configuration

| Object | Type | Required | Description | | ----------- | --------------------------------------------- | -------- | ---------------------------------------- | | config | KeycloakConfig | Yes | Keycloak configuration. | | initOptions | KeycloakInitOptions | No | Keycloak init options. |

initOptions Default Value

{
  flow: 'standard',
  checkLoginIframe: false,
  onLoad: 'login-required',
}

Dynamic Keycloak Configuration

Use the example below to generate dynamic Keycloak configuration.

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

Use with vue-router

If you need to wait for authentication to complete before proceeding with your Vue app setup, for instance, because you are using the vue-router package and need to initialize the router only after the authentication process is completed, you should initialize your app in the following way:

router/index.ts

import { createRouter, createWebHistory } from 'vue-router'

const routes = [ /* Your routes */ ]

const initRouter = () => {
  const history = createWebHistory(import.meta.env.BASE_URL)
  return createRouter({ history, routes })
}

export { initRouter }

main.ts

import { createApp } from 'vue'
import { vueKeycloak } from '@josempgon/vue-keycloak'
import App from './App.vue'
import { initRouter } from './router'

const app = createApp(App)

await vueKeycloak.install(app, {
  config: {
    url: 'http://keycloak-server/auth',
    realm: 'myrealm',
    clientId: 'myapp',
  },
})

app.use(initRouter())
app.mount('#app')

If you are building for a browser that does not support Top-level await, you should wrap the Vue plugin and router initialization in an async IIFE:

(async () => {
  await vueKeycloak.install(app, options);

  app.use(initRouter());
  app.mount('#app');
})();

Use Token

A helper function is exported to manage the access token.

getToken

| Function | Type | Description | | ---------------- | ------------------------------------------------------ | ---------------------------------------------------------------------- | | getToken | (minValidity?: number) => Promise<string> | Returns a promise that resolves with the current access token. |

The token will be refreshed if expires within minValidity seconds. The minValidity parameter is optional and defaults to 10. If -1 is passed as minValidity, the token will be forcibly refreshed.

A typical usage for this function is to be called before every API call, using a request interceptor in your HTTP client library.

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

// Create an instance of axios with the base URL read from the environment
const baseURL = import.meta.env.VITE_API_URL
const instance = axios.create({ baseURL })

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

Composition API

<script setup>
import { computed } from 'vue'
import { useKeycloak } from '@josempgon/vue-keycloak'

const { hasRoles } = useKeycloak()

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

useKeycloak

The useKeycloak function exposes the following data.

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

const {
  // Reactive State
  keycloak,
  isAuthenticated,
  isPending,
  hasFailed,
  token,
  decodedToken,
  username,
  userId,
  roles,
  resourceRoles,

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

Reactive State

| State | Type | Description | | --------------- | ------------------------------------------------------ | ------------------------------------------------------------------- | | keycloak | Ref<Keycloak> | Instance of the keycloak-js adapter. | | 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> | Raw value of the access token. | | decodedToken | Ref<KeycloakTokenParsed> | Decoded value of the access token. | | username | Ref<string> | Username. Extracted from decodedToken['preferred_username']. | | userId | Ref<string> | User identifier. Extracted from decodedToken['sub']. | | roles | Ref<string[]> | List of the user's roles. | | resourceRoles | Ref<Record<string, string[]> | List of the user's roles in specific resources. |

Functions

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

License

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