35 lines
No EOL
914 B
JavaScript
35 lines
No EOL
914 B
JavaScript
import { PUBLIC_URL } from '$env/static/public';
|
|
|
|
export async function handleLogin(username, password) {
|
|
|
|
const response = await fetch(`${PUBLIC_URL}/api/users/login`, {
|
|
method: 'POST',
|
|
credentials:'include',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify
|
|
({
|
|
email_or_username: username,
|
|
password: password
|
|
})
|
|
|
|
})
|
|
|
|
if(response.status === 200)
|
|
{
|
|
console.log("залогинен")
|
|
|
|
const data = await response.json()
|
|
const token = data.authorization
|
|
localStorage.setItem('BPChat', token)
|
|
window.location.href = '/chat'
|
|
return "" // чтобы ничего не выводилось в качестве ошибки
|
|
}
|
|
else if(response.status === 401)
|
|
{
|
|
return "Неправильный логин или пароль"
|
|
} else {
|
|
console.log(response)
|
|
}
|
|
} |