implemented connection to the backend

This commit is contained in:
2024-02-06 17:18:37 +01:00
parent 876d066f3d
commit 73d0b89b4d
11 changed files with 409 additions and 62 deletions

View File

@ -1,17 +1,66 @@
let globalUserObject: { [key: string]: any } | null = null;
// let globalUserObject = null;
import axios, { AxiosError } from 'axios';
import serversideConfig from '../../serversideConfig';
import fs from 'fs';
import https from 'https';
export default defineEventHandler((event) => {
let loginSuccessful = false;
let errorMsg = '';
export default defineEventHandler(async (event) => {
loginSuccessful = false;
if (event.path.startsWith("/api/login")) {
const body = await readBody(event)
// read the certificate and create agent
const caCert = fs.readFileSync('./certs/server_ca.pem');
// const httpsAgent = new https.Agent({
// keepAlive: true,
// ca: caCert,
// rejectUnauthorized: true,
// });
const agent = new https.Agent({
rejectUnauthorized: false,
});
const axiosInstance = axios.create({
headers: {
'Content-Type': 'application/json',
Accept: "*",
},
httpsAgent: agent
});
// get user object from backend
const userObject = {
username: "test",
password: "asd"
try {
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/login`, {
username: body.username,
password: body.password,
});
loginSuccessful = true;
} catch (err) {
if (axios.isAxiosError(err)) {
const axiosError = err as AxiosError;
if (axiosError.response) {
// Axios error
console.error(axiosError.response.data.message);
errorMsg = axiosError.response.data.message;
} else if (axiosError.request) {
// If error was caused by the request
console.error(axiosError.request);
} else {
// Other errors
console.error('Error', axiosError.message);
}
} else {
// No AxiosError
console.error('Error', err);
}
}
globalUserObject = userObject;
}
})
export { globalUserObject };
export { loginSuccessful, errorMsg };

View File

@ -0,0 +1,83 @@
import axios, { AxiosError } from 'axios';
import serversideConfig from '../../serversideConfig';
import fs from 'fs';
import https from 'https';
let registeringSuccessful = false;
let errorMsg = '';
export default defineEventHandler(async (event) => {
registeringSuccessful = false;
if (event.path.startsWith("/api/signup")) {
const body = await readBody(event)
// read the certificate and create agent
const caCert = fs.readFileSync('./certs/server_ca.pem');
// const httpsAgent = new https.Agent({
// keepAlive: true,
// ca: caCert,
// rejectUnauthorized: true,
// });
const agent = new https.Agent({
rejectUnauthorized: false,
});
const axiosInstance = axios.create({
headers: {
'Content-Type': 'application/json',
Accept: "*",
},
httpsAgent: agent
});
// do the post request in the backend
try {
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/signup`, {
username: body.username,
password: body.password,
password_repeat: body.password_repeat,
lastLogin: body.lastLogin,
fullName: body.fullName,
email: body.email,
phonenumber: body.phonenumber,
address: body.address,
city: body.city,
postcode: body.postcode,
adminBool: body.adminBool,
technician1Bool: body.technician1Bool,
technician2Bool: body.technician2Bool,
technicianMonitoringBool: body.technicianMonitoringBool,
merchantBool: body.merchantBool,
internBool: body.internBool,
});
registeringSuccessful = true;
} catch (err) {
if (axios.isAxiosError(err)) {
const axiosError = err as AxiosError;
if (axiosError.response) {
// Axios error
console.error(axiosError.response.data.message);
errorMsg = axiosError.response.data.message;
} else if (axiosError.request) {
// If error was caused by the request
console.error(axiosError.request);
} else {
// Other errors
console.error('Error', axiosError.message);
}
} else {
// No AxiosError
console.error('Error', err);
}
}
}
})
export { registeringSuccessful, errorMsg };
registeringSuccessful = false;