48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
export default defineEventHandler(async (event) => {
|
|
const conf = useRuntimeConfig()
|
|
|
|
const body = await readBody(event)
|
|
|
|
const is_https = await getRequestProtocol(event) == "https"
|
|
//console.log(is_https)
|
|
|
|
if ( !body.email || !body.password )
|
|
throw createError({ statusCode: 400, message: "Bad form. Use e-mail and password" })
|
|
//console.log(body)
|
|
const req = await fetch(conf.fastapi_url + "/login",
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
email: body.email,
|
|
password: body.password,
|
|
})
|
|
}
|
|
)
|
|
|
|
if (!req.ok) throw createError({ statusCode: req.status })
|
|
else {
|
|
const body = await req.json();
|
|
|
|
await setUserSession(event,{
|
|
user: {
|
|
name: body.first_name,
|
|
surname: body.last_name,
|
|
email: body.email
|
|
},
|
|
secure: {
|
|
user_id: body.id
|
|
}
|
|
}, {
|
|
cookie: {
|
|
secure: is_https,
|
|
sameSite: is_https ? 'none' : 'lax'
|
|
}
|
|
})
|
|
console.log(await getUserSession(event))
|
|
|
|
return;
|
|
}
|
|
})
|