Skip to content

Commit

Permalink
Add SPA Form Based Authentication instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Nov 1, 2023
1 parent 58bc0c4 commit 2826a83
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/src/main/asciidoc/security-authentication-mechanisms.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,53 @@ quarkus.http.auth.form.login-page=
quarkus.http.auth.form.error-page=
----

Now that you have disabled redirects for the SPA, you must login and logout programmatically from your client.
Below are example JavaScript methods for logging into the `j_security_check` endpoint and logging out of the application by destroying the cookie.

[source,javascript]
----
const login = () => {
// Create an object to represent the form data
const formData = new URLSearchParams();
formData.append("j_username", username);
formData.append("j_password", password);
// Make an HTTP POST request using fetch against j_security_check endpoint
fetch("j_security_check", {
method: "POST",
body: formData,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
})
.then((response) => {
if (response.status === 200) {
// Authentication was successful
console.log("Authentication successful");
} else {
// Authentication failed
console.error("Invalid credentials");
}
})
.catch((error) => {
console.error(error);
});
};
----

To logout of the SPA you must destroy the cookie and redirect back to your main page.

[source,javascript]
----
const logout= () => {
// delete the credential cookie essentially killing the session
const removeCookie = `quarkus-credential=; Max-Age=0;path=/`;
document.cookie = removeCookie;
// redirect to main page
window.location.href = "/";
};
----

The following properties can be used to configure form-based authentication:

include::{generated-dir}/config/quarkus-vertx-http-config-group-form-auth-config.adoc[opts=optional, leveloffset=+1]
Expand Down

0 comments on commit 2826a83

Please sign in to comment.