Skip to content

Commit 397a632

Browse files
committed
fix: adding action to fetch token from unified shell
1 parent db5fcfb commit 397a632

File tree

5 files changed

+36
-51
lines changed

5 files changed

+36
-51
lines changed

web-src/assetDetails.html

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<title>Asset Detail</title>
77
<link rel="stylesheet" href="./assetDetails.css"> <!-- Adjust path as needed -->
88
<link rel="stylesheet" href="https://use.typekit.net/dil4fkj.css">
9+
<script src="./src/index.js" type="module"></script>
910
</head>
1011
<body>
1112
<h1 class="asset-details-title">Asset Details</h1>

web-src/assetsUsageReport.html

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<title>Assets Usage Report</title>
77
<link rel="stylesheet" href="assetsUsageReport.css">
88
<link rel="stylesheet" href="https://use.typekit.net/dil4fkj.css">
9+
<script src="./src/index.js" type="module"></script>
910
</head>
1011
<body>
1112
<div class="assets-usage-report">

web-src/src/assetDetails.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const defaultThumbnail = require('../no-image.png');
2+
import { getState } from './index.js';
3+
4+
let accessToken;
25
(async () => {
36
// eslint-disable-next-line no-undef
47
const queryParams = new URLSearchParams(window.location.search);
@@ -9,6 +12,14 @@ const defaultThumbnail = require('../no-image.png');
912
const jsonString = decodeURIComponent(encodedJsonString);
1013
data = JSON.parse(jsonString);
1114
}
15+
16+
try {
17+
let state = await getState();
18+
accessToken = state.imsToken;
19+
20+
} catch (error) {
21+
console.log(error);
22+
}
1223
init(data);
1324

1425
function init(data) {
@@ -114,7 +125,13 @@ const defaultThumbnail = require('../no-image.png');
114125
});
115126
pageDiv.appendChild(viewDetail);
116127
pagesSection.appendChild(pageDiv);
117-
fetch(`https://288650-edsassettracker-stage.adobeio-static.net/api/v1/web/EDS-Asset-Tracker1/fetchList?hlxUrl=${hlxUrl}`)
128+
fetch(`https://288650-edsassettracker-stage.adobeio-static.net/api/v1/web/EDS-Asset-Tracker1/fetchList?hlxUrl=${hlxUrl}`, {
129+
method: 'GET', // or 'POST' if you want to send data
130+
headers: {
131+
'Authorization': `Bearer ${accessToken}`, // Send the access token in the Authorization header
132+
'Content-Type': 'application/json' // Specify the content type as JSON
133+
}
134+
})
118135
.then(response => response.json())
119136
.then(data => {
120137
console.log(data.payload.pageDetails[page].tags);

web-src/src/assetsData.js

+8-47
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import { PublicClientApplication } from './msal-browser-2.14.2.js';
2+
import excApp, { init } from '@adobe/exc-app';
3+
4+
import { getState } from './index.js';
25

36
var defaultThumbnail = require('../no-image.png');
47
let accessToken;
5-
let connectAttempts = 0;
6-
(async () => {
78

8-
const sp = {
9-
clientApp: {
10-
auth: {
11-
clientId: '2b4aa217-ddcd-4fe0-b09c-5a472764f552',
12-
authority: 'https://login.microsoftonline.com/fa7b1b5a-7b34-4387-94ae-d2c178decee1',
13-
},
14-
},
15-
login: {
16-
redirectUri: '/spauth.html',
17-
},
18-
};
9+
(async () => {
1910

2011
// Create mask and spinner elements
2112
const mask = document.createElement('div');
@@ -34,43 +25,13 @@ let connectAttempts = 0;
3425
const pagePath = queryParams.get('pagePath');
3526
let data = {};
3627
async function connectAndFetchData() {
37-
const publicClientApplication = new PublicClientApplication(sp.clientApp);
38-
const accounts = publicClientApplication.getAllAccounts();
39-
40-
if (accounts.length === 0) {
41-
// User is not logged in, show the login popup
42-
await publicClientApplication.loginPopup(sp.login);
43-
44-
}
45-
46-
const account = publicClientApplication.getAllAccounts()[0];
47-
const accessTokenRequest = {
48-
scopes: ['files.readwrite', 'sites.readwrite.all'],
49-
account,
50-
};
5128

5229
try {
53-
const res = await publicClientApplication.acquireTokenSilent(accessTokenRequest);
54-
accessToken = res.accessToken;
30+
let state = await getState();
31+
accessToken = state.imsToken;
32+
5533
} catch (error) {
56-
// Acquire token silent failure, and send an interactive request
57-
if (error.name === 'InteractionRequiredAuthError') {
58-
try {
59-
const res = await publicClientApplication.acquireTokenPopup(accessTokenRequest);
60-
accessToken = res.accessToken;
61-
console.log(accessToken);
62-
} catch (err) {
63-
console.error(`Cannot connect to SharePoint: ${err.message}`);
64-
document.body.removeChild(mask);
65-
document.querySelector('.assets-usage-report').style.display = 'block';
66-
return; // Exit if token acquisition fails
67-
}
68-
} else {
69-
console.error('Error acquiring token silently:', error.message);
70-
document.body.removeChild(mask);
71-
document.querySelector('.assets-usage-report').style.display = 'block';
72-
return;
73-
}
34+
console.log(error);
7435
}
7536

7637
// Proceed if we have a valid access token

web-src/src/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ window.onload = () => {
103103
// fallback mode, run the application without the Experience Cloud Runtime
104104
}
105105

106-
console.log("I am in console");
107-
//showActionsList()
106+
showActionsList()
108107
document.getElementById('actionForm').onsubmit = (event) => {
109108
event.preventDefault()
110109
setTimeout(doSubmit, 1)
@@ -120,7 +119,6 @@ function initRuntime () {
120119
// ready event brings in authentication/user info
121120
runtime.on('ready', ({ imsOrg, imsToken, imsProfile, locale }) => {
122121
// tell the exc-runtime object we are done
123-
console.log(imsToken);
124122
runtime.done()
125123
state = { imsOrg, imsToken, imsProfile, locale }
126124
console.log('exc-app:ready')
@@ -205,3 +203,10 @@ async function invokeAction (action, _headers, _params) {
205203
const result = await actionWebInvoke(action[1], headers, params)
206204
return result
207205
}
206+
207+
export async function getState() {
208+
while (!state.imsToken) {
209+
await new Promise(resolve => setTimeout(resolve, 100));
210+
}
211+
return state;
212+
}

0 commit comments

Comments
 (0)