Files
HQNode/classes/Verification.js
2020-06-23 23:27:27 -05:00

147 lines
3.0 KiB
JavaScript

import https from 'https';
import Account from './Account';
const HOST = 'api-quiz.hype.space';
const headers = {
'x-hq-client': 'Android/1.12.2',
'content-type': 'application/json; charset=UTF-8',
'user-agent': 'okhttp/3.8.0',
};
export default class Verification {
constructor({
callsEnabled,
expires,
phone,
retrySeconds,
verificationId,
}) {
this.callsEnabled = callsEnabled;
this.expires = expires;
this.phone = phone;
this.retrySeconds = retrySeconds;
this.verificationId = verificationId;
}
/*
* confirm(string code)
*
* Confirmed phone number (sms code) and returns an authentication token.
*
*/
confirm(code, callback) {
let account;
let error;
const body = {
code,
};
const options = {
host: HOST,
path: '/verifications/' + this.verificationId,
method: 'POST',
headers: {
...headers,
'content-length': JSON.stringify(body).length,
},
};
const req = https.request(options, (res, err) => {
if (err) {
return { response: null, error: err };
}
res.on('data', (data) => {
account = JSON.parse(data);
console.log('account data', account);
if (account.auth && !account.auth.accessToken) {
if (account.error) {
error = account.error;
} else {
error = data;
}
callback({ response: account, error });
return;
} else if (account.auth) {
callback({ response: account, error: null });
return;
}
callback({ response: account, error });
});
});
req.write(JSON.stringify(body));
req.end();
}
/*
* create(Verification verification, string username, string referrer, string region)
*
* Creates a new user and returns its account info.
*
* Account: {
* userId,
* username,
* admin,
* tester,
* guest,
* avatarUrl,
* loginToken,
* accessToken,
* authToken,
* };
*
*/
create(username, referrer, callback) {
let account;
let error;
const body = {
country: 'GB',
language: 'en',
referringUsername: referrer,
username,
verificationId: this.verificationId,
};
const options = {
host: HOST,
path: '/users',
method: 'POST',
headers: {
...headers,
'content-length': JSON.stringify(body).length,
},
};
const req = https.request(options, (res, err) => {
if (err) {
return { response: null, error: err };
}
res.on('data', (data) => {
account = JSON.parse(data);
if (!account.accessToken) {
if (account.error) {
error = account.error;
} else {
error = data;
}
account = null;
} else {
account = new Account(account);
}
callback({ response: account, error });
});
});
req.write(JSON.stringify(body));
req.end();
}
}