tried it with axios

This commit is contained in:
2024-01-26 18:25:46 +01:00
parent 2a4dc80efa
commit c5fbb12dfd
4 changed files with 68 additions and 49 deletions

15
axios.config.js Normal file
View File

@ -0,0 +1,15 @@
import axios from 'axios';
import serversideConfig from './serversideConfig.js'
//create axios instance
const Axios = axios.create({
baseURL: `http://${serversideConfig.url}:${serversideConfig.port}`,
headers: {
// 'Accept': 'application/json',
'Content-Type': 'application/json',
//Authorization: `Bearer`,
Accept: "*",
},
})
export default Axios;

View File

@ -41,6 +41,7 @@
<script setup> <script setup>
import { ref } from 'vue'; import { ref } from 'vue';
// import LoginService from '../server/loginService.js'; // import LoginService from '../server/loginService.js';
import Axios from '../axios.config.js';
const darkMode = ref(true); const darkMode = ref(true);
</script> </script>
@ -53,52 +54,23 @@ export default {
const username = document.getElementById('username-input').value; const username = document.getElementById('username-input').value;
const password = document.getElementById('password-input').value; const password = document.getElementById('password-input').value;
const { data, error } = await useFetch('/api/login', { const requestBody = {
method: 'post', username: username,
body: { password: password,
username: username,
password: password
}
})
if (!data.value) {
console.log(error.value)
} else {
console.log(data.value)
} }
// }).then(res => {
// const data = res.data.value
// const error = res.error.value
// if (error) {
// // dealing error
// // console.log(error)
// } else {
// console.log(data)
// }
// }, error => {
// console.log('exception...')
// console.log(error)
// })
// console.log(data.value) try {
// console.log(error) let res = await Axios.post('/api/login', requestBody);
// }
// // const response = await LoginService.login(data); // something to do with the res?
// const { response } = await useFetch('/api/login') console.log(res)
// console.log(response)
// sucessfully logged in
// Axios.post('/api/login', data, { this.$router.push('/')
// headers: { } catch (err) {
// 'Content-Type': 'application/json' // handle the error
// } console.log(err.message)
// }) }
// .then(function (response) {
// console.log(response);
// })
// .catch(function (error) {
// console.log(error);
// });
} }
} }
} }

View File

@ -1,15 +1,30 @@
import { globalUserObject } from "../middleware/login";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const body = await readBody(event) const body = await readBody(event)
if ((body.username == 'test') && (body.password == 'asd')) { let correctUsername;
setResponseStatus(event, 202) let correctPassword;
return { if (globalUserObject !== null) {
login: 'successful' correctUsername = globalUserObject.username;
} correctPassword = globalUserObject.password;
} else { }
let authorized = false;
if (body.username == correctUsername && body.password == correctPassword) {
authorized = true;
}
if (!authorized) {
throw createError({ throw createError({
statusCode: 400, statusCode: 400,
statusMessage: 'Login failed', statusMessage: 'Username or password not valid.',
}) })
} }
setResponseStatus(event, 202)
return 'Successfully logged in.'
}) })

View File

@ -0,0 +1,17 @@
let globalUserObject: { [key: string]: any } | null = null;
// let globalUserObject = null;
export default defineEventHandler((event) => {
if (event.path.startsWith("/api/login")) {
// get user object from backend
const userObject = {
username: "test",
password: "asd"
}
globalUserObject = userObject;
}
})
export { globalUserObject };