* navbar with protected routes

feature: #3
This commit is contained in:
2023-01-03 02:26:15 +01:00
parent 23c40c5468
commit 46dd4ef361
18 changed files with 304 additions and 443 deletions

View File

@@ -1,7 +0,0 @@
<html>
<body>
<script>
parent.postMessage(location.href, location.origin)
</script>
</body>
</html>

View File

@@ -1,85 +1,7 @@
<script setup>
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'
import { RouterView } from 'vue-router'
</script>
<template>
<header>
<img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" />
<div class="wrapper">
<HelloWorld msg="You did it!" />
<nav>
<RouterLink to="/">Home</RouterLink>
<RouterLink to="/about">About</RouterLink>
</nav>
</div>
</header>
<RouterView />
</template>
<style scoped>
header {
line-height: 1.5;
max-height: 100vh;
}
.logo {
display: block;
margin: 0 auto 2rem;
}
nav {
width: 100%;
font-size: 12px;
text-align: center;
margin-top: 2rem;
}
nav a.router-link-exact-active {
color: var(--color-text);
}
nav a.router-link-exact-active:hover {
background-color: transparent;
}
nav a {
display: inline-block;
padding: 0 1rem;
border-left: 1px solid var(--color-border);
}
nav a:first-of-type {
border: 0;
}
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
.logo {
margin: 0 2rem 0 0;
}
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
}
nav {
text-align: left;
margin-left: -1rem;
font-size: 1rem;
padding: 1rem 0;
margin-top: 1rem;
}
}
</style>

View File

@@ -1,74 +0,0 @@
/* color palette from <https://github.com/vuejs/theme> */
:root {
--vt-c-white: #ffffff;
--vt-c-white-soft: #f8f8f8;
--vt-c-white-mute: #f2f2f2;
--vt-c-black: #181818;
--vt-c-black-soft: #222222;
--vt-c-black-mute: #282828;
--vt-c-indigo: #2c3e50;
--vt-c-divider-light-1: rgba(60, 60, 60, 0.29);
--vt-c-divider-light-2: rgba(60, 60, 60, 0.12);
--vt-c-divider-dark-1: rgba(84, 84, 84, 0.65);
--vt-c-divider-dark-2: rgba(84, 84, 84, 0.48);
--vt-c-text-light-1: var(--vt-c-indigo);
--vt-c-text-light-2: rgba(60, 60, 60, 0.66);
--vt-c-text-dark-1: var(--vt-c-white);
--vt-c-text-dark-2: rgba(235, 235, 235, 0.64);
}
/* semantic color variables for this project */
:root {
--color-background: var(--vt-c-white);
--color-background-soft: var(--vt-c-white-soft);
--color-background-mute: var(--vt-c-white-mute);
--color-border: var(--vt-c-divider-light-2);
--color-border-hover: var(--vt-c-divider-light-1);
--color-heading: var(--vt-c-text-light-1);
--color-text: var(--vt-c-text-light-1);
--section-gap: 160px;
}
@media (prefers-color-scheme: dark) {
:root {
--color-background: var(--vt-c-black);
--color-background-soft: var(--vt-c-black-soft);
--color-background-mute: var(--vt-c-black-mute);
--color-border: var(--vt-c-divider-dark-2);
--color-border-hover: var(--vt-c-divider-dark-1);
--color-heading: var(--vt-c-text-dark-1);
--color-text: var(--vt-c-text-dark-2);
}
}
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
position: relative;
font-weight: normal;
}
body {
min-height: 100vh;
color: var(--color-text);
background: var(--color-background);
transition: color 0.5s, background-color 0.5s;
line-height: 1.6;
font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
font-size: 15px;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

View File

@@ -2,37 +2,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
font-weight: normal;
}
a,
.green {
text-decoration: none;
color: hsla(160, 100%, 37%, 1);
transition: 0.4s;
}
@media (hover: hover) {
a:hover {
background-color: hsla(160, 100%, 37%, 0.2);
}
}
@media (min-width: 1024px) {
body {
display: flex;
place-items: center;
}
#app {
display: grid;
grid-template-columns: 1fr 1fr;
padding: 0 2rem;
}
}

View File

@@ -1,8 +1,9 @@
import Keycloak from 'keycloak-js'
import store from '../store'
let keycloak = null
function setup() {
export function keycloakSetup() {
keycloak = new Keycloak({
url: 'https://auth.denysoft.eu/',
realm: 'FST',
@@ -20,6 +21,7 @@ function setup() {
.then(refreshed => {
if (refreshed) {
console.info('Token refreshed ' + refreshed)
store.commit('initKeycloak', keycloak)
}
})
.catch(() => {
@@ -31,12 +33,22 @@ function setup() {
alert('failed to initialize')
})
}
function login() {
export function login() {
keycloak.login()
}
function getKeycloak() {
export function getKeycloak() {
return keycloak
}
export default { setup, login, getKeycloak }
export function isVerwaltung() {
return keycloak.hasRealmRole('verwaltung')
}
export function isEmployee() {
return keycloak.hasRealmRole('employee')
}
export function isAdmin() {
return keycloak.hasRealmRole('admin')
}

View File

@@ -1,43 +0,0 @@
<script setup>
defineProps({
msg: {
type: String,
required: true
}
})
</script>
<template>
<div class="greetings">
<h1 class="green">{{ msg }}</h1>
<h3>
Youve successfully created a project with
<a href="https://vitejs.dev/" target="_blank" rel="noopener">Vite</a> +
<a href="https://vuejs.org/" target="_blank" rel="noopener">Vue 3</a>.
</h3>
</div>
</template>
<style scoped>
h1 {
font-weight: 500;
font-size: 2.6rem;
top: -10px;
}
h3 {
font-size: 1.2rem;
}
.greetings h1,
.greetings h3 {
text-align: center;
}
@media (min-width: 1024px) {
.greetings h1,
.greetings h3 {
text-align: left;
}
}
</style>

114
src/components/Navbar.vue Normal file
View File

@@ -0,0 +1,114 @@
<template>
<div class="flex justify-between bg-gray-900 text-white pt-2 pb-3 pl-8">
<div class="basis-1/3 logo">
<p class="font-mono text-lg py-2">
<RouterLink to="/">Filmstudiostube</RouterLink>
</p>
</div>
<div class="flex justify-around basis-1/3 max-md:hidden">
<div v-if="!isAnonym">
<p class="py-2">Aufträge</p>
</div>
<div v-if="!isAnonym" class="nav-item">
<p class="py-2">Support</p>
</div>
<div v-if="isVerwaltung" class="nav-item">
<p class="py-2">Administration</p>
</div>
<div v-if="isAnonym" class="nav-item">
<p class="py-2">Arbeiten</p>
</div>
<div v-if="isAnonym" class="nav-item">
<p class="py-2">Kontakt</p>
</div>
<div v-if="isAnonym" class="nav-item">
<RouterLink to="/about">
<p class="py-2">Über uns</p>
</RouterLink>
</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">
<button class="bg-blue-600 hover:bg-blue-500 duration-300 text-white px-3 py-1 my-2 rounded" @click="login">
Login
</button>
</div>
<div class="pt-2 md:hidden" @click="openNavbar">
<div class="space-y-1.5 pt-2 pb-2 mr-2 pr-2 pl-2 bg-slate-100 rounded">
<span class="block w-6 h-0.5 bg-gray-500"></span>
<span class="block w-6 h-0.5 bg-gray-500"></span>
<span class="block w-6 h-0.5 bg-gray-500"></span>
</div>
</div>
</div>
<div v-if="!isNavbarClosed">
<ul class="bg-gray-900">
<li v-if="!isAnonym" class="block text-md py-4 text-white text-center">
<RouterLink to="/contracts">Aufträge</RouterLink>
</li>
<li v-if="!isAnonym" class="block text-md py-4 text-white text-center">
<RouterLink to="/support">Support</RouterLink>
</li>
<li v-if="isVerwaltung" class="block text-md py-4 text-white text-center">
<RouterLink to="/administration">Administration</RouterLink>
</li>
<li v-if="isAnonym" class="block text-md py-4 text-white text-center">
<RouterLink to="/arbeiten">Arbeiten</RouterLink>
</li>
<li v-if="isAnonym" class="block text-md py-4 text-white text-center">
<RouterLink to="/contact">Kontakt</RouterLink>
</li>
<li v-if="isAnonym" class="block text-md py-4 text-white text-center">
<RouterLink to="/about">Über uns</RouterLink>
</li>
<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">
Login
</button>
</li>
</ul>
</div>
</template>
<script>
import {keycloakSetup} from "../authentication/AuthHelper";
export default {
data() {
return {
keycloak: this.$store.state.keycloak.keycloak,
isAnonym: false,
isNavbarClosed: true
}
},
computed: {
isVerwaltung() {
if (this.$store.state.keycloak.keycloak === null){
this.isAnonym = true;
return false
}
return this.$store.state.keycloak.keycloak.realmAccess.roles.includes('verwaltung')
},
isAdmin() {
if (this.$store.state.keycloak.keycloak === null){
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: {
login() {
keycloakSetup()
},
openNavbar(){
this.isNavbarClosed = !this.isNavbarClosed
}
}
}
</script>

View File

@@ -1,86 +0,0 @@
<script setup>
import WelcomeItem from './WelcomeItem.vue'
import DocumentationIcon from './icons/IconDocumentation.vue'
import ToolingIcon from './icons/IconTooling.vue'
import EcosystemIcon from './icons/IconEcosystem.vue'
import CommunityIcon from './icons/IconCommunity.vue'
import SupportIcon from './icons/IconSupport.vue'
</script>
<template>
<WelcomeItem>
<template #icon>
<DocumentationIcon />
</template>
<template #heading>Documentation</template>
Vues
<a href="https://vuejs.org/" target="_blank" rel="noopener">official documentation</a>
provides you with all information you need to get started.
</WelcomeItem>
<WelcomeItem>
<template #icon>
<ToolingIcon />
</template>
<template #heading>Tooling</template>
This project is served and bundled with
<a href="https://vitejs.dev/guide/features.html" target="_blank" rel="noopener">Vite</a>. The
recommended IDE setup is
<a href="https://code.visualstudio.com/" target="_blank" rel="noopener">VSCode</a> +
<a href="https://github.com/johnsoncodehk/volar" target="_blank" rel="noopener">Volar</a>. If
you need to test your components and web pages, check out
<a href="https://www.cypress.io/" target="_blank" rel="noopener">Cypress</a> and
<a href="https://on.cypress.io/component" target="_blank">Cypress Component Testing</a>.
<br />
More instructions are available in <code>README.md</code>.
</WelcomeItem>
<WelcomeItem>
<template #icon>
<EcosystemIcon />
</template>
<template #heading>Ecosystem</template>
Get official tools and libraries for your project:
<a href="https://pinia.vuejs.org/" target="_blank" rel="noopener">Pinia</a>,
<a href="https://router.vuejs.org/" target="_blank" rel="noopener">Vue Router</a>,
<a href="https://test-utils.vuejs.org/" target="_blank" rel="noopener">Vue Test Utils</a>, and
<a href="https://github.com/vuejs/devtools" target="_blank" rel="noopener">Vue Dev Tools</a>. If
you need more resources, we suggest paying
<a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">Awesome Vue</a>
a visit.
</WelcomeItem>
<WelcomeItem>
<template #icon>
<CommunityIcon />
</template>
<template #heading>Community</template>
Got stuck? Ask your question on
<a href="https://chat.vuejs.org" target="_blank" rel="noopener">Vue Land</a>, our official
Discord server, or
<a href="https://stackoverflow.com/questions/tagged/vue.js" target="_blank" rel="noopener"
>StackOverflow</a
>. You should also subscribe to
<a href="https://news.vuejs.org" target="_blank" rel="noopener">our mailing list</a> and follow
the official
<a href="https://twitter.com/vuejs" target="_blank" rel="noopener">@vuejs</a>
twitter account for latest news in the Vue world.
</WelcomeItem>
<WelcomeItem>
<template #icon>
<SupportIcon />
</template>
<template #heading>Support Vue</template>
As an independent project, Vue relies on community backing for its sustainability. You can help
us by
<a href="https://vuejs.org/sponsor/" target="_blank" rel="noopener">becoming a sponsor</a>.
</WelcomeItem>
</template>

View File

@@ -1,85 +0,0 @@
<template>
<div class="item">
<i>
<slot name="icon"></slot>
</i>
<div class="details">
<h3>
<slot name="heading"></slot>
</h3>
<slot></slot>
</div>
</div>
</template>
<style scoped>
.item {
margin-top: 2rem;
display: flex;
}
.details {
flex: 1;
margin-left: 1rem;
}
i {
display: flex;
place-items: center;
place-content: center;
width: 32px;
height: 32px;
color: var(--color-text);
}
h3 {
font-size: 1.2rem;
font-weight: 500;
margin-bottom: 0.4rem;
color: var(--color-heading);
}
@media (min-width: 1024px) {
.item {
margin-top: 0;
padding: 0.4rem 0 1rem calc(var(--section-gap) / 2);
}
i {
top: calc(50% - 25px);
left: -26px;
position: absolute;
border: 1px solid var(--color-border);
background: var(--color-background);
border-radius: 8px;
width: 50px;
height: 50px;
}
.item:before {
content: ' ';
border-left: 1px solid var(--color-border);
position: absolute;
left: 0;
bottom: calc(50% + 25px);
height: calc(50% - 25px);
}
.item:after {
content: ' ';
border-left: 1px solid var(--color-border);
position: absolute;
left: 0;
top: calc(50% + 25px);
height: calc(50% - 25px);
}
.item:first-of-type:before {
display: none;
}
.item:last-of-type:after {
display: none;
}
}
</style>

View File

@@ -8,7 +8,6 @@ import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faUserSecret } from '@fortawesome/free-solid-svg-icons'
import './assets/main.css'
import AuthHelper from './authentication/AuthHelper'
/* add icons to the library */
library.add(faUserSecret)

View File

@@ -1,6 +1,7 @@
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AuthHelper from '../authentication/AuthHelper'
import store from '../store/index'
import { getKeycloak, keycloakSetup } from '../authentication/AuthHelper'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
@@ -13,13 +14,59 @@ const router = createRouter({
requiresAuth: false
}
},
{
path: '/contracts',
name: 'contracts',
// this page is lazy-loaded when the route is visited.
component: () => import('../views/ContractsView.vue'),
meta: {
requiresAuth: true
}
},
{
path: '/support',
name: 'support',
// this page is lazy-loaded when the route is visited.
component: () => import('../views/SupportView.vue'),
meta: {
requiresAuth: true
}
},
{
path: '/support',
name: 'support',
// this page is lazy-loaded when the route is visited.
component: () => import('../views/AdministrationView.vue'),
meta: {
requiresAuth: true,
requiresAdmin: true
}
},
{
path: '/arbeiten',
name: 'arbeiten',
// this page is lazy-loaded when the route is visited.
component: () => import('../views/ArbeitenView.vue'),
meta: {
requiresAuth: false
}
},
{
path: '/contact',
name: 'contact',
// this page is lazy-loaded when the route is visited.
component: () => import('../views/ContactView.vue'),
meta: {
requiresAuth: false
}
},
{
path: '/about',
name: 'about',
// this page is lazy-loaded when the route is visited.
component: () => import('../views/AboutView.vue'),
meta: {
requiresAuth: true
requiresAuth: false
}
},
{
@@ -43,10 +90,11 @@ const router = createRouter({
router.beforeEach(async (to, from, next) => {
if (to.meta.requiresAuth === true) {
if (AuthHelper.getKeycloak() === null) {
await AuthHelper.setup()
if (getKeycloak() === null) {
await keycloakSetup()
store.commit('initKeycloak', getKeycloak())
}
if (!AuthHelper.getKeycloak().authenticated) {
if (!getKeycloak().authenticated) {
next('/forbidden')
}
next()

View File

@@ -1,15 +1,17 @@
<script setup>
import Navbar from "../components/Navbar.vue";
</script>
<script>
export default {
mounted() {
}
}
</script>
<template>
<Navbar/><!-- Causes rerendering each time todo: possible use in App.vue-->
<div class="about">
<h1>This is an about page</h1>
<font-awesome-icon icon="fa-solid fa-user-secret" />
<h1>This is a About page</h1>
</div>
</template>
<style>
@media (min-width: 1024px) {
.about {
min-height: 100vh;
display: flex;
align-items: center;
}
}
</style>

View File

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

View File

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

17
src/views/ContactView.vue Normal file
View File

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

View File

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

View File

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

17
src/views/SupportView.vue Normal file
View File

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