Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Authentication to use local storage and easy auth refresh #1117

Merged

Conversation

mattgotteiner
Copy link
Collaborator

Purpose

  • Ensure tokens are refreshed when using easy auth so the full refresh window is used before requiring login again
  • Store tokens in local storage so sessions are preserved across tabs

Does this introduce a breaking change?

[ ] Yes
[X] No

Pull Request Type

What kind of change does this Pull Request introduce?

[ ] Bugfix
[X] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:

What to Check

  • Get the code
  • Validate .auth/refresh is called when logging into the app using the network tab on developer settings
  • Validate auth_setup shows localStorage as the msal cache location

@mattgotteiner
Copy link
Collaborator Author

#1051

@pamelafox
Copy link
Collaborator

I think some snapshot tests need updating to reflect change to localStorage?

@@ -88,18 +89,24 @@ export const getRedirectUri = () => {
// Get an access token if a user logged in using app services authentication
// Returns null if the app doesn't support app services authentication
const getAppServicesToken = (): Promise<AppServicesToken | null> => {
return fetch(appServicesAuthTokenUrl).then(r => {
return fetch(appServicesAuthTokenRefreshUrl).then(r => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is constantly refreshing? Is that the general recommendation? (Versus refreshing only based on expiration or some such?) Might be a naive question

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be a solid recommendation on how often to do this

https://learn.microsoft.com/en-us/azure/app-service/configure-authentication-oauth-tokens#refresh-auth-tokens

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, CC @jamesc in case he has any thoughts, but this seems fine to ship.

@mattgotteiner
Copy link
Collaborator Author

mattgotteiner commented Jan 5, 2024

I think some snapshot tests need updating to reflect change to localStorage?

How do you do this?

nvm, figured it out. it's not the snapshot tests it was authentication helper

@mattgotteiner mattgotteiner merged commit 0b8724a into Azure-Samples:main Jan 9, 2024
10 checks passed
@jamesc
Copy link

jamesc commented Jan 9, 2024 via email

@jamesc
Copy link

jamesc commented Jan 9, 2024 via email

@mirojs
Copy link

mirojs commented Feb 18, 2024

Hello @mattgotteiner @pamelafox

Upon refreshing the app page from the browser (Chrome), the developer tool reported error:
GET https://app-backend-***.azurewebsites.net/.auth/refresh 404 (Not Found)
rs @ authConfig.ts:92
(anonymous) @ authConfig.ts:120

Besides,
const authSetup = await fetchAuthSetup(); Line 61
export const appServicesToken = await getAppServicesToken(); Line 120
were labelled: 'await' function is only allowed within an async function

Could you help to check further? Thanks!

@matthewgotteiner
Copy link

Thanks for reporting for.

regarding .auth not found - this is expected if easy auth is not setup, we might need to add more checks for this to avoid refreshing.

regarding the typescript compile error what version of node are you using?

@mirojs
Copy link

mirojs commented Feb 19, 2024

@mattgotteiner Thanks for the quick response! My node version is v20.11.0

HughRunyan pushed a commit to RMI/RMI_chatbot that referenced this pull request Mar 26, 2024
…re-Samples#1117)

* switch to localstorage; add refresh

* run prettier

* fix tests

---------

Co-authored-by: Matt Gotteiner <magottei@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants