mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-17 07:04:36 +00:00
78 lines
1.6 KiB
TypeScript
78 lines
1.6 KiB
TypeScript
|
|
import {
|
|
SessionResponse,
|
|
} from "./types";
|
|
|
|
const apiHost = "https://api.fakeyou.com";
|
|
// const createAccount = `${apiHost}}/create_account`;
|
|
const login = `${apiHost}/v1/login`;
|
|
const getSession = `${apiHost}/v1/session`;
|
|
const logout = `${apiHost}/v1/logout`;
|
|
|
|
export const CreateSession = async ({
|
|
usernameOrEmail, password
|
|
}:{
|
|
usernameOrEmail: string;
|
|
password: string;
|
|
})=> {
|
|
//TODO: This SHOULD NOT be sent in plaintext
|
|
const request = {
|
|
username_or_email: usernameOrEmail,
|
|
password: password,
|
|
}
|
|
|
|
return await fetch(login,{
|
|
method: 'POST',
|
|
headers: {
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json",
|
|
},
|
|
credentials: 'include',
|
|
body: JSON.stringify(request)
|
|
})
|
|
.then(res => res.json())
|
|
.then(res => { return res; })
|
|
.catch(e => {
|
|
return {
|
|
success: false,
|
|
error_reason: "frontend_error",
|
|
};
|
|
});
|
|
}
|
|
export const GetSession = async () => {
|
|
return await fetch(getSession, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
},
|
|
credentials: 'include',
|
|
})
|
|
.then(res => res.json())
|
|
.then(res => {
|
|
const response : SessionResponse = res;
|
|
return response;
|
|
})
|
|
.catch(e => {
|
|
return {
|
|
success: false,
|
|
error_reason: "frontend_error",
|
|
};
|
|
});
|
|
}
|
|
export async function DestroySession() : Promise<boolean> {
|
|
return await fetch(logout, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
},
|
|
credentials: 'include',
|
|
})
|
|
.then(res => res.json())
|
|
.then(res => {
|
|
const response : SessionResponse = res;
|
|
return response.success;
|
|
})
|
|
.catch(e => {
|
|
return false;
|
|
});
|
|
} |