2024-04-26 21:12:56 +01:00
if ( localStorage . getItem ( "DONOTSHARE-secretkey" ) === null ) {
window . location . replace ( "/login" )
document . body . innerHTML = "Redirecting..."
throw new Error ( ) ;
}
let remote = localStorage . getItem ( "homeserverURL" )
if ( remote == null ) {
2024-04-26 21:15:43 +01:00
localStorage . setItem ( "homeserverURL" , "https://auth.hectabit.org" )
remote = "https://auth.hectabit.org"
2024-04-26 21:12:56 +01:00
}
function attempt ( ) {
2024-05-06 12:22:13 +01:00
if ( document . getElementById ( "appidbox" ) . value != "" ) {
2024-04-26 21:12:56 +01:00
fetch ( origin + "/api/newauth" , {
method : "POST" ,
headers : {
"Content-Type" : "application/json"
} ,
body : JSON . stringify ( {
2024-05-06 12:53:04 +01:00
name : document . getElementById ( "appidbox" ) . value ,
2024-04-26 21:12:56 +01:00
rdiruri : document . getElementById ( "rdiruribox" ) . value ,
secretKey : localStorage . getItem ( "DONOTSHARE-secretkey" )
} )
} )
. then ( response => {
async function doStuff ( ) {
let code = await response . json ( )
2024-05-06 12:53:04 +01:00
if ( response . status === 200 ) {
document . getElementById ( "status" ) . innerText = "Your secret key is: " + code [ "key" ] + " and your client id is: " + code [ "appId" ] + ". This will only be shown once!"
2024-04-26 21:12:56 +01:00
getauths ( ) ;
2024-05-06 12:53:04 +01:00
} else if ( response . status === 500 ) {
2024-04-26 21:12:56 +01:00
document . getElementById ( "status" ) . innerText = "Whoops... Something went wrong. Please try again later. (Error Code 500)"
2024-05-06 12:53:04 +01:00
} else if ( response . status === 401 ) {
2024-04-26 21:12:56 +01:00
document . getElementById ( "status" ) . innerText = "AppID already taken. (Error Code 401)"
} else {
document . getElementById ( "status" ) . innerText = "Unkown error encountered. (Error Code " + response . status + ")"
}
}
doStuff ( )
} )
}
}
function getauths ( ) {
fetch ( origin + "/api/listauth" , {
method : "POST" ,
body : JSON . stringify ( {
secretKey : localStorage . getItem ( "DONOTSHARE-secretkey" )
} ) ,
headers : {
"Content-Type" : "application/json; charset=UTF-8"
}
} )
. then ( ( response ) => {
async function doStuff ( ) {
let responseData = await response . json ( )
document . querySelectorAll ( ".oauthentry" ) . forEach ( ( el ) => el . remove ( ) ) ;
for ( let i in responseData ) {
let oauthElement = document . createElement ( "div" )
let oauthText = document . createElement ( "p" )
2024-05-06 12:53:04 +01:00
let oauthName = document . createElement ( "p" )
let oauthUrl = document . createElement ( "p" )
2024-04-26 21:12:56 +01:00
let oauthRemoveButton = document . createElement ( "button" )
oauthText . innerText = "Client ID: " + responseData [ i ] [ "appId" ]
2024-05-06 12:53:04 +01:00
oauthName . innerText = "App name: " + responseData [ i ] [ "name" ]
oauthUrl . innerText = "Redirect Url: " + responseData [ i ] [ "rdiruri" ]
2024-04-26 21:12:56 +01:00
oauthRemoveButton . innerText = "Delete Permanently"
2024-05-06 12:53:04 +01:00
oauthRemoveButton . addEventListener ( "click" , ( ) => {
if ( window . confirm ( "Are you SURE you would like to delete this FOREVER?" ) === true ) {
2024-04-26 21:12:56 +01:00
fetch ( origin + "/api/deleteauth" , {
method : "POST" ,
body : JSON . stringify ( {
secretKey : localStorage . getItem ( "DONOTSHARE-secretkey" ) ,
appId : responseData [ i ] [ "appId" ]
} ) ,
headers : {
"Content-Type" : "application/json; charset=UTF-8"
}
} )
oauthElement . remove ( )
}
} ) ;
oauthElement . append ( oauthText )
2024-05-06 12:22:13 +01:00
oauthElement . append ( oauthName )
oauthElement . append ( oauthUrl )
2024-04-26 21:12:56 +01:00
oauthElement . append ( oauthRemoveButton )
oauthElement . classList . add ( "oauthentry" )
document . getElementById ( "oauthlist" ) . append ( oauthElement )
}
}
doStuff ( )
} ) ;
}
getauths ( )