@codemaster138/authy-client
Version:
Client for the authy API
30 lines • 1.21 kB
JavaScript
import createSession from './session';
export default async function login(g, config) {
return new Promise((resolve, reject) => {
if (!g.app) reject('Must define app before using authy');
if (!config.username) reject('Missing required argument: config.username');
if (!config.password) reject('Missing required argument: config.password');
config.setCookie = config.setCookie || true; // Whether to automatically set the cookie
fetch(`${g.protocol}://${g.host}:${g.port}/login`, {
headers: [
['content-type', 'application/json']
],
method: 'post',
credentials: 'include',
body: JSON.stringify({
app: g.app,
name: config.username,
pass: config.password
})
})
.then(res => {
if (res.status.toString()[0] === '2') return res.text().then(data => {
g.setSession(createSession(g));
resolve(data);
});
return res.text().then(data => reject(data));
}).catch(err => {
reject(err);
})
});
}