I am working on an app with frontend using Vue 3. I am initializing a keycloak-js configuration in my main.ts file. I login successfully with my Keycloak running on Docker. I receive the JWT token. Afterwards, I save my token in localStorage so that when I close the browser I don't need to do another login. Up until this point everything is working perfectly. However, when I get the token from localStorage I save in my Pinia store in order to be accessible in one of my Vue components. Unfortunately, when I open my frontend app Vue loads first the NavigationBar.vue and afterwords the main.ts file. This causes an issue because the property in my Pinia store is still empty (the logic for populating it is in main.ts).
Can you give me any suggestions how can I make Vue to load first everything from main.ts and then start rendering the vue components?
I am attaching code so that you can see what I've done.
main.ts
import { createApp } from 'vue'
import './style.css'
import Keycloak from "keycloak-js";
import { createPinia } from 'pinia'
import {useAuthStore} from "./store/auth";
// @ts-ignore
import App from './App.vue'
const pinia = createPinia()
const app = createApp(App);
app.use(pinia);
let keycloak = new Keycloak({
url: "http://192.168.0.2:8082/",
realm: "test",
clientId: "test"
});
keycloak.init({
redirectUri: "http://localhost:5173",
onLoad: "check-sso",
checkLoginIframe: false,
// @ts-ignore
token: localStorage.getItem("token"),
// @ts-ignore
refreshToken: localStorage.getItem("refreshToken")
}).then(() => {
const authStore = useAuthStore();
authStore.storedToken = keycloak.token as string;
console.log("main.ts check authStore, storedToken: " + keycloak.token);
if (keycloak != undefined && keycloak.authenticated) {
if (keycloak.token != undefined) {
localStorage.setItem("token", keycloak.token)
}
if (keycloak.refreshToken != undefined) {
localStorage.setItem("refreshToken", keycloak.refreshToken)
}
}
console.log(keycloak);
});
app.mount('#app');
export default keycloak;
NavigationBar.vue
<script lang="ts">
import keycloak from "../main";
import {useAuthStore} from "../store/auth";
export default {
name: "NavigationBar",
mounted() {
const authStore = useAuthStore();
console.log("authStore.storedToken " + authStore.storedToken)
this.userToken = authStore.storedToken
},
data() {
return {
userToken: ""
}
},
methods: {
keycloak() {
return keycloak
}
}
}
</script>
App.vue
<script setup lang="ts">
import NavigationBar from "./components/NavigationBar.vue";
</script>
<template>
<NavigationBar></NavigationBar>
</template>
<style scoped>
</style>
Output from the console of Firefox browser
[vite] connecting... client.ts:18:8
[vite] connected. client.ts:133:14
authStore.storedToken NavigationBar.vue:69:16
main.ts check authStore, storedToken: eyJhbGciOiJSUzI1NiIsInR5cCI... main.ts:29:12
Object { init: init(initOptions), login: login(options), createLoginUrl: createLoginUrl(options), logout: logout(options), createLogoutUrl: createLogoutUrl(options), register: register(options), createRegisterUrl: createRegisterUrl(options), createAccountUrl: createAccountUrl(options), accountManagement: accountManagement(), hasRealmRole: hasRealmRole(role)
, … }
main.ts:39:12