chat_front_svelte/src/lib/userFunction.js

120 lines
No EOL
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export async function UserCheck(){
const response = await fetch('http://localhost:8000/api/users/me', {
method: 'GET',
credentials:'include'
})
if(!response.ok){
window.location.href = '/'
return(response.status)
}
else{
const data = await response.json();
return data
}
}
export async function handleLogin(username, password) {
const response = await fetch('http://localhost:8000/api/users/login', {
method: 'POST',
credentials:'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify
({
email_or_username: username,
password: password
})
})
if(!response.ok)
{
console.log(response.status)
}
else
{
console.log('лох залогинен!');
window.location.href = '/chatPage'
}
}
export async function handleLogout() {
try{
const response = await fetch('http://localhost:8000/api/users/logout',
{
method:'POST',
credentials:'include'
})
if (response.ok)
{
console.log("ты вышел, лох");
window.location.href = '/'
} else
{
console.error('Не вышел, лошара');
}
} catch (error)
{
console.error('Ошибка при выполнении выхода:', error.message);
}
}
export async function getLastMessages(){
const response = await fetch('http://localhost:8000/api/chat/get_some_messages/2?messages_loaded=0&messages_to_get=14',
{
method:'GET',
credentials:'include'
})
if(!response.ok)
{
console.log(response.status)
}
if(response.ok)
{
let msgMassive = await response.json();
let localTime
for(let i = 0; i< msgMassive.length;i++){
localTime = new Date(msgMassive[i].created_at)
msgMassive[i].created_at = localTime
}
msgMassive.reverse();
console.log(msgMassive)
return msgMassive
}
}
export async function MessagePicToUrl(messagePic){
console.log(messagePic)
const DataForm = new FormData();
DataForm.append('file', messagePic)
const respone = await fetch('http://localhost:8000/api/images/upload_image',
{
method:"POST",
body:DataForm
})
if(!respone.ok)
console.log("ошибка", respone.status)
if(respone.ok){
const data = await respone.json();
console.log("картинка принята")
return data.image_url;
}
}