# Exercise 2: Authentication in Vue.js fronted application

There are several ways to use Keycloak from web applications. The easiest option is to use the official Keycloak JavaScript client library which is defined as dependency in package.json.

The shows the simplified architecture:

![](https://1980280846-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGEw-4BAG72jtDCP4q%2Fsync%2F05e7cca42c1d63b2ed17fcb3289d8fdc3bf35f42.png?generation=1601889007225927\&alt=media)

The Vue.js application triggers the authentication directly when the application is opened. See the file main.js:

```javascript
import Keycloak from 'keycloak-js';

let initOptions = {
  url: 'https://keycloak-default.niklas-heidloff-b3c-4x16-162e406f043e20da9b0ef0731954a894-0000.us-south.containers.appdomain.cloud/auth',
    realm: 'quarkus', clientId: 'frontend', onLoad: 'login-required'
}

Vue.config.productionTip = false
Vue.config.devtools = true
Vue.use(BootstrapVue);

let keycloak = Keycloak(initOptions);
keycloak.init({ onLoad: initOptions.onLoad }).then((auth) => {
  if (!auth) {
    window.location.reload();
  }

  new Vue({
    store,
    router,
    render: h => h(App)
  }).$mount('#app')

  let payload = {
    idToken: keycloak.idToken,
    accessToken: keycloak.token
  }
  if (keycloak.token && keycloak.idToken && keycloak.token != '' && keycloak.idToken != '') {
    store.commit("login", payload);
    console.log("User has logged in: " + keycloak.subject)
  }
  else {
    store.commit("logout");
  }
```

In order to use the Keycloak API, three pieces of information are required. The `Keycloak URL`, the `realm` and the `client id`.

As you see in the image below the `Vuex store` saves `access token`, `id token` and `user name`. When the tokens expire, new tokens are requested via the refresh token und the Vuex store is updated.

![](https://1980280846-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGEw-4BAG72jtDCP4q%2Fsync%2Fd4d7210acf5644996aa993253032954bf34b1cae.png?generation=1598017424580733\&alt=media)

[Related blog post](http://heidloff.net/article/securing-vue-js-applications-keycloak/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ibm-developer.gitbook.io/get-started-with-security-for-your-java-microservi/authentication-and-authorization-with-keycloak-and-quarkus/application_authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
