Files
molecular_frontend/src/Api/Keycloak/Keycloak.tsx
DeOwl f3372eb0ad v1.0
- added task rendering instead of molecule
- fied a lot of errors
- changes experiment page to show generic experiment info
2026-05-25 12:55:46 +03:00

95 lines
2.5 KiB
TypeScript

import axios from "axios";
import Keycloak, { type KeycloakProfile } from "keycloak-js";
import { routes } from "Routes/Routes";
const keycloak = new Keycloak({
url: import.meta.env.VITE_KEYCLOAK_URL
? import.meta.env.VITE_KEYCLOAK_URL
: "",
realm: "quant_sim-realm",
clientId: "react-frontend",
});
export default keycloak;
export async function getProfile(keycloak: Keycloak) {
return keycloak.loadUserProfile();
}
export const updateUserData = async (
profile: KeycloakProfile,
): Promise<KeycloakProfile | undefined> => {
try {
await keycloak.updateToken(30);
const response = await axios.post(
`${keycloak.authServerUrl}/realms/${keycloak.realm}/account/`,
{
firstName: profile.firstName,
lastName: profile.lastName,
email: profile.email,
username: profile.username,
attributes: profile.attributes || {},
},
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${keycloak.token}`,
},
withCredentials: true, // Important: allows credentials in CORS
},
);
if (response.status === 204) {
// Refresh the local profile after successful update
const updatedProfile = await keycloak.loadUserProfile();
return updatedProfile;
}
} catch (err) {
console.error(err);
}
};
export const updatePasswordWithRedirect = async (
successRedirectUrl?: string,
) => {
try {
await keycloak.updateToken(30);
// Trigger the UPDATE_PASSWORD required action
// This will redirect the user to Keycloak's password change page
return keycloak.login({
action: "UPDATE_PASSWORD",
redirectUri:
successRedirectUrl ||
window.location.origin +
"/" +
routes.SettingsPage.path +
"?password_updated=true",
// Optional: Force re-authentication
prompt: "login",
});
} catch (error) {
console.error("Failed to initiate password update:", error);
throw error;
}
};
export const SendEmailVerification = async () => {
try {
await keycloak.updateToken(30);
// Trigger the UPDATE_PASSWORD required action
// This will redirect the user to Keycloak's password change page
return keycloak.login({
action: "VERIFY_EMAIL",
redirectUri:
window.location.origin +
"/" +
routes.SettingsPage.path +
"?email_sent=true",
});
} catch (error) {
console.error("Failed to initiate password update:", error);
throw error;
}
};