@codemaster138/authy-client
Version:
Client for the authy API
91 lines (86 loc) • 2.67 kB
JavaScript
class Session {
constructor(g) {
this.globals = g;
}
/**
* Read from session storage
* @param {object} path What to read
*/
async read(path) {
// Wait for data from server
let data = await fetch(`${this.globals.protocol}://${this.globals.host}:${this.globals.port}/fromSession`, {
credentials: 'include',
headers: [
['content-type', 'application/json']
],
method: 'post',
body: JSON.stringify({
path: path
})
});
// Wait for json conversion
let json = await data.json();
// Return object
return json;
}
/**
* Store data in the current session. This data will be wiped when the session expires (after 24 hours)
* @param {object} data Data to store/overwrite
*/
async write(data) {
// Wait for response from server
await fetch(`${this.globals.protocol}://${this.globals.host}:${this.globals.port}/toSession`, {
credentials: 'include',
headers: [
['content-type', 'application/json']
],
method: 'post',
body: JSON.stringify({
data: data
})
});
return
}
/**
* Stores user data. These attributes will persist, even when the session expires. Note that the user must be logged in.
* @param {object} data Data to strore
*/
async writeUser(data) {
// Wait for response from server
await fetch(`${this.globals.protocol}://${this.globals.host}:${this.globals.port}/toAccount`, {
credentials: 'include',
headers: [
['content-type', 'application/json']
],
method: 'post',
body: JSON.stringify({
data: data
})
});
return
}
/**
* Read data from the user's account
* @param {object} path Template object
*/
async readUser(path) {
// Wait for data from server
let data = await fetch(`${this.globals.protocol}://${this.globals.host}:${this.globals.port}/fromAccount`, {
credentials: 'include',
headers: [
['content-type', 'application/json']
],
method: 'post',
body: JSON.stringify({
path: path
})
});
// Wait for json conversion
let json = await data.json();
// Return object
return json;
}
}
export default function createSession(g) {
return new Session(g);
}