fix: front end now compile

This commit is contained in:
Pierre Tellier 2025-02-03 22:48:17 +01:00
parent 23e6c7f9bb
commit 89d7b2d342
8 changed files with 30 additions and 135 deletions

1
front/my-vue-app/env.d.ts vendored Normal file
View File

@ -0,0 +1 @@
/// <reference types="vite/client" />

View File

@ -3,15 +3,15 @@ import axios from "axios";
const backendUrl = "http://localhost:8080/" const backendUrl = "http://localhost:8080/"
// TODO: spawn a error modal // TODO: spawn a error modal
function defaultApiErrorHandler(err){ function defaultApiErrorHandler(err: String){
console.log(err) console.log(err)
} }
function defaultApiSuccessHandler(response){ function defaultApiSuccessHandler(response: () => void){
console.log(response) 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) console.log("callApi: "+ endpoint)
axios.get(backendUrl + endpoint).then( axios.get(backendUrl + endpoint).then(
onSuccessHandler == null ? defaultApiSuccessHandler : onSuccessHandler onSuccessHandler == null ? defaultApiSuccessHandler : onSuccessHandler
@ -22,4 +22,5 @@ function callApi(endpoint: string, onSuccessHandler, onErrorHandler): void {
) )
} }
export {callApi} export {callApi}
*/

View File

@ -4,7 +4,7 @@ import { useAuthStore } from "@/stores/authStore.ts";
import keycloakService from '@/services/keycloak'; import keycloakService from '@/services/keycloak';
// Setup auth store as a plugin so it can be accessed globally in our FE // Setup auth store as a plugin so it can be accessed globally in our FE
const authStorePlugin = { const authStorePlugin = {
install(app, option) { install(app: any, option: any) {
const store = useAuthStore(option.pinia); const store = useAuthStore(option.pinia);
app.config.globalProperties.$store = store; app.config.globalProperties.$store = store;
keycloakService.CallInitStore(store); keycloakService.CallInitStore(store);

View File

@ -1,14 +1,8 @@
import { createRouter, createWebHistory } from 'vue-router' import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
const router = createRouter({ const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
routes: [ routes: [
{
path: '/',
name: 'home',
component: HomeView,
},
{ {
path: '/about', path: '/about',
name: 'about', name: 'about',
@ -17,15 +11,6 @@ const router = createRouter({
// which is lazy-loaded when the route is visited. // which is lazy-loaded when the route is visited.
component: () => import('../views/AboutView.vue'), 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', path: '/test',
name: 'test', name: 'test',

View File

@ -8,7 +8,7 @@ const options = {
const keycloak = new Keycloak(options); const keycloak = new Keycloak(options);
let authenticated; let authenticated: boolean | undefined;
let store = null; let store = null;
async function login(){ async function login(){
@ -36,7 +36,7 @@ async function signup(){
* *
* @param onInitCallback * @param onInitCallback
*/ */
async function init(onInitCallback) { async function init(onInitCallback: () => void) {
try { try {
authenticated = await keycloak.init({ authenticated = await keycloak.init({
onLoad: "check-sso", onLoad: "check-sso",
@ -54,7 +54,7 @@ async function init(onInitCallback) {
* Initializes store with Keycloak user data * Initializes store with Keycloak user data
* *
*/ */
async function initStore(storeInstance) { async function initStore(storeInstance: any) {
try { try {
store = storeInstance store = storeInstance
console.log(keycloak) console.log(keycloak)
@ -71,7 +71,7 @@ async function initStore(storeInstance) {
/** /**
* Logout user * Logout user
*/ */
function logout(url) { function logout(url: string) {
keycloak.logout({ redirectUri: url }); keycloak.logout({ redirectUri: url });
} }

View File

@ -1,14 +1,15 @@
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import keycloakService from '@/services/keycloak'; import keycloakService from '@/services/keycloak';
import type Keycloak from "keycloak-js";
export const useAuthStore = defineStore("storeAuth", { export const useAuthStore = defineStore("storeAuth", {
state: () => { state: () => {
return { return {
testv: true, testv: true,
authenticated: false, authenticated: false,
user: { user: {
token: null, token: "",
refreshToken: null, refreshToken: "",
username: null, username: "",
}, },
} }
}, },
@ -16,11 +17,11 @@ export const useAuthStore = defineStore("storeAuth", {
getters: {}, getters: {},
actions: { actions: {
// Initialize Keycloak OAuth // Initialize Keycloak OAuth
async initOauth(keycloak, clearData = true) { async initOauth(keycloak: Keycloak, clearData = true) {
if(clearData) { await this.clearUserData(); } if(clearData) { await this.clearUserData(); }
this.authenticated = keycloak.authenticated; this.authenticated = !!keycloak.authenticated; // the !! removes undefined
if (this.authenticated){ if (this.authenticated && keycloak.token && keycloak.idTokenParsed && keycloak.refreshToken){
this.user.username = keycloak.idTokenParsed.given_name; this.user.username = keycloak.idTokenParsed.given_name;
this.user.token = keycloak.token; this.user.token = keycloak.token;
this.user.refreshToken = keycloak.refreshToken; this.user.refreshToken = keycloak.refreshToken;
@ -29,7 +30,8 @@ export const useAuthStore = defineStore("storeAuth", {
async login(){ async login(){
try { try {
const keycloak = await keycloakService.callLogin(); const keycloak = await keycloakService.callLogin();
await this.initOauth(keycloak); if (keycloak)
await this.initOauth(keycloak);
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }
@ -37,7 +39,8 @@ export const useAuthStore = defineStore("storeAuth", {
async signup() { async signup() {
try { try {
const keycloak = await keycloakService.callSignup(); const keycloak = await keycloakService.callSignup();
await this.initOauth(keycloak); if (keycloak)
await this.initOauth(keycloak);
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }
@ -55,7 +58,8 @@ export const useAuthStore = defineStore("storeAuth", {
async refreshUserToken() { async refreshUserToken() {
try { try {
const keycloak = await keycloakService.CallTokenRefresh(); const keycloak = await keycloakService.CallTokenRefresh();
await this.initOauth(keycloak, false); if (keycloak)
await this.initOauth(keycloak, false);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
@ -66,7 +70,11 @@ export const useAuthStore = defineStore("storeAuth", {
// Clear user's store data // Clear user's store data
clearUserData() { clearUserData() {
this.authenticated = false; this.authenticated = false;
this.user = {}; this.user = {
token: "",
refreshToken: "",
username: "",
};
} }
} }
}); });

View File

@ -1,62 +0,0 @@
<script>
import api from "../services/api";
import {useAuthStore} from "@/stores/authStore";
export default {
data() {
return {
store: useAuthStore(),
loading: false,
blogLink: import.meta.env.VITE_BLOG_LINK
};
},
created() {
},
methods: {
}
}
</script>
<template>
<div class="card">
<button
class="mr-15"
type="button"
title="Refreshes user token"
@click="store.refreshUserToken"
>
Refresh Token
</button>
<button
class="mr-15"
type="button"
title="Refreshes user token"
@click="store.login"
>
login
</button>
<!-- Logout button -->
<button
type="button"
title="Logout Keycloak user"
@click="store.logout"
>
Logout
</button>
</div>
<div class="card py-10">
<h2>Keycloak User</h2>
<p>Auth: {{store.authenticated}}</p>
<p class="my-5">Username: {{ store.user.username }}</p>
<p class="my-5">Token:</p>
<p class="wrap-text font-small my-5">{{ store.user.token }}</p>
<p class="my-5">Refresh Token:</p>
<p class="wrap-text font-small my-5">{{ store.user.refToken }}</p>
</div>
</template>
<style scoped>
</style>

View File

@ -1,38 +0,0 @@
<script lang="ts">
import axios from 'axios'
import {callApi} from "@/helpers.ts";
import ErrorModal from "@/views/errorModal.vue";
export default {
components: {ErrorModal},
inject: ['callApi'],
data() {
return {
posts: null
}
},
methods: {
async getRandom() {
this.posts = await axios.get('http://localhost:8080/random')
},
async getUserInfo() {
//this.posts = await axios.get('http://localhost:8080/getUserInfo')
callApi("getUserInfo");
}
}
}
</script>
<template>
<button @click="getRandom()">get a random value</button>
<button @click="getUserInfo()">get user info</button>
<p> Res: {{this.posts?.data}}</p>
<error-modal error="test"></error-modal>
</template>
<style scoped>
</style>