* refactoring and fixes by login

feature: #4
This commit is contained in:
2023-01-04 00:25:46 +01:00
parent 46dd4ef361
commit 1eab4c586f
15 changed files with 262 additions and 255 deletions

23
package-lock.json generated
View File

@@ -21,7 +21,8 @@
"vue": "^3.2.45", "vue": "^3.2.45",
"vue-i18n": "^9.2.2", "vue-i18n": "^9.2.2",
"vue-router": "^4.1.6", "vue-router": "^4.1.6",
"vuex": "^4.0.2" "vuex": "^4.0.2",
"vuex-persistedstate": "^4.1.0"
}, },
"devDependencies": { "devDependencies": {
"@rushstack/eslint-patch": "^1.1.4", "@rushstack/eslint-patch": "^1.1.4",
@@ -3108,7 +3109,6 @@
"version": "4.2.2", "version": "4.2.2",
"resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz",
"integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==", "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==",
"dev": true,
"engines": { "engines": {
"node": ">=0.10.0" "node": ">=0.10.0"
} }
@@ -6913,6 +6913,12 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/shvl": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/shvl/-/shvl-2.0.3.tgz",
"integrity": "sha512-V7C6S9Hlol6SzOJPnQ7qzOVEWUQImt3BNmmzh40wObhla3XOYMe4gGiYzLrJd5TFa+cI2f9LKIRJTTKZSTbWgw==",
"deprecated": "older versions vulnerable to prototype pollution"
},
"node_modules/signal-exit": { "node_modules/signal-exit": {
"version": "3.0.7", "version": "3.0.7",
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz",
@@ -7434,6 +7440,19 @@
"vue": "^3.0.2" "vue": "^3.0.2"
} }
}, },
"node_modules/vuex-persistedstate": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/vuex-persistedstate/-/vuex-persistedstate-4.1.0.tgz",
"integrity": "sha512-3SkEj4NqwM69ikJdFVw6gObeB0NHyspRYMYkR/EbhR0hbvAKyR5gksVhtAfY1UYuWUOCCA0QNGwv9pOwdj+XUQ==",
"deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.",
"dependencies": {
"deepmerge": "^4.2.2",
"shvl": "^2.0.3"
},
"peerDependencies": {
"vuex": "^3.0 || ^4.0.0-rc"
}
},
"node_modules/walker": { "node_modules/walker": {
"version": "1.0.8", "version": "1.0.8",
"resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz", "resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz",

View File

@@ -22,7 +22,8 @@
"vue": "^3.2.45", "vue": "^3.2.45",
"vue-i18n": "^9.2.2", "vue-i18n": "^9.2.2",
"vue-router": "^4.1.6", "vue-router": "^4.1.6",
"vuex": "^4.0.2" "vuex": "^4.0.2",
"vuex-persistedstate": "^4.1.0"
}, },
"devDependencies": { "devDependencies": {
"@rushstack/eslint-patch": "^1.1.4", "@rushstack/eslint-patch": "^1.1.4",

View File

@@ -3,13 +3,13 @@ import store from '../store'
let keycloak = null let keycloak = null
export function keycloakSetup() { export async function keycloakSetup() {
keycloak = new Keycloak({ keycloak = new Keycloak({
url: 'https://auth.denysoft.eu/', url: 'https://auth.denysoft.eu/',
realm: 'FST', realm: 'FST',
clientId: 'frontend' clientId: 'frontend'
}) })
return keycloak await keycloak
.init({ .init({
onLoad: 'login-required', onLoad: 'login-required',
checkLoginIframe: false checkLoginIframe: false
@@ -32,23 +32,10 @@ export function keycloakSetup() {
.catch(function () { .catch(function () {
alert('failed to initialize') alert('failed to initialize')
}) })
} store.commit('initKeycloak', keycloak)
export function login() { store.commit('isAnonym', false)
keycloak.login()
} }
export function getKeycloak() { export function getKeycloak() {
return keycloak return keycloak
} }
export function isVerwaltung() {
return keycloak.hasRealmRole('verwaltung')
}
export function isEmployee() {
return keycloak.hasRealmRole('employee')
}
export function isAdmin() {
return keycloak.hasRealmRole('admin')
}

View File

@@ -7,19 +7,29 @@
</div> </div>
<div class="flex justify-around basis-1/3 max-md:hidden"> <div class="flex justify-around basis-1/3 max-md:hidden">
<div v-if="!isAnonym"> <div v-if="!isAnonym">
<RouterLink to="/contracts">
<p class="py-2">Aufträge</p> <p class="py-2">Aufträge</p>
</RouterLink>
</div> </div>
<div v-if="!isAnonym" class="nav-item"> <div v-if="!isAnonym" class="nav-item">
<RouterLink to="/support">
<p class="py-2">Support</p> <p class="py-2">Support</p>
</RouterLink>
</div> </div>
<div v-if="isVerwaltung" class="nav-item"> <div v-if="isVerwaltung" class="nav-item">
<RouterLink to="/administration">
<p class="py-2">Administration</p> <p class="py-2">Administration</p>
</RouterLink>
</div> </div>
<div v-if="isAnonym" class="nav-item"> <div v-if="isAnonym" class="nav-item">
<RouterLink to="/arbeiten">
<p class="py-2">Arbeiten</p> <p class="py-2">Arbeiten</p>
</RouterLink>
</div> </div>
<div v-if="isAnonym" class="nav-item"> <div v-if="isAnonym" class="nav-item">
<RouterLink to="/contact">
<p class="py-2">Kontakt</p> <p class="py-2">Kontakt</p>
</RouterLink>
</div> </div>
<div v-if="isAnonym" class="nav-item"> <div v-if="isAnonym" class="nav-item">
<RouterLink to="/about"> <RouterLink to="/about">
@@ -27,8 +37,12 @@
</RouterLink> </RouterLink>
</div> </div>
</div> </div>
<div v-if="isAnonym" class="nav-item basis-1/6 max-md:justify-end max-md:pr-5 max-md:flex max-md:basis-3/6"> <div
<button class="bg-blue-600 hover:bg-blue-500 duration-300 text-white px-3 py-1 my-2 rounded" @click="login"> v-if="isAnonym"
class="nav-item basis-1/6 max-md:justify-end max-md:pr-5 max-md:flex max-md:basis-3/6">
<button
class="bg-blue-600 hover:bg-blue-500 duration-300 text-white px-3 py-1 my-2 rounded"
@click="login">
Login Login
</button> </button>
</div> </div>
@@ -61,7 +75,9 @@
<RouterLink to="/about">Über uns</RouterLink> <RouterLink to="/about">Über uns</RouterLink>
</li> </li>
<li v-if="isAnonym" class="block py-2 flex justify-center"> <li v-if="isAnonym" class="block py-2 flex justify-center">
<button class="bg-blue-600 hover:bg-blue-500 duration-300 text-white px-3 py-1 my-2 rounded" @click="login"> <button
class="bg-blue-600 hover:bg-blue-500 duration-300 text-white px-3 py-1 my-2 rounded"
@click="login">
Login Login
</button> </button>
</li> </li>
@@ -69,44 +85,33 @@
</div> </div>
</template> </template>
<script> <script>
import {keycloakSetup} from "../authentication/AuthHelper"; import { getKeycloak, keycloakSetup } from '../authentication/AuthHelper'
export default { export default {
data() { data() {
return { return {
keycloak: this.$store.state.keycloak.keycloak, keycloak: this.$store.state.keycloak.keycloak,
isAnonym: false,
isNavbarClosed: true isNavbarClosed: true
} }
}, },
computed: { computed: {
isVerwaltung() { isVerwaltung() {
if (this.$store.state.keycloak.keycloak === null){ if (getKeycloak() === null) {
this.isAnonym = true;
return false return false
} }
return this.$store.state.keycloak.keycloak.realmAccess.roles.includes('verwaltung') return this.$store.state.keycloak.keycloak.realmAccess.roles.includes('verwaltung')
}, },
isAdmin() { isAnonym() {
if (this.$store.state.keycloak.keycloak === null){ return this.$store.state.keycloak.isAnonym
this.isAnonym = true;
return false
}
return this.$store.state.keycloak.keycloak.realmAccess.roles.includes('admin')
},
isEmployee() {
if (this.$store.state.keycloak.keycloak === null){
this.isAnonym = true;
return false
}
return this.$store.state.keycloak.keycloak.realmAccess.roles.includes('employeee')
} }
}, },
methods: { methods: {
login() { async login() {
keycloakSetup() if (this.$store.state.keycloak.keycloak === null) {
await keycloakSetup()
}
}, },
openNavbar(){ openNavbar() {
this.isNavbarClosed = !this.isNavbarClosed this.isNavbarClosed = !this.isNavbarClosed
} }
} }

View File

@@ -33,8 +33,8 @@ const router = createRouter({
} }
}, },
{ {
path: '/support', path: '/administration',
name: 'support', name: 'administration',
// this page is lazy-loaded when the route is visited. // this page is lazy-loaded when the route is visited.
component: () => import('../views/AdministrationView.vue'), component: () => import('../views/AdministrationView.vue'),
meta: { meta: {
@@ -87,12 +87,17 @@ const router = createRouter({
} }
] ]
}) })
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
//if user was logged in but reloaded page. So we have all data, need only restore it
if (store.state.keycloak.keycloak !== null && getKeycloak() === null) {
await keycloakSetup()
}
if (to.meta.requiresAuth === true) { if (to.meta.requiresAuth === true) {
if (getKeycloak() === null) { if (getKeycloak() === null) {
await keycloakSetup() await keycloakSetup()
store.commit('initKeycloak', getKeycloak()) store.commit('initKeycloak', getKeycloak())
store.commit('isAnonym', false)
} }
if (!getKeycloak().authenticated) { if (!getKeycloak().authenticated) {
next('/forbidden') next('/forbidden')

View File

@@ -2,11 +2,13 @@ import { createStore } from 'vuex'
import moduleA from './moduleA.module' import moduleA from './moduleA.module'
import moduleB from './moduleB.module' import moduleB from './moduleB.module'
import keycloak from './keycloak.module' import keycloak from './keycloak.module'
import createPersistedState from 'vuex-persistedstate'
export default createStore({ export default createStore({
modules: { modules: {
keycloak: keycloak, keycloak: keycloak,
a: moduleA, a: moduleA,
b: moduleB b: moduleB
} },
plugins: [createPersistedState()]
}) })

View File

@@ -1,20 +1,22 @@
export default { export default {
state: () => ({ state: () => ({
keycloak: null keycloak: null,
isAnonym: true
}), }),
mutations: { mutations: {
initKeycloak(state, data) { initKeycloak(state, data) {
state.keycloak = data state.keycloak = data
},
isAnonym(state, data) {
state.isAnonym = data
} }
}, },
actions: { actions: {
initKeycloak({ commit }) { initKeycloak({ commit }, data) {
commit('initKeycloak') commit('initKeycloak', data)
}
}, },
getters: { isAnonym({ commit }, data) {
getKeycloak(state) { commit('isAnonym', data)
return state.keycloak
} }
} }
} }

View File

@@ -1,17 +1,15 @@
<script setup> <script setup>
import Navbar from "../components/Navbar.vue"; import Navbar from '../components/Navbar.vue'
</script> </script>
<script> <script>
export default { export default {
mounted() { mounted() {}
}
} }
</script> </script>
<template> <template>
<Navbar/><!-- Causes rerendering each time todo: possible use in App.vue--> <Navbar /><!-- Causes rendering each time todo: possible use in App.vue-->
<div class="about"> <div class="about">
<h1>This is a About page</h1> <h1>This is a About page</h1>
</div> </div>
</template> </template>
<style> <style></style>
</style>

View File

@@ -1,17 +1,15 @@
<script setup> <script setup>
import Navbar from "../components/Navbar.vue"; import Navbar from '../components/Navbar.vue'
</script> </script>
<script> <script>
export default { export default {
mounted() { mounted() {}
}
} }
</script> </script>
<template> <template>
<Navbar/><!-- Causes rerendering each time todo: possible use in App.vue--> <Navbar /><!-- Causes rerendering each time todo: possible use in App.vue-->
<div class="about"> <div class="about">
<h1>This is an Administration page</h1> <h1>This is an Administration page</h1>
</div> </div>
</template> </template>
<style> <style></style>
</style>

View File

@@ -1,17 +1,15 @@
<script setup> <script setup>
import Navbar from "../components/Navbar.vue"; import Navbar from '../components/Navbar.vue'
</script> </script>
<script> <script>
export default { export default {
mounted() { mounted() {}
}
} }
</script> </script>
<template> <template>
<Navbar/><!-- Causes rerendering each time todo: possible use in App.vue--> <Navbar /><!-- Causes rerendering each time todo: possible use in App.vue-->
<div class="about"> <div class="about">
<h1>This is an Arbeiten page</h1> <h1>This is an Arbeiten page</h1>
</div> </div>
</template> </template>
<style> <style></style>
</style>

View File

@@ -1,17 +1,15 @@
<script setup> <script setup>
import Navbar from "../components/Navbar.vue"; import Navbar from '../components/Navbar.vue'
</script> </script>
<script> <script>
export default { export default {
mounted() { mounted() {}
}
} }
</script> </script>
<template> <template>
<Navbar/><!-- Causes rerendering each time todo: possible use in App.vue--> <Navbar /><!-- Causes rerendering each time todo: possible use in App.vue-->
<div class="about"> <div class="about">
<h1>This is an Contact page</h1> <h1>This is an Contact page</h1>
</div> </div>
</template> </template>
<style> <style></style>
</style>

View File

@@ -1,17 +1,15 @@
<script setup> <script setup>
import Navbar from "../components/Navbar.vue"; import Navbar from '../components/Navbar.vue'
</script> </script>
<script> <script>
export default { export default {
mounted() { mounted() {}
}
} }
</script> </script>
<template> <template>
<Navbar/><!-- Causes rerendering each time todo: possible use in App.vue--> <Navbar /><!-- Causes rerendering each time todo: possible use in App.vue-->
<div class="about"> <div class="about">
<h1>This is an Contracts page</h1> <h1>This is an Contracts page</h1>
</div> </div>
</template> </template>
<style> <style></style>
</style>

View File

@@ -1,17 +1,15 @@
<script setup> <script setup>
import Navbar from "../components/Navbar.vue"; import Navbar from '../components/Navbar.vue'
</script> </script>
<script> <script>
export default { export default {
mounted() { mounted() {}
}
} }
</script> </script>
<template> <template>
<Navbar/><!-- Causes rerendering each time todo: possible use in App.vue--> <Navbar /><!-- Causes rerendering each time todo: possible use in App.vue-->
<div class="about"> <div class="about">
<h1>This is a Home page</h1> <h1>This is a Home page</h1>
</div> </div>
</template> </template>
<style> <style></style>
</style>

View File

@@ -1,17 +1,15 @@
<script setup> <script setup>
import Navbar from "../components/Navbar.vue"; import Navbar from '../components/Navbar.vue'
</script> </script>
<script> <script>
export default { export default {
mounted() { mounted() {}
}
} }
</script> </script>
<template> <template>
<Navbar/><!-- Causes rerendering each time todo: possible use in App.vue--> <Navbar /><!-- Causes rerendering each time todo: possible use in App.vue-->
<div class="about"> <div class="about">
<h1>This is an Support page</h1> <h1>This is an Support page</h1>
</div> </div>
</template> </template>
<style> <style></style>
</style>