tried it with axios
This commit is contained in:
@ -1,15 +1,30 @@
|
||||
import { globalUserObject } from "../middleware/login";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
|
||||
const body = await readBody(event)
|
||||
|
||||
if ((body.username == 'test') && (body.password == 'asd')) {
|
||||
setResponseStatus(event, 202)
|
||||
return {
|
||||
login: 'successful'
|
||||
}
|
||||
} else {
|
||||
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) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Login failed',
|
||||
statusMessage: 'Username or password not valid.',
|
||||
})
|
||||
}
|
||||
|
||||
setResponseStatus(event, 202)
|
||||
return 'Successfully logged in.'
|
||||
|
||||
})
|
||||
|
||||
17
server/middleware/login.ts
Normal file
17
server/middleware/login.ts
Normal 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 };
|
||||
Reference in New Issue
Block a user