@vaadin/hilla-frontend
Advanced tools
Comparing version 24.4.0-beta5 to 24.4.0-rc1
@@ -10,7 +10,33 @@ import type { MiddlewareClass, MiddlewareContext, MiddlewareNext } from './Connect.js'; | ||
} | ||
export type SuccessCallback = () => Promise<void> | void; | ||
export type NavigateFunction = (path: string) => void; | ||
export interface LoginOptions { | ||
loginProcessingUrl?: string; | ||
/** | ||
* The URL for login request, defaults to `/login`. | ||
*/ | ||
loginProcessingUrl?: URL | string; | ||
/** | ||
* The success callback. | ||
*/ | ||
onSuccess?: SuccessCallback; | ||
/** | ||
* The navigation callback, called after successful login. The default | ||
* reloads the page. | ||
*/ | ||
navigate?: NavigateFunction; | ||
} | ||
export interface LogoutOptions { | ||
logoutUrl?: string; | ||
/** | ||
* The URL for logout request, defaults to `/logout`. | ||
*/ | ||
logoutUrl?: URL | string; | ||
/** | ||
* The success callback. | ||
*/ | ||
onSuccess?: SuccessCallback; | ||
/** | ||
* The navigation callback, called after successful logout. The default | ||
* reloads the page. | ||
*/ | ||
navigate?: NavigateFunction; | ||
} | ||
@@ -17,0 +43,0 @@ /** |
@@ -41,3 +41,18 @@ import CookieManager from "./CookieManager.js"; | ||
await updateCsrfTokensBasedOnResponse(response); | ||
return response; | ||
} | ||
function normalizePath(url) { | ||
const effectiveBaseURL = new URL(".", document.baseURI); | ||
const effectiveBaseURI = effectiveBaseURL.toString(); | ||
let normalized = url; | ||
if (normalized.startsWith(effectiveBaseURL.pathname)) { | ||
return `/${normalized.slice(effectiveBaseURL.pathname.length)}`; | ||
} | ||
normalized = normalized.startsWith(effectiveBaseURI) ? `/${normalized.slice(effectiveBaseURI.length)}` : normalized; | ||
return normalized; | ||
} | ||
function navigateWithPageReload(to) { | ||
const url = to.startsWith("/") ? new URL(`.${to}`, document.baseURI) : to; | ||
window.location.replace(url); | ||
} | ||
async function login(username, password, options) { | ||
@@ -70,2 +85,9 @@ try { | ||
} | ||
if (options?.onSuccess) { | ||
await options.onSuccess(); | ||
} | ||
const url = savedUrl ?? defaultUrl ?? document.baseURI; | ||
const toPath = normalizePath(url); | ||
const navigate = options?.navigate ?? navigateWithPageReload; | ||
navigate(toPath); | ||
return { | ||
@@ -96,12 +118,13 @@ defaultUrl, | ||
const logoutUrl = options?.logoutUrl ?? "logout"; | ||
let response; | ||
try { | ||
const headers = getSpringCsrfTokenHeadersForAuthRequest(document); | ||
await doLogout(logoutUrl, headers); | ||
response = await doLogout(logoutUrl, headers); | ||
} catch { | ||
try { | ||
const response = await fetch("?nocache"); | ||
const responseText = await response.text(); | ||
const noCacheResponse = await fetch("?nocache"); | ||
const responseText = await noCacheResponse.text(); | ||
const doc = new DOMParser().parseFromString(responseText, "text/html"); | ||
const headers = getSpringCsrfTokenHeadersForAuthRequest(doc); | ||
await doLogout(logoutUrl, headers); | ||
response = await doLogout(logoutUrl, headers); | ||
} catch (error) { | ||
@@ -113,2 +136,10 @@ clearSpringCsrfMetaTags(); | ||
CookieManager.remove(JWT_COOKIE_NAME); | ||
if (response && response.ok && response.redirected) { | ||
if (options?.onSuccess) { | ||
await options.onSuccess(); | ||
} | ||
const toPath = normalizePath(response.url); | ||
const navigate = options?.navigate ?? navigateWithPageReload; | ||
navigate(toPath); | ||
} | ||
} | ||
@@ -115,0 +146,0 @@ } |
@@ -5,3 +5,3 @@ function __REGISTER__(feature, vaadinObj = window.Vaadin ??= {}) { | ||
is: feature ? `${"@vaadin/hilla-frontend"}/${feature}` : "@vaadin/hilla-frontend", | ||
version: "24.4.0-beta5" | ||
version: "24.4.0-rc1" | ||
}); | ||
@@ -8,0 +8,0 @@ } |
{ | ||
"name": "@vaadin/hilla-frontend", | ||
"version": "24.4.0-beta5", | ||
"version": "24.4.0-rc1", | ||
"description": "Hilla core frontend utils", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
110631
1137