removed Login component

This commit is contained in:
2023-03-01 10:24:10 +01:00
parent b6841591c4
commit ca4480b122
5 changed files with 177 additions and 59 deletions

159
src/util/net.js Normal file
View File

@ -0,0 +1,159 @@
/**
* Network related functions.
*
* markus.thielen@basebox.health
*/
import store from '@/store'
import router from '@/router'
/**
* GqlError - custom error thrown by the gqlQuery function.
*
* Since the GraphQL server might through multiple errors at once,
* this error class uses a message array.
*/
class GqlError extends Error {
/**
* Construct a GqlError.
*
* GqlError handles errors received from the GraphQL server as well as
* JavaScript errors thrown by the fetch API etc.
*
* @param {*} error JSON server response or Error instance or just a string.
*/
constructor(error) {
/* create array of error message strings */
const errorMessages = [];
if (error instanceof Error) {
/* JavaScript Error instance */
let msg = error.toString();
if (msg.indexOf("Failed to fetch") > 0) {
/* convert to a user-friendly message */
msg = "Failed to connect to basebox server.";
}
errorMessages.push(msg);
} else if (error instanceof String) {
errorMessages.push(error);
} else {
/* assume this is a GraphQL server response (JSON) */
try {
for (const e of error.errors) {
let s = e.message;
/* add info from extension, if available */
if (e.extensions) {
if (e.code) {
s += ` - ${e.extensions.code}`;
}
if (e.reason) {
s += ` - ${e.extensions.reason}`;
}
}
errorMessages.push(s);
}
} catch(e) {
errorMessages.push("Failed to interpret error: " + e.toString());
errorMessages.push(error.toString());
}
}
/* concat errors into a single message for the parent classes */
super(errorMessages.join(" / "));
this.errors = error.errors;
this.messages = errorMessages;
this.name = "GqlError";
}
/**
* Check if this error is an authentication error (401).
*/
is401() {
/* check if one of the server errors has a 401 extension */
for (const error of this.errors) {
if (error.extensions && error.extensions.code.toString() === "401") {
return true;
}
}
return false;
}
}
/**
* Send GraphQL request to the configured server.
*
* @param {String} query the GraphQL query/mutation to send
* @returns Promise with response JSON's data member on success,
* Error with error message array on failure
*/
export function gqlQuery(query)
{
/* prepare fetch options */
const fetchOpt = {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ query: query }),
};
/* if we're logged in, we add the authorization header */
if (store.getters.isLoggedIn) {
fetchOpt.headers["Authorization"] = store.getters.authData.token;
}
console.info(fetchOpt);
return fetch(store.getters.gqlUrl, fetchOpt).then(
/* fetch success */
async response => {
if (response.ok) {
/* convert to JSON */
const rspJson = await response.json();
if (rspJson.data) {
/* success */
return new Promise((resolve, reject) => {
resolve(rspJson.data);
});
} else {
/* some errors... */
return new Promise((resolve, reject) => {
const error = new GqlError(rspJson);
/* if this is a 401, we redirect to the login page */
if (error.is401()) {
router.push({
name: "Login",
query: {
next: router.currentRoute.fullPath,
}
});
}
reject(new GqlError(rspJson));
});
}
} else {
/* low level/network error */
return new Promise((resolve, reject) => {
reject(new GqlError(response.statusText));
});
}
},
/* fetch failure */
reason => {
return new Promise((resolve, reject) => {
reject(new GqlError(reason));
});
}
);
}