@samhammer/authentication-vue
Advanced tools
+25
-18
@@ -76,3 +76,3 @@ import Keycloak from "keycloak-js"; | ||
| return false; | ||
| if (this.pluginState) { | ||
| if (this.pluginState && this.pluginState.keycloak) { | ||
| const resource = !apiClientId ? authOptions.apiClientId : apiClientId; | ||
@@ -101,10 +101,4 @@ return this.pluginState.keycloak.hasResourceRole(roleName, resource); | ||
| } | ||
| const keycloak = new Keycloak({ | ||
| url: authOptions.authUrl, | ||
| realm: authOptions.realm, | ||
| clientId: authOptions.appClientId | ||
| }); | ||
| this.pluginState = { | ||
| keycloak: keycloak, | ||
| authOptios: authOptions, | ||
| authOptions: authOptions, | ||
| accessTokenKey: `${authOptions.appClientId}-accessToken`, | ||
@@ -114,5 +108,2 @@ refreshTokenKey: `${authOptions.appClientId}-refreshToken`, | ||
| }; | ||
| keycloak.onAuthSuccess = this.setStorage.bind(this); | ||
| keycloak.onAuthRefreshSuccess = this.setStorage.bind(this); | ||
| keycloak.onTokenExpired = this.onTokenExpired.bind(this); | ||
| return this.initKeycloak(); | ||
@@ -129,2 +120,7 @@ } | ||
| throw new Error("Init has to be called first"); | ||
| delete this.pluginState.keycloak; | ||
| this.pluginState.keycloak = this.getKeycloakInstance(); | ||
| this.pluginState.keycloak.onAuthSuccess = this.setStorage.bind(this); | ||
| this.pluginState.keycloak.onAuthRefreshSuccess = this.setStorage.bind(this); | ||
| this.pluginState.keycloak.onTokenExpired = this.onTokenExpired.bind(this); | ||
| const authenticated = await this.pluginState.keycloak.init({ | ||
@@ -137,3 +133,3 @@ flow: "standard", | ||
| idToken: await StoreProvider.store.getItem(this.pluginState.idTokenKey), | ||
| ...this.pluginState.authOptios.keycloakInitOptions | ||
| ...this.pluginState.authOptions.keycloakInitOptions | ||
| }); | ||
@@ -151,3 +147,3 @@ if (authenticated) { | ||
| console.debug("login navigating to keycloak with returnUrl", redirectUri); | ||
| if (!this.pluginState) | ||
| if (!this.pluginState || !this.pluginState?.keycloak) | ||
| throw new Error("Init has to be called first"); | ||
@@ -158,3 +154,3 @@ await this.pluginState.keycloak.login({ redirectUri, idpHint: idp }); | ||
| console.debug("create loginUrl to keycloak with returnUrl", redirectUri); | ||
| if (!this.pluginState) | ||
| if (!this.pluginState || !this.pluginState.keycloak) | ||
| throw new Error("Init has to be called first"); | ||
@@ -165,3 +161,3 @@ return this.pluginState.keycloak.createLoginUrl({ redirectUri, idpHint: idp }); | ||
| console.debug("create logoutUrl to keycloak with returnUrl", redirectUri); | ||
| if (!this.pluginState) | ||
| if (!this.pluginState || !this.pluginState.keycloak) | ||
| throw new Error("Init has to be called first"); | ||
@@ -173,3 +169,3 @@ return this.pluginState.keycloak.createLogoutUrl({ redirectUri }); | ||
| await this.clearStorage(); | ||
| if (!this.pluginState) | ||
| if (!this.pluginState || !this.pluginState.keycloak) | ||
| return; | ||
@@ -179,3 +175,3 @@ await this.pluginState.keycloak.logout({ redirectUri }); | ||
| async setStorage() { | ||
| if (!this.pluginState) | ||
| if (!this.pluginState || !this.pluginState.keycloak) | ||
| return; | ||
@@ -195,3 +191,3 @@ await StoreProvider.store.setItem(this.pluginState.refreshTokenKey, this.pluginState.keycloak.refreshToken ?? ""); | ||
| try { | ||
| if (!this.pluginState) | ||
| if (!this.pluginState || !this.pluginState.keycloak) | ||
| return; | ||
@@ -210,2 +206,13 @@ const successful = await this.pluginState.keycloak.updateToken(minValidity); | ||
| refreshSingle = throttle(() => this.refresh(10), 5000, { trailing: false }); | ||
| getKeycloakInstance() { | ||
| const state = this.pluginState; | ||
| if (!state || !state.authOptions.appClientId || !state.authOptions.authUrl || !state.authOptions.realm) { | ||
| throw new Error("AuthOptions need to be set correctly"); | ||
| } | ||
| return new Keycloak({ | ||
| url: state.authOptions.authUrl, | ||
| realm: state.authOptions.realm, | ||
| clientId: state.authOptions.appClientId | ||
| }); | ||
| } | ||
| } | ||
@@ -212,0 +219,0 @@ class GuestPlugin { |
+1
-1
| { | ||
| "name": "@samhammer/authentication-vue", | ||
| "version": "2.3.0", | ||
| "version": "2.3.1", | ||
| "description": "Keycloak authentication for VueJS projects", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
17357
3.43%417
1.71%