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,30 +1,14 @@
import { globalUserObject } from "../middleware/login";
import { loginSuccessful, errorMsg } from "../middleware/login";
export default defineEventHandler(async (event) => {
const body = await readBody(event)
let correctUsername;
let correctPassword;
if (globalUserObject !== null) {
correctUsername = globalUserObject.username;
correctPassword = globalUserObject.password;
}
let authorized = false;
if (body.username == correctUsername && body.password == correctPassword) {
authorized = true;
}
if (!authorized) {
if (!loginSuccessful) {
throw createError({
statusCode: 400,
statusMessage: 'Username or password not valid.',
statusMessage: errorMsg,
})
}
setResponseStatus(event, 202)
setResponseStatus(event, 200)
return 'Successfully logged in.'
})

14
server/api/signup.ts Normal file
View File

@ -0,0 +1,14 @@
import { registeringSuccessful, errorMsg } from "../middleware/signUp.js";
export default defineEventHandler(async (event) => {
if (!registeringSuccessful) {
throw createError({
statusCode: 400,
statusMessage: errorMsg,
})
}
setResponseStatus(event, 202)
return 'Successfully signed up.'
})

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;