masto-fe-standalone/public/auth.js

115 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2023-10-12 18:13:42 +00:00
document.addEventListener("DOMContentLoaded", async function() {
await ready();
});
async function ready() {
const domain = localStorage.getItem('domain');
let accessToken = localStorage.getItem(`access_token`);
if (domain) document.getElementById('instance').value = domain;
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
if (domain && code && !accessToken) await getToken(code, domain).then(res => accessToken = res);
if (accessToken) {
window.location.href = '/prepare.html';
}
}
async function auth() {
setMessage('Please wait');
2024-12-15 16:19:57 +00:00
2023-10-12 18:13:42 +00:00
const instance = document.getElementById('instance').value;
2024-12-15 16:19:57 +00:00
const matches = instance.match(/((?:http|https):\/\/)?(.*)/);
const protocol = matches[1];
if (protocol) {
localStorage.setItem('protocol', protocol);
}
const domain = matches[2];
2023-10-12 18:13:42 +00:00
if (!domain) {
setMessage('Invalid instance', false);
return;
}
localStorage.setItem('domain', domain);
2024-12-15 16:19:57 +00:00
// We need to run this every time in cases like Iceshrimp,
// where the client id/secret aren't reusable (yet) because
// they contain use-once session information.
2023-10-12 18:13:42 +00:00
await registerApp(domain);
authorize(domain);
}
async function registerApp(domain) {
setMessage('Registering app');
2024-12-15 16:19:57 +00:00
const protocol = localStorage.getItem(`protocol`) ?? `https://`;
const appsUrl = `${protocol}${domain}/api/v1/apps`;
2023-10-12 18:13:42 +00:00
const formData = new FormData();
2024-12-19 13:28:35 +00:00
formData.append('client_name', 'Masto-FE (🦥 flavour)');
2024-12-21 14:26:26 +00:00
formData.append('website', 'https://codeberg.org/superseriousbusiness/masto-fe-standalone');
2023-10-12 18:13:42 +00:00
formData.append('redirect_uris', document.location.origin + document.location.pathname);
formData.append('scopes', 'read write follow push');
// eslint-disable-next-line promise/catch-or-return
await fetch(appsUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams(formData),
})
.then(async res => {
const app = await res.json();
localStorage.setItem(`client_id`, app.client_id);
localStorage.setItem(`client_secret`, app.client_secret);
});
}
function authorize(domain) {
setMessage('Authorizing');
const clientId = localStorage.getItem(`client_id`);
2024-12-15 16:19:57 +00:00
const protocol = localStorage.getItem(`protocol`) ?? `https://`;
document.location.href = `${protocol}${domain}/oauth/authorize?response_type=code&client_id=${clientId}&redirect_uri=${document.location.origin + document.location.pathname}&scope=read+write+follow+push`;
2023-10-12 18:13:42 +00:00
}
async function getToken(code, domain) {
setMessage('Getting token');
2024-12-15 16:19:57 +00:00
const protocol = localStorage.getItem(`protocol`) ?? `https://`;
const tokenUrl = `${protocol}${domain}/oauth/token`;
2023-10-12 18:13:42 +00:00
const clientId = localStorage.getItem(`client_id`);
const clientSecret = localStorage.getItem(`client_secret`);
const formData = new FormData();
formData.append('grant_type', 'authorization_code');
formData.append('code', code);
formData.append('client_id', clientId);
formData.append('client_secret', clientSecret);
formData.append('scope', 'read write follow push');
formData.append('redirect_uri', document.location.origin + document.location.pathname);
// eslint-disable-next-line promise/catch-or-return
return fetch(tokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams(formData),
})
.then(async res => {
const app = await res.json();
if (app.access_token) localStorage.setItem(`access_token`, app.access_token);
return app.access_token;
});
}
function setMessage(message, disabled = true) {
document.getElementById('message').textContent = message;
document.getElementById('btn').disabled = disabled;
2024-12-15 16:19:57 +00:00
}