useIsAuthenticated hook returning true when token is expired
See original GitHub issueCore Library
MSAL.js v2 (@azure/msal-browser)
Core Library Version
2.32.0
Wrapper Library
MSAL React (@azure/msal-react)
Wrapper Library Version
1.5.0
Public or Confidential Client?
Public
Description
I’m using the useIsAuthenticated() hook to render specific components when a user is signed in with a valid session. When the token expires, useIsAuthenticated() returns true.
Error Message
InteractionRequiredAuthError: interaction_required: AADSTS70044: The session has expired or is invalid due to sign-in frequency checks by conditional access. Trace ID: c6c8ef47-fefa-4c22-a062-0c711089dc00 Correlation ID: b72a73fe-3359-44ca-b770-8eaab110d757
Msal Logs
No response
MSAL Configuration
{
auth: {
clientId: '',
authority: '',
redirectUri: window.location.origin
},
cache: {
cacheLocation: 'localStorage',
storeAuthStateInCookie: false
}
}
Relevant Code Snippets
import { useIsAuthenticated } from '@azure/msal-react'
const IsAuthenticated = useIsAuthenticated()
export default function MyComponent () {
return (
IsAuthenticated === true
? Valid Session
: Invalid Session
)
}
Reproduction Steps
- Wait for the token to expire
- Log the return value of useIsAuthenticated()
Expected Behavior
useIsAuthenticated() should be returning false when a session is invalid.
Identity Provider
Azure AD / MSA
Browsers Affected (Select all that apply)
Chrome
Regression
No response
Source
External (Customer)
Issue Analytics
- State:
- Created 9 months ago
- Comments:12 (1 by maintainers)
Top Results From Across the Web
React MSAL - Silent SSO could not be completed, when token ...
I was able to make the authentication working and I am able to acquire token silently, however I am facing issue when original...
Read more >Tutorial: Create a React single-page app that uses auth code ...
Token acquisition and renewal are handled by the MSAL for React (MSAL ... const isAuthenticated = useIsAuthenticated(); return ( <> <Navbar ...
Read more >Next.js Authentication - JWT Refresh Token Rotation with ...
If the call happens after the accessToken has expired, we still have a chance to refresh it, as long as refreshToken is still...
Read more >Handle JWT Token expiration in React with Hooks - BezKoder
How to check when JWT Token is expired. There are two ways to check if Token is expired or not. 1. get expiry...
Read more >Authenticating React Apps With Auth0 - Smashing Magazine
When the access token expires, the app will be forced to make the user ... the claims are returned in an ID Token...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kvargha thanks for raising the issue.
The
useIsAuthenticated
hook is designed to return true when there is a user account in the cache, meaning the user is “Authenticated” in your application. It doesn’t reflect the state of validity of any of the related tokens, which determine whether or not the user is “Authorized”.If you want to determine if the user is authorized, please use the
useMsalAuthentication
hook orMsalAuthenticationTemplate
instead. Thanks!Thank you @tnorling! This is exactly what I needed. I’ll be closing the issue now.