Multiple SPFx web parts using Graph API on the same page, is this possible?

Occasional Contributor

Scenario: You have two SPFx web parts that users can add on a page. One web part is fetching user’s calendar events, another one is fetching user’s emails. Both use Graph API and both need different rights. User authenticates the first one and browser gets a token with rights to read his/her calendar event. Then user tries to authenticate the second web part but according to what I have read from here this will not work because there already is a token for Graph API with rights only to read the calendar events.

Question: Is it possible to “flush” the token (with Storage.removeItem for example?) that is already there in the browser and get a new one with a broader scope for both reading the users calendar + reading his/her emails. Could this be easily done, so that every time we are adding a new web part that is using Graph API to a page , we could check if there already is a token associated with Graph API and if there is one with not enough rights for the new web part, we could flush that token and ask for a new one with more rights (the old rights + the new rights needed).

2 Replies

Going to reply to myself :D How about if the token handling etc. for the whole page (all the web parts) was done with a SharePoint Framework extension (Application customizer). So, the SharePoint Framework extension would handle all the authorization request and keep track of the Graph API rights needed for all of the web parts the users adds to the page? Could this be done? Is the SharePoint extension capable of doing this? 

Following this, I ultimately want to do the same thing. Right now I am doing this on a classic page with multiple script editor web parts, trying to learn SPFX currently.

I've got one function that gets the token, then once I have verified I have the token, I call the various functions that make the graph calls and populate the results in each SEWP. Havent figured out how to mimic this in SPFX.