fix: fixes

This commit is contained in:
Denis Evers 2023-05-23 18:47:22 +08:00
parent 39b267b7d7
commit 3b8939f1f5
3 changed files with 52 additions and 30 deletions

View File

@ -1,14 +1,14 @@
// app.js
document.addEventListener("DOMContentLoaded", async () => {
const auth0Client = await handleAuthentication();
await handleAuthentication();
const logoutButton = document.getElementById("logout");
const purchaseTokensButton = document.getElementById("purchase-tokens");
if (logoutButton) {
logoutButton.addEventListener("click", (e) => {
logoutButton.addEventListener("click", async (e) => {
e.preventDefault();
auth0Client.logout({ returnTo: window.location.origin });
await logout();
sessionStorage.removeItem('userProfile'); // Add this line
});
} else {
@ -20,10 +20,9 @@ document.addEventListener("DOMContentLoaded", async () => {
console.log(user);
// Display user information
// Add the following code after the line where userProfile is defined
if (isAuthenticated) {
// Storing user profile information in sessionStorage
sessionStorage.setItem('userProfile', JSON.stringify(userProfile));
sessionStorage.setItem('userProfile', JSON.stringify(user));
// Alternatively, you could use localStorage which persists even when the browser is closed and reopened
// localStorage.setItem('userProfile', JSON.stringify(userProfile));

View File

@ -1,51 +1,72 @@
// auth.js
const auth0Client = auth0.createAuth0Client({
let auth0Client;
async function initializeAuth0Client() {
auth0Client = await auth0.createAuth0Client({
domain: "dev-g4e7bfpbacem6e8d.au.auth0.com",
clientId: "PaFcTx3jOQffBEMJaD6d3xnmgSmzmTzq",
redirect_uri: window.location.origin + "/callback",
});
});
}
async function login() {
await auth0Client.loginWithRedirect();
if (!auth0Client) {
await initializeAuth0Client();
}
await auth0Client.loginWithRedirect();
}
async function logout() {
await auth0Client.logout();
if (!auth0Client) {
await initializeAuth0Client();
}
await auth0Client.logout();
}
async function handleAuthentication() {
if (window.location.pathname === "/callback" && window.location.search.includes("code=")) {
await auth0Client.handleRedirectCallback();
window.history.replaceState({}, document.title, "/");
if (!auth0Client) {
await initializeAuth0Client();
}
if (window.location.pathname === "/callback" && window.location.search.includes("code=")) {
await auth0Client.handleRedirectCallback();
window.history.replaceState({}, document.title, "/");
const user = await getUser();
if (user) {
registerUserWithWorker(user.sub); // User's Auth0 ID is stored in the 'sub' field
}
const user = await getUser();
if (user) {
registerUserWithWorker(user.sub); // User's Auth0 ID is stored in the 'sub' field
}
}
}
async function isAuthenticated() {
return await auth0Client.isAuthenticated();
if (!auth0Client) {
await initializeAuth0Client();
}
return await auth0Client.isAuthenticated();
}
async function getUser() {
return await auth0Client.getUser();
if (!auth0Client) {
await initializeAuth0Client();
}
return await auth0Client.getUser();
}
async function registerUserWithWorker(auth0Id) {
const response = await fetch("/register", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ auth0Id })
});
const response = await fetch("/register", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ auth0Id })
});
if (response.ok) {
console.log("User registered successfully with worker");
} else {
console.error("Failed to register user with worker", response);
}
if (response.ok) {
console.log("User registered successfully with worker");
} else {
console.error("Failed to register user with worker", response);
}
}
initializeAuth0Client(); // Initialise the Auth0 client when the script loads

View File

@ -1,3 +1,5 @@
// chatgpt.js
async function getErrorFix(apiKey, engineCode, motorBrand, model) {
const prompt = `I am a diesel mechanic trying to diagnose an issue with a ${motorBrand} engine displaying error code ${engineCode}. What are the possible causes and solutions for this error code?`;