2023-10-09 14:49:11 +00:00
document . addEventListener ( "DOMContentLoaded" , async function ( ) {
await ready ( ) ;
} ) ;
async function ready ( ) {
const domain = localStorage . getItem ( 'domain' ) ;
2023-10-09 17:37:52 +00:00
let accessToken = localStorage . getItem ( ` access_token ` ) ;
2023-10-09 14:49:11 +00:00
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 ) ;
2023-10-09 17:37:52 +00:00
if ( accessToken ) {
window . location . href = '/prepare.html' ;
}
2023-10-09 14:49:11 +00:00
}
async function auth ( ) {
setMessage ( 'Please wait' ) ;
const instance = document . getElementById ( 'instance' ) . value ;
const domain = instance . match ( /(?:https?:\/\/)?(.*)/ ) [ 1 ] ;
if ( ! domain ) {
2023-10-09 17:31:31 +00:00
setMessage ( 'Invalid instance' , false ) ;
2023-10-09 14:49:11 +00:00
return ;
}
localStorage . setItem ( 'domain' , domain ) ;
2023-10-09 17:31:31 +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
await registerApp ( domain ) ;
2023-10-09 14:49:11 +00:00
authorize ( domain ) ;
}
async function registerApp ( domain ) {
setMessage ( 'Registering app' ) ;
const appsUrl = ` https:// ${ domain } /api/v1/apps ` ;
const formData = new FormData ( ) ;
formData . append ( 'client_name' , 'Masto-FE standalone' ) ;
2023-10-09 16:18:34 +00:00
formData . append ( 'redirect_uris' , document . location . origin + document . location . pathname ) ;
2023-10-09 14:49:11 +00:00
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 ( ) ;
2023-10-09 17:37:52 +00:00
localStorage . setItem ( ` client_id ` , app . client _id ) ;
localStorage . setItem ( ` client_secret ` , app . client _secret ) ;
2023-10-09 14:49:11 +00:00
} ) ;
}
function authorize ( domain ) {
setMessage ( 'Authorizing' ) ;
2023-10-09 17:37:52 +00:00
const clientId = localStorage . getItem ( ` client_id ` ) ;
2023-10-09 16:18:34 +00:00
document . location . href = ` https:// ${ domain } /oauth/authorize?response_type=code&client_id= ${ clientId } &redirect_uri= ${ document . location . origin + document . location . pathname } &scope=read+write+follow+push ` ;
2023-10-09 14:49:11 +00:00
}
async function getToken ( code , domain ) {
setMessage ( 'Getting token' ) ;
const tokenUrl = ` https:// ${ domain } /oauth/token ` ;
2023-10-09 17:37:52 +00:00
const clientId = localStorage . getItem ( ` client_id ` ) ;
const clientSecret = localStorage . getItem ( ` client_secret ` ) ;
2023-10-09 14:49:11 +00:00
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' ) ;
2023-10-09 16:18:34 +00:00
formData . append ( 'redirect_uri' , document . location . origin + document . location . pathname ) ;
2023-10-09 14:49:11 +00:00
// 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 ( ) ;
2023-10-09 17:37:52 +00:00
if ( app . access _token ) localStorage . setItem ( ` access_token ` , app . access _token ) ;
2023-10-09 14:49:11 +00:00
return app . access _token ;
} ) ;
}
2023-10-09 17:31:31 +00:00
function setMessage ( message , disabled = true ) {
2023-10-09 14:49:11 +00:00
document . getElementById ( 'message' ) . textContent = message ;
2023-10-09 17:31:31 +00:00
document . getElementById ( 'btn' ) . disabled = disabled ;
2023-10-09 14:49:11 +00:00
}