Files
TueIT_App/server/middleware/signUp.ts

83 lines
2.7 KiB
TypeScript

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;