Skip to content
Snippets Groups Projects
Commit f7bf48d3 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Replace the remember-login attribute with a new try-login one

With the new ability to log in without redirecting to keycloak and reloading the page
we can now try to login on start every time.

Instead of remembering the login state in the session storage we just ask keycloak in an iframe
on start. To better describe this new behaviour rename the attribute from remember-login to try-login.
parent 86d93a15
No related branches found
No related tags found
No related merge requests found
...@@ -19,8 +19,9 @@ ...@@ -19,8 +19,9 @@
- example `<vpu-auth client-id="my-client-id" load-person></vpu-auth>` - example `<vpu-auth client-id="my-client-id" load-person></vpu-auth>`
- `force-login` (optional, default: off): if enabled a login will be forced, there never will be a login button - `force-login` (optional, default: off): if enabled a login will be forced, there never will be a login button
- example `<vpu-auth client-id="my-client-id" force-login></vpu-auth>` - example `<vpu-auth client-id="my-client-id" force-login></vpu-auth>`
- `remember-login` (optional, default: off): if enabled a login will be forced if the user was logged in, in the same session - `try-login` (optional, default: off): if enabled the a login will happen if the user is already logged in
- example `<vpu-auth client-id="my-client-id" remember-login></vpu-auth>` and finishing the login process would not result in a page location change (reload/redirect).
- example `<vpu-auth client-id="my-client-id" try-login></vpu-auth>`
## Events to listen to ## Events to listen to
......
...@@ -87,13 +87,19 @@ export class KeycloakWrapper extends EventTarget { ...@@ -87,13 +87,19 @@ export class KeycloakWrapper extends EventTarget {
} }
/** /**
* Returns true in case we just got redirected from the login page * If this returns true you need to call login() at one point to finish the login process.
*/ */
isLoggingIn() { isLoggingIn() {
const href = window.location.href; const href = window.location.href;
return (href.search('[&#]state=') >= 0 && href.search('[&#]session_state=') >= 0); return (href.search('[&#]state=') >= 0 && href.search('[&#]session_state=') >= 0);
} }
/**
* Logs the user in. Might lead to a site refresh or the user needing to authenticate.
*
* @param {object} options
* @param {string} [options.lang] - The locale to use on the keycloak login page
*/
async login(options) { async login(options) {
await this._ensureInit(); await this._ensureInit();
...@@ -107,10 +113,24 @@ export class KeycloakWrapper extends EventTarget { ...@@ -107,10 +113,24 @@ export class KeycloakWrapper extends EventTarget {
} }
} }
async clearToken() { /**
* Logs the user in if it is possible without leaving the page or the user needing to authenticate again.
*/
async tryLogin() {
await this._ensureInit();
}
/**
* Logs the user out locally, but not with keycloak. Login will instantly log the user back in without
* requiring a re-auth.
*/
async localLogout() {
this._keycloak.clearToken(); this._keycloak.clearToken();
} }
/**
* Log the user out from keycloak.
*/
async logout() { async logout() {
await this._ensureInit(); await this._ensureInit();
this._keycloak.logout(); this._keycloak.logout();
......
...@@ -50,7 +50,7 @@ class AuthDemo extends LitElement { ...@@ -50,7 +50,7 @@ class AuthDemo extends LitElement {
<h1 class="title">Auth-Demo</h1> <h1 class="title">Auth-Demo</h1>
</div> </div>
<div class="container"> <div class="container">
<vpu-auth lang="${this.lang}" client-id="${commonUtils.setting('keyCloakClientId')}" silent-check-sso-uri="${silentCheckSsoUri}" load-person remember-login></vpu-auth> <vpu-auth lang="${this.lang}" client-id="${commonUtils.setting('keyCloakClientId')}" silent-check-sso-uri="${silentCheckSsoUri}" load-person try-login></vpu-auth>
</div> </div>
</section> </section>
`; `;
......
...@@ -41,7 +41,7 @@ class VPUAuth extends VPULitElement { ...@@ -41,7 +41,7 @@ class VPUAuth extends VPULitElement {
this.subject = ""; this.subject = "";
this.name = ""; this.name = "";
this.personId = ""; this.personId = "";
this.rememberLogin = false; this.tryLogin = false;
this.person = null; this.person = null;
const _getLoginData = () => { const _getLoginData = () => {
...@@ -143,7 +143,7 @@ class VPUAuth extends VPULitElement { ...@@ -143,7 +143,7 @@ class VPUAuth extends VPULitElement {
return { return {
lang: { type: String }, lang: { type: String },
forceLogin: { type: Boolean, attribute: 'force-login' }, forceLogin: { type: Boolean, attribute: 'force-login' },
rememberLogin: { type: Boolean, attribute: 'remember-login' }, tryLogin: { type: Boolean, attribute: 'try-login' },
loadPerson: { type: Boolean, attribute: 'load-person' }, loadPerson: { type: Boolean, attribute: 'load-person' },
clientId: { type: String, attribute: 'client-id' }, clientId: { type: String, attribute: 'client-id' },
silentCheckSsoUri: { type: String, attribute: 'silent-check-sso-uri' }, silentCheckSsoUri: { type: String, attribute: 'silent-check-sso-uri' },
...@@ -164,19 +164,21 @@ class VPUAuth extends VPULitElement { ...@@ -164,19 +164,21 @@ class VPUAuth extends VPULitElement {
this._kcwrapper = new KeycloakWrapper(baseURL, realm, this.clientId, this.silentCheckSsoUri); this._kcwrapper = new KeycloakWrapper(baseURL, realm, this.clientId, this.silentCheckSsoUri);
this._kcwrapper.addEventListener('changed', this._onKCChanged); this._kcwrapper.addEventListener('changed', this._onKCChanged);
const handleLogin = async () => {
if (this.forceLogin || this._kcwrapper.isLoggingIn()) {
this._setLoginStatus(LoginStatus.LOGGING_IN);
await this._kcwrapper.login();
} else if (this.tryLogin) {
this._setLoginStatus(LoginStatus.LOGGING_IN);
await this._kcwrapper.tryLogin();
if (this._loginStatus === LoginStatus.LOGGING_IN)
this._setLoginStatus(LoginStatus.LOGGED_OUT);
} else {
this._setLoginStatus(LoginStatus.LOGGED_OUT);
}
};
let doLogin = false; handleLogin();
if ((this.rememberLogin && sessionStorage.getItem('vpu-logged-in')) || this.forceLogin) {
doLogin = true;
}
// load Keycloak if we want to force the login or if we were redirected from the Keycloak login page
if (doLogin || this._kcwrapper.isLoggingIn()) {
this._setLoginStatus(LoginStatus.LOGGING_IN);
this._kcwrapper.login()
} else {
this._setLoginStatus(LoginStatus.LOGGED_OUT);
}
this.updateComplete.then(() => { this.updateComplete.then(() => {
window.onresize = () => { window.onresize = () => {
...@@ -221,12 +223,6 @@ class VPUAuth extends VPULitElement { ...@@ -221,12 +223,6 @@ class VPUAuth extends VPULitElement {
if (propName === "lang") { if (propName === "lang") {
i18n.changeLanguage(this.lang); i18n.changeLanguage(this.lang);
} }
if (propName == "_loginStatus") {
if (this._loginStatus === LoginStatus.LOGGED_IN)
sessionStorage.setItem('vpu-logged-in', true);
else
sessionStorage.removeItem('vpu-logged-in');
}
}); });
super.update(changedProperties); super.update(changedProperties);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment