#Has anyone gotten Nuxt 3 to work with Keycloak?

3 messages · Page 1 of 1 (latest)

simple eagle
#

I'm having a lot of trouble getting it to work, I was hoping to use Nuxt Auth but that's not done yet and I don't know when it might be out, but we're already talking about Nuxt 4? Am I missing something here?

rich pewter
#

Just saw this message, yes I'm using keycloak right now, except it only works on the client so only static sites.

#
// plugins/keycloak.client.ts
import Keycloak from 'keycloak-js'

export default defineNuxtPlugin(async () => {
  const config = useRuntimeConfig()
  // define new keycloak
  const keycloak = new Keycloak({
    url: config.public.keycloakAuthUrl,
    realm: config.public.keycloakRealm,
    clientId: config.public.keycloakClientId
  })

  try {
    // init keycloak instance
    await keycloak.init({
      onLoad: 'check-sso',
      responseMode: 'query',
      pkceMethod: 'S256'
    })

  } catch (error) {
    console.error('Failed to initialize Keycloak adapter: ', error)
  }

  return {
    provide: {
      // provide global keycloak instance
      keycloak
    }
  }
})

then in your app you can call const {$keycloak} = useNuxtApp()