question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useIsAuthenticated hook returning true when token is expired

See original GitHub issue

Core 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

  1. Wait for the token to expire
  2. 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:closed
  • Created 9 months ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
hectormmgcommented, Dec 7, 2022

@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 or MsalAuthenticationTemplate instead. Thanks!

0reactions
kvarghacommented, Dec 21, 2022

Thank you @tnorling! This is exactly what I needed. I’ll be closing the issue now.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found