Attempt at better handling of auth redirect.

This commit is contained in:
flash 2025-05-19 11:13:31 +00:00
parent 3a8659de69
commit 911bf2f934
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
3 changed files with 37 additions and 36 deletions
src/mami.js

View file

@ -22,9 +22,10 @@ const MamiFlashiiAuth = function(flashii) {
async refresh() {
try {
({ token_type: type=null, access_token: token=null } = await flashii.v1.chat.token({}));
({ token_type: type=null, access_token: token=null } = await flashii.v1.chat.token());
} catch(ex) {
//await flashii.v1.chat.login({}); what if the internet didn't suck balls
if(ex instanceof Error && ex.cause === 'flashii.v1.unauthorized')
await flashii.v1.chat.login();
}
},
};

View file

@ -70,7 +70,7 @@ const Flashii = function(baseUrl) {
if(typeof id === 'number')
id = id.toString();
if(/^([^0-9]+)$/gu.test(id))
throw new Error('id argument is not an acceptable chat server id.');
throw new Error('id argument is not an acceptable chat server id.', { cause: 'flashii.v1.chat.servers.id' });
return id;
};
@ -79,7 +79,7 @@ const Flashii = function(baseUrl) {
return new Promise(resolve => {
redirect ??= `${location.protocol}//${location.host}`;
if(typeof redirect !== 'string')
throw new Error('redirect must a string.');
throw new Error('redirect must a string.', { cause: 'flashii.v1.chat.login.redirect' });
const url = createUrl('/v1/chat/login');
url.searchParams.set('redirect', redirect);
@ -95,7 +95,7 @@ const Flashii = function(baseUrl) {
const params = {};
if(proto !== null && typeof proto !== 'string')
throw new Error('proto must be a string or null');
throw new Error('proto must be a string or null', { cause: 'flashii.v1.chat.servers.proto' });
params['proto'] = proto;
if(secure !== null)
@ -104,9 +104,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/chat/servers', fields, fresh, params });
if(status === 400)
throw new Error('An argument contains an unsupported value.');
throw new Error('An argument contains an unsupported value.', { cause: 'flashii.v1.value' });
if(status > 299)
throw new Error(`Failed to fetch chat server list with error code ${status}.`);
throw new Error(`Failed to fetch chat server list with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -116,11 +116,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/chat/servers/${id}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('No chat server with that id could be found.');
throw new Error('No chat server with that id could be found.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch chat server with error code ${status}.`);
throw new Error(`Failed to fetch chat server with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -135,11 +135,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/chat/servers/recommended', fields, fresh, params });
if(status === 400)
throw new Error('An argument contains an unsupported value.');
throw new Error('An argument contains an unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Was unable to determine a server based on the requested parameters.');
throw new Error('Was unable to determine a server based on the requested parameters.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch recommended chat server with error code ${status}.`);
throw new Error(`Failed to fetch recommended chat server with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -147,9 +147,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/chat/token', authed: true, fresh: true });
if(status === 403)
throw new Error('You must be logged in to use chat.');
throw new Error('You must be logged in to use chat.', { cause: 'flashii.v1.unauthorized' });
if(status > 299)
throw new Error(`Failed to fetch authorization token with error code ${status}.`);
throw new Error(`Failed to fetch authorization token with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -165,9 +165,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/colours/presets', fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status > 299)
throw new Error(`Failed to fetch colour presets with error code ${status}.`);
throw new Error(`Failed to fetch colour presets with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -177,11 +177,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/colours/presets/${name}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Requested colour preset does not exist.');
throw new Error('Requested colour preset does not exist.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch colour preset "${name}" with error code ${status}.`);
throw new Error(`Failed to fetch colour preset "${name}" with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -190,7 +190,7 @@ const Flashii = function(baseUrl) {
if(typeof id === 'number')
id = id.toString();
if(/^([^0-9]+)$/gu.test(id))
throw new Error('id argument is not an acceptable emoticon id.');
throw new Error('id argument is not an acceptable emoticon id.', { cause: 'flashii.v1.emotes.id' });
return id;
};
@ -198,9 +198,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/emotes', fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status > 299)
throw new Error(`Failed to fetch emoticons with error code ${status}.`);
throw new Error(`Failed to fetch emoticons with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -210,11 +210,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/emotes/${id}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Requested emoticon does not exist.');
throw new Error('Requested emoticon does not exist.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch emoticon "${id}" with error code ${status}.`);
throw new Error(`Failed to fetch emoticon "${id}" with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -224,11 +224,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/emotes/${id}/strings`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Requested emoticon does not exist.');
throw new Error('Requested emoticon does not exist.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch emoticon "${id}" with error code ${status}.`);
throw new Error(`Failed to fetch emoticon "${id}" with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -237,7 +237,7 @@ const Flashii = function(baseUrl) {
if(typeof id === 'number')
id = id.toString();
if(/^([^0-9]+)$/gu.test(id))
throw new Error('id argument is not an acceptable kaomoji id.');
throw new Error('id argument is not an acceptable kaomoji id.', { cause: 'flashii.v1.kaomoji.id' });
return id;
};
@ -245,9 +245,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/kaomoji', params: { as }, fields, fresh });
if(status === 400)
throw new Error('As or fields argument contains unsupported value.');
throw new Error('As or fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status > 299)
throw new Error(`Failed to fetch kaomoji with error code ${status}.`);
throw new Error(`Failed to fetch kaomoji with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -257,11 +257,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/kaomoji/${id}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Requested kaomoji does not exist.');
throw new Error('Requested kaomoji does not exist.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch kaomoji "${id}" with error code ${status}.`);
throw new Error(`Failed to fetch kaomoji "${id}" with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};

View file

@ -117,7 +117,7 @@ const MamiSockChatHandlers = function(
}
if(ev.detail.session.needsAuth) {
ctx.flashii.v1.chat.login({});
ctx.flashii.v1.chat.login();
return;
}