You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

var User;
window.onload = async () =>
{
const fragment = new URLSearchParams(window.location.hash.slice(1));
const [accessToken, tokenType] = [fragment.get('access_token'), fragment.get('token_type')];
if (!accessToken) return window.location.href = '/';
User = await getUser(accessToken, tokenType);
};
async function getUser(accessToken, tokenType)
{
return await fetch(`${window.location.origin}/api/getUser`,
{
method: "POST",
headers: {
authorization: `${tokenType} ${accessToken}`,
},
})
.then(result => result.json())
.then(response =>
{
return response
})
.catch(console.error);
}
async function sendRequest(form)
{
var leagueName = form.children[0].children[1].value;
var players = form.children[1].children[0].children[0].children[0].value
var owner= User.id
var payload = {
name:leagueName,
players,
owner
}
return await fetch(`${window.location.origin}/api/createLeague`,
{
method: "POST",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body:JSON.stringify(payload),
})
.then(result => result.json())
.then(response =>
{
if(response.Error) alert(response.Error)
console.log(response)
location.replace(location.origin+'/leagues?league='+response.data._id);
})
.catch(console.error);
}