diff --git a/front/my-vue-app/env.d.ts b/front/my-vue-app/env.d.ts new file mode 100644 index 0000000..11f02fe --- /dev/null +++ b/front/my-vue-app/env.d.ts @@ -0,0 +1 @@ +/// diff --git a/front/my-vue-app/src/helpers.ts b/front/my-vue-app/src/helpers.ts index 712236e..0bd894a 100644 --- a/front/my-vue-app/src/helpers.ts +++ b/front/my-vue-app/src/helpers.ts @@ -3,15 +3,15 @@ import axios from "axios"; const backendUrl = "http://localhost:8080/" // TODO: spawn a error modal -function defaultApiErrorHandler(err){ +function defaultApiErrorHandler(err: String){ console.log(err) } -function defaultApiSuccessHandler(response){ +function defaultApiSuccessHandler(response: () => void){ console.log(response) } - -function callApi(endpoint: string, onSuccessHandler, onErrorHandler): void { +/* +function callApi(endpoint: string, onSuccessHandler: () => void, onErrorHandler: (err: String) => void): void { console.log("callApi: "+ endpoint) axios.get(backendUrl + endpoint).then( onSuccessHandler == null ? defaultApiSuccessHandler : onSuccessHandler @@ -22,4 +22,5 @@ function callApi(endpoint: string, onSuccessHandler, onErrorHandler): void { ) } -export {callApi} \ No newline at end of file +export {callApi} + */ \ No newline at end of file diff --git a/front/my-vue-app/src/plugins/authStore.ts b/front/my-vue-app/src/plugins/authStore.ts index aba3b9e..1a5c06e 100644 --- a/front/my-vue-app/src/plugins/authStore.ts +++ b/front/my-vue-app/src/plugins/authStore.ts @@ -4,7 +4,7 @@ import { useAuthStore } from "@/stores/authStore.ts"; import keycloakService from '@/services/keycloak'; // Setup auth store as a plugin so it can be accessed globally in our FE const authStorePlugin = { - install(app, option) { + install(app: any, option: any) { const store = useAuthStore(option.pinia); app.config.globalProperties.$store = store; keycloakService.CallInitStore(store); diff --git a/front/my-vue-app/src/router/index.ts b/front/my-vue-app/src/router/index.ts index 30568f5..e9697d9 100644 --- a/front/my-vue-app/src/router/index.ts +++ b/front/my-vue-app/src/router/index.ts @@ -1,14 +1,8 @@ import { createRouter, createWebHistory } from 'vue-router' -import HomeView from '../views/HomeView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ - { - path: '/', - name: 'home', - component: HomeView, - }, { path: '/about', name: 'about', @@ -17,15 +11,6 @@ const router = createRouter({ // which is lazy-loaded when the route is visited. component: () => import('../views/AboutView.vue'), }, - { - path: '/random', - name: 'random', - // route level code-splitting - // this generates a separate chunk (About.[hash].js) for this route - // which is lazy-loaded when the route is visited. - component: () => import('../views/Random.vue'), - }, - { path: '/test', name: 'test', diff --git a/front/my-vue-app/src/services/keycloak.ts b/front/my-vue-app/src/services/keycloak.ts index 7d04eed..6ca32df 100644 --- a/front/my-vue-app/src/services/keycloak.ts +++ b/front/my-vue-app/src/services/keycloak.ts @@ -8,7 +8,7 @@ const options = { const keycloak = new Keycloak(options); -let authenticated; +let authenticated: boolean | undefined; let store = null; async function login(){ @@ -36,7 +36,7 @@ async function signup(){ * * @param onInitCallback */ -async function init(onInitCallback) { +async function init(onInitCallback: () => void) { try { authenticated = await keycloak.init({ onLoad: "check-sso", @@ -54,7 +54,7 @@ async function init(onInitCallback) { * Initializes store with Keycloak user data * */ -async function initStore(storeInstance) { +async function initStore(storeInstance: any) { try { store = storeInstance console.log(keycloak) @@ -71,7 +71,7 @@ async function initStore(storeInstance) { /** * Logout user */ -function logout(url) { +function logout(url: string) { keycloak.logout({ redirectUri: url }); } diff --git a/front/my-vue-app/src/stores/authStore.ts b/front/my-vue-app/src/stores/authStore.ts index 165d4a0..9b0f345 100644 --- a/front/my-vue-app/src/stores/authStore.ts +++ b/front/my-vue-app/src/stores/authStore.ts @@ -1,14 +1,15 @@ import { defineStore } from "pinia"; import keycloakService from '@/services/keycloak'; +import type Keycloak from "keycloak-js"; export const useAuthStore = defineStore("storeAuth", { state: () => { return { testv: true, authenticated: false, user: { - token: null, - refreshToken: null, - username: null, + token: "", + refreshToken: "", + username: "", }, } }, @@ -16,11 +17,11 @@ export const useAuthStore = defineStore("storeAuth", { getters: {}, actions: { // Initialize Keycloak OAuth - async initOauth(keycloak, clearData = true) { + async initOauth(keycloak: Keycloak, clearData = true) { if(clearData) { await this.clearUserData(); } - this.authenticated = keycloak.authenticated; - if (this.authenticated){ + this.authenticated = !!keycloak.authenticated; // the !! removes undefined + if (this.authenticated && keycloak.token && keycloak.idTokenParsed && keycloak.refreshToken){ this.user.username = keycloak.idTokenParsed.given_name; this.user.token = keycloak.token; this.user.refreshToken = keycloak.refreshToken; @@ -29,7 +30,8 @@ export const useAuthStore = defineStore("storeAuth", { async login(){ try { const keycloak = await keycloakService.callLogin(); - await this.initOauth(keycloak); + if (keycloak) + await this.initOauth(keycloak); } catch (error) { console.log(error) } @@ -37,7 +39,8 @@ export const useAuthStore = defineStore("storeAuth", { async signup() { try { const keycloak = await keycloakService.callSignup(); - await this.initOauth(keycloak); + if (keycloak) + await this.initOauth(keycloak); } catch (error) { console.log(error) } @@ -55,7 +58,8 @@ export const useAuthStore = defineStore("storeAuth", { async refreshUserToken() { try { const keycloak = await keycloakService.CallTokenRefresh(); - await this.initOauth(keycloak, false); + if (keycloak) + await this.initOauth(keycloak, false); } catch (error) { console.error(error); } @@ -66,7 +70,11 @@ export const useAuthStore = defineStore("storeAuth", { // Clear user's store data clearUserData() { this.authenticated = false; - this.user = {}; + this.user = { + token: "", + refreshToken: "", + username: "", + }; } } }); \ No newline at end of file diff --git a/front/my-vue-app/src/views/HomeView.vue b/front/my-vue-app/src/views/HomeView.vue deleted file mode 100644 index a06d2bb..0000000 --- a/front/my-vue-app/src/views/HomeView.vue +++ /dev/null @@ -1,62 +0,0 @@ - - - - - \ No newline at end of file diff --git a/front/my-vue-app/src/views/Random.vue b/front/my-vue-app/src/views/Random.vue deleted file mode 100644 index 8d2d5a3..0000000 --- a/front/my-vue-app/src/views/Random.vue +++ /dev/null @@ -1,38 +0,0 @@ - - - - - \ No newline at end of file