Skip to content

Commit

Permalink
adding/removing audiences
Browse files Browse the repository at this point in the history
  • Loading branch information
YannickFuereder committed Feb 1, 2025
1 parent 0250aa6 commit 8e84e85
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Assets/js/data/auth/oauth/addAudience.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export async function addAudience(clientId, url) {
await LoginManager.validateToken();
const req = await fetch(`https://api.login.${LoginManager.domain}/user/oauth/audiences`, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + LoginManager.getCookie('token'),
'Content-Type': 'application/json',
},
body: JSON.stringify({
clientId: clientId,
url: url,
}),
});

if (req.status == 401) {
window.location.href = LoginManager.buildLoginUrl(window.location.href);
return;
}

return req;
}
21 changes: 21 additions & 0 deletions Assets/js/data/auth/oauth/deleteAudience.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export async function deleteAudience(clientId, audienceId) {
await LoginManager.validateToken();
const req = await fetch(`https://api.login.${LoginManager.domain}/user/oauth/audiences`, {
method: 'DELETE',
headers: {
'Authorization': 'Bearer ' + LoginManager.getCookie('token'),
'Content-Type': 'application/json',
},
body: JSON.stringify({
clientId: clientId,
audienceId: audienceId,
}),
});

if (req.status == 401) {
window.location.href = LoginManager.buildLoginUrl(window.location.href);
return;
}

return req;
}
2 changes: 1 addition & 1 deletion Assets/js/data/auth/oauth/getClients.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export async function getClients() {
const req = await fetch(`https://api.login.${LoginManager.domain}/user/oauth`, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + token,
'Authorization': 'Bearer ' + LoginManager.getCookie('token'),
},
});

Expand Down
50 changes: 50 additions & 0 deletions Assets/js/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { getClients } from './data/auth/oauth/getClients';
import { getTrustedClients } from './data/auth/oauth/getTrustedClients';
import { untrustClient } from './data/auth/oauth/untrustClient';
import { linkSocialAccount } from './data/auth/linkSocialAccount';
import { deleteAudience as deleteAudienceReq } from './data/auth/oauth/deleteAudience';
import { addAudience as addAudienceReq } from './data/auth/oauth/addAudience';

const languages = [
{ key: 'en-us', name: 'English' },
Expand Down Expand Up @@ -458,6 +460,18 @@ async function deleteSSOClient(clientId) {
document.getElementById('sso_' + clientId).remove();
}

async function deleteAudience(clientId, audienceId) {
const req = await deleteAudienceReq(clientId, audienceId);
const res = await req.json();

if (res.statusCode != 200) {
console.log(res);
return;
}

document.getElementById('sso_audience_' + redirectId).remove();
}

async function deleteSSORedirect(clientId, redirectId) {
const req = await deleteRedirect(clientId, redirectId);
const res = await req.json();
Expand All @@ -470,6 +484,42 @@ async function deleteSSORedirect(clientId, redirectId) {
document.getElementById('sso_redirect_' + redirectId).remove();
}

async function addAudience(clientId) {
const item = document.getElementById('sso_' + clientId);
const redirects = item.querySelector('#sso_audiences');

//TODO: add button feedback

const req = await addAudienceReq(clientId, item.querySelector('#sso_addAudience').previousElementSibling.value);
const res = await req.json();

if (res.statusCode != 203) {
console.log(res);
return;
}

item.querySelector('#sso_addAudience').previousElementSibling.value = '';

const redirect = document.createElement('div');
redirect.id = 'sso_audience_' + res.data.id;
const url = document.createElement('input');
url.type = 'text';
url.value = res.data.url;
url.disabled = true;
redirect.appendChild(url);
const deleteBtn = document.createElement('button');
deleteBtn.innerText = 'Delete';
deleteBtn.addEventListener('click', async () => await deleteAudienceReq(clientId, res.data.id));
redirect.appendChild(deleteBtn);

if (redirects.children.length > 1) {
redirects.insertBefore(redirect, redirects.children[1]);
return;
}

redirects.append(redirect);
}

async function addSSORedirect(clientId) {
const item = document.getElementById('sso_' + clientId);
const redirects = item.querySelector('#sso_redirects');
Expand Down

0 comments on commit 8e84e85

Please sign in to comment.