Skip to content

[question] Why and how do I prevent a blank page when the Keycloak server is not available? Vue, Keycloak, Quasar #179

@Excel1

Description

@Excel1

Im relative new to vue and i am trying to connect my Vue Application based on Quasar with Keycloak. I followed this tutorial and used the dsb-norge/vue-keycloak-js library.

Everything works in case, the Keycloak server is reachable.

Now, of course, it is possible that the server is unreachable for some reason, such as DNS problems. In this case i got a blank white page instead of showing the page without working login button. With using the onInit Option from the library i can catch the error but very lately and still the page is blank.

How can the page still loaded and show content just without a working login button if the keycloak server is unreachable/not able to process my request?

//keycloak.ts
import VueKeyCloak from '@dsb-norge/vue-keycloak-js'
import { boot } from 'quasar/wrappers';
import { VueKeycloakInstance } from '@dsb-norge/vue-keycloak-js/dist/types';
import { api } from 'boot/axios';
import {InternalAxiosRequestConfig} from 'axios';

export default boot(async ({ app}) => {
  function registerTokenInterceptor () {
    app.provide('keycloak', app.config.globalProperties.$keycloak);

    api.interceptors.request.use((config: InternalAxiosRequestConfig) => {
      if (app.config.globalProperties.$keycloak.authenticated) {
        config.headers.Authorization = `Bearer ${app.config.globalProperties.$keycloak.token}`
      }
      return config
    }, error => {
      return Promise.reject(error)
    }) //null, { synchronous: true })
  }

  return new Promise(resolve => {
    app.use(VueKeyCloak, {
      init: {
        checkLoginIframe: false, // or 'login-required'
        flow: 'standard',
        onLoad: 'check-sso',
        pkceMethod: 'S256',
        silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html'
      },
      config: {
        url: 'http://localhost:8080/auth',
        realm: 'master',
        clientId: 'testclient',
      },
      logout: {
        redirectUri: 'http://localhost:9000',
      },
      onReady() {
        registerTokenInterceptor()
        resolve()
      }
    })
  })
})

declare module '@vue/runtime-core' {

  interface ComponentCustomProperties  {
    $keycloak: VueKeycloakInstance
  }
}```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions