added logout function and persistent pinia store
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
import { store } from 'quasar/wrappers'
|
||||
import { createPinia } from 'pinia'
|
||||
import { Router } from 'vue-router';
|
||||
|
||||
/*
|
||||
* When adding new properties to stores, you should also
|
||||
* extend the `PiniaCustomProperties` interface.
|
||||
* @see https://pinia.vuejs.org/core-concepts/plugins.html#typing-new-store-properties
|
||||
*/
|
||||
declare module 'pinia' {
|
||||
export interface PiniaCustomProperties {
|
||||
readonly router: Router;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If not building with SSR mode, you can
|
||||
* directly export the Store instantiation;
|
||||
*
|
||||
* The function below can be async too; either use
|
||||
* async/await or return a Promise which resolves
|
||||
* with the Store instance.
|
||||
*/
|
||||
|
||||
export default store((/* { ssrContext } */) => {
|
||||
const pinia = createPinia()
|
||||
|
||||
// You can add Pinia plugins here
|
||||
// pinia.use(SomePiniaPlugin)
|
||||
|
||||
return pinia
|
||||
})
|
||||
@@ -1,20 +0,0 @@
|
||||
import type Keycloak from 'keycloak-js';
|
||||
import { defineStore } from 'pinia';
|
||||
//import api from 'axios';
|
||||
|
||||
export const useKeycloakStore = defineStore('keycloakStore', {
|
||||
state: ()=>({
|
||||
keycloak: null as Keycloak| null,
|
||||
}),
|
||||
getters: {
|
||||
getKeycloak: (state) => state.keycloak,
|
||||
},
|
||||
actions:{
|
||||
setKeycloak(keycloak: Keycloak): void {
|
||||
this.keycloak = keycloak;
|
||||
},
|
||||
async logout(){
|
||||
await this.keycloak?.logout();
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -1,26 +1,66 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import {api} from 'boot/axios';
|
||||
import {ptpUser, responseModel} from 'src/models';
|
||||
import Keycloak from 'keycloak-js';
|
||||
import { api } from 'src/boot/axios';
|
||||
import { ptpUser, responseModel } from 'src/models';
|
||||
import Keycloak, { KeycloakInitOptions, KeycloakLoginOptions } from 'keycloak-js';
|
||||
|
||||
export const useUserStore = defineStore('userStore', {
|
||||
state: () => ({
|
||||
user: {"keycloakID":null,"firstName":null,"lastName":null,"username":null},
|
||||
user: null as ptpUser | null,
|
||||
userLoaded: false,
|
||||
keycloak: null as Keycloak | null,
|
||||
keycloakId: "",
|
||||
}),
|
||||
getters: {
|
||||
|
||||
},
|
||||
getters: {},
|
||||
actions: {
|
||||
getPtpUserByKeycloakID(keycloakID : string) {
|
||||
this.loadPtpUsersByKeycloakID(keycloakID).then((response : responseModel) => {
|
||||
async getPtpUserByKeycloakID(keycloakID : string) {
|
||||
await this.loadPtpUsersByKeycloakID(keycloakID).then((response : responseModel) => {
|
||||
this.user = response.response;
|
||||
this.userLoaded = true;
|
||||
console.log("ich habe das gesetzt: " + JSON.stringify(this.user) + " / " + this.userLoaded);
|
||||
});
|
||||
},
|
||||
async loadPtpUsersByKeycloakID(keycloakID : string) : Promise<responseModel> {
|
||||
return (await api.get("/users/login/"+keycloakID)).data;
|
||||
},
|
||||
async initKeycloak() {
|
||||
/**
|
||||
* Variablen sind in quasar.config.js definiert und werden hier verwendet.
|
||||
* @param keycloakPort Port, auf dem Keycloak läuft, hinterlegt in docker-compose.yml
|
||||
* @param frontendPort Port, auf dem das Frontend läuft, hinterlegt in quasar.config.js und realm-export.json von Keycloak für die Redirect-URL
|
||||
* @param realm Name des Realms, hinterlegt in realm-export.json von Keycloak
|
||||
* @param clientId Name des Clients, hinterlegt in realm-export.json von Keycloak
|
||||
*/
|
||||
const keycloakPort: string = "" + process.env.PORT_KEYCLOAK;
|
||||
const frontendPort: string = "" + process.env.PORT_FRONTEND;
|
||||
const realm : string= "" + process.env.KEYCLOAK_REALM;
|
||||
const clientId: string = "" + process.env.KEYCLOAK_CLIENT_ID;
|
||||
const keycloak: Keycloak = new Keycloak({
|
||||
url: 'http://localhost:' + keycloakPort + '/',
|
||||
realm: realm,
|
||||
clientId: clientId
|
||||
});
|
||||
const initOptions: KeycloakInitOptions = {
|
||||
onLoad: 'login-required', //erzwingt Login
|
||||
redirectUri: 'http://localhost:' + frontendPort + '/',
|
||||
checkLoginIframe: false,
|
||||
locale: 'login-required',
|
||||
}
|
||||
try {
|
||||
await keycloak.init(initOptions).then( (auth: boolean): void => {
|
||||
console.log("Authenticated: " + auth);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
this.keycloak = keycloak;
|
||||
return keycloak;
|
||||
},
|
||||
async login() {
|
||||
if(!this.keycloak){
|
||||
await this.initKeycloak();
|
||||
}else await this.keycloak?.login( {prompt: 'login'} as KeycloakLoginOptions);
|
||||
},
|
||||
async logout() {
|
||||
window.location.href= 'http://localhost:' + process.env.PORT_KEYCLOAK + '/realms/' + process.env.KEYCLOAK_REALM + '/protocol/openid-connect/logout?post_logout_redirect_uri=http%3A%2F%2Flocalhost:' + process.env.PORT_FRONTEND + '&client_id=' + process.env.KEYCLOAK_CLIENT_ID;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user