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

vuecloak

v1.2.0

Published

A Keycloak plugin for Vue 3

Downloads

16

Readme

vuecloak

Vuecloak uses the official Keycloak JS Adapter.

Installation

Using npm

npm i -S vuecloak

Using Yarn

yarn add vuecloak

Get started

Config

It is mandatory to provide a config object containing your Keycloak url, realm and clientId.

import { Vuecloak } from 'vuecloak'

app
  .use(Vuecloak, {
    config: {
      url: 'KEYCLOAK_URL',
      realm: 'KEYCLOAK_REALM',
      clientId: 'KEYCLOAK_CLIENT_ID'
    }
  })

Init options

You can provide custom initialization options to Keycloak adapter through init property.

app
  .use(Vuecloak, {
    init: {
      onLoad: 'login-required',
      checkLoginIframe: false
    }
  })

Callback events

Vuecloak allows you to listen to Keycloak callback events.

|Key|Type| |---|---| |onReady|Function(keycloak)| |onAuthSuccess|Function(keycloak)| |onAuthError|Function(keycloak)| |onAuthRefreshSuccess|Function(keycloak)| |onAuthRefreshError|Function(keycloak)| |onAuthLogout|Function(keycloak)| |onTokenExpired|Function(keycloak)| |onInitSuccess|Function(authenticated)| |onInitError|Function(error)|

You can use this way:

app
  .use(Vuecloak, {
    onReady (keycloak) {
      ...
    },
    onInitSuccess (authenticated) {
      ...
    },
  })

Usage

Vuecloak adds a $keycloak property with its properties and methods to global Vue instance for you to use within your templates.

<template>
  <button @click="$keycloak.logout">
    Logout
  </button>
</template>

Inject

You can add $keycloak instance to your Vue setup too using inject option.

import { inject, onBeforeMount } from 'vue'

export default {
  setup () {
    const keycloak = inject('$keycloak')

    onBeforeMount(() => {
      keycloak.loadUserInfo()
        .then((user) => {
          ...
        })
    })
  }
})

Update token

Vuecloak has no strategy for keeping your tokens valid, so you need to do this on your own. A good way is to check it before API calls.

axios.interceptors.request.use(async config => {
  await app.config.globalProperties.$keycloak.updateToken()

  config.headers.common.Authorization = `Bearer ${app.config.globalProperties.$keycloak.token}`

  return config
})

Full example

app
  .use(Vuecloak, {
    config: {
      url: 'KEYCLOAK_URL',
      realm: 'KEYCLOAK_REALM',
      clientId: 'KEYCLOAK_CLIENT_ID'
    },
    init: {
      onLoad: 'login-required',
      checkLoginIframe: false
    },
    onReady (keycloak) {...},
    onAuthSuccess (keycloak) {...},
    onAuthError (keycloak) {...},
    onAuthRefreshSuccess (keycloak) {...},
    onAuthRefreshError (keycloak) {...},
    onAuthLogout (keycloak) {...},
    onTokenExpired (keycloak) {...},
    onInitSuccess (authenticated) {...},
    onInitError (error) {...},
  })

Debug

Vuecloak uses the power of Vue Devtools to provide a great developer experience.

Vue devtools support

Only available in Vue Devtools 6+