Added support for new login/token endpoints.
This commit is contained in:
parent
4acdf6090f
commit
db20f087b1
6 changed files with 124 additions and 81 deletions
|
@ -66,6 +66,7 @@ const Flashii = function(baseUrl) {
|
|||
body=null,
|
||||
headers=null,
|
||||
type='json',
|
||||
authed=false,
|
||||
}) => {
|
||||
return new Commitment((success, fail) => {
|
||||
const url = createUrl(path, fields);
|
||||
|
@ -80,7 +81,7 @@ const Flashii = function(baseUrl) {
|
|||
headers ??= {};
|
||||
if(fresh) headers['Cache-Control'] = 'no-cache';
|
||||
|
||||
const options = { type, headers };
|
||||
const options = { type, headers, authed };
|
||||
|
||||
$xhr.send(method, url, options, body)
|
||||
.success(success)
|
||||
|
@ -93,6 +94,39 @@ const Flashii = function(baseUrl) {
|
|||
|
||||
fii.v1 = {};
|
||||
|
||||
fii.v1.chat = {};
|
||||
fii.v1.chat.login = function({ redirect=null, assign=true }) {
|
||||
return new Commitment(success => {
|
||||
redirect ??= `${location.protocol}//${location.host}`;
|
||||
if(typeof redirect !== 'string')
|
||||
throw new Error('redirect must a string.');
|
||||
|
||||
const url = createUrl('/v1/chat/login');
|
||||
url.setParam('redirect', redirect);
|
||||
|
||||
// intentionally does not call success
|
||||
if(assign)
|
||||
location.assign(url);
|
||||
else
|
||||
success(url);
|
||||
});
|
||||
};
|
||||
fii.v1.chat.token = function() {
|
||||
return new Commitment((success, fail) => {
|
||||
send({ method: 'GET', path: '/v1/chat/token', authed: true, fresh: true })
|
||||
.success(({ status, body }) => {
|
||||
if(status === 403)
|
||||
throw new Error('You must be logged in to use chat.');
|
||||
if(status > 299)
|
||||
throw new Error(`Failed to fetch authorization token with error code ${status}.`);
|
||||
|
||||
success(body);
|
||||
})
|
||||
.fail(fail)
|
||||
.run();
|
||||
});
|
||||
};
|
||||
|
||||
const verifyColourPresetName = name => {
|
||||
if(/^([^A-Za-z0-9\-_]+)$/gu.test(name))
|
||||
throw new Error('name argument is not an acceptable colour preset name.');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue