You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using the loginRedirect. I'm in a Nextjs/React app.
I'm using redirectStartPage to hint at where I'd like the user to be redirected to once they have logged in, but I want to be able to override that destination on arrival depending on whether they're a new user or not. Because of that, I'm configuring navigateToLoginRequestUrl: false.
I can find out whether they're a new user during the LOGIN_SUCCESS event: I have a property event.payload.account.idTokenClaims.newUser which is true if they're a new user, and in that case I know where I want to take them.
But in other cases I want to use the value of redirectStartPage. The only way I've found to retrieve that value (short of storing it somewhere myself and retrieving it again) is to use MSAL internals. Currently I'm listening for HANDLE_REDIRECT_START, and in that handler:
Get the BrowserCacheManager from instance.getTokenCache().storage -- I have to tell TS to ignore this line because this is a private property. I haven't found another way to access it.
Get the current interaction ID via storage.getInteractionInProgress()
Use that value to put together the address of another value in storage, and retrieve storage.getItem(`msal.${interactionId}.request.params`)
Base64-decode that
JSON-decode that
Find the value I'm looking for in the redirectStartPage property of that
If I do this later, like during the LOGIN_SUCCESS or HANDLE_REDIRECT_END event, the value I want has already been cleared out of storage, and I haven't found a way to get it.
So I'm doing the above during the redirect start event, then capturing whether it's a new user or not during the login success event, and I'm awkwardly passing this data down into a component so that I can redirect based on it, because with Nextjs's app router I can't do a SPA-style redirection unless I'm in a component and so can use its hook. On the other hand, if I try to do all the above logic in a useEffect of a component, there's a race condition -- it often runs too late, and so I don't reliably receive the HANDLE_REDIRECT_START event, meaning I don't get the redirectStartPage out.
What I have is working, but it's very unpleasant. There must be a better way. Have I gone in totally the wrong direction? Did I miss something vital in the docs? How can I improve this?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm using the
loginRedirect
. I'm in a Nextjs/React app.I'm using
redirectStartPage
to hint at where I'd like the user to be redirected to once they have logged in, but I want to be able to override that destination on arrival depending on whether they're a new user or not. Because of that, I'm configuringnavigateToLoginRequestUrl: false
.I can find out whether they're a new user during the
LOGIN_SUCCESS
event: I have a propertyevent.payload.account.idTokenClaims.newUser
which is true if they're a new user, and in that case I know where I want to take them.But in other cases I want to use the value of
redirectStartPage
. The only way I've found to retrieve that value (short of storing it somewhere myself and retrieving it again) is to use MSAL internals. Currently I'm listening forHANDLE_REDIRECT_START
, and in that handler:BrowserCacheManager
frominstance.getTokenCache().storage
-- I have to tell TS to ignore this line because this is a private property. I haven't found another way to access it.storage.getInteractionInProgress()
storage.getItem(`msal.${interactionId}.request.params`)
redirectStartPage
property of thatIf I do this later, like during the
LOGIN_SUCCESS
orHANDLE_REDIRECT_END
event, the value I want has already been cleared out of storage, and I haven't found a way to get it.So I'm doing the above during the redirect start event, then capturing whether it's a new user or not during the login success event, and I'm awkwardly passing this data down into a component so that I can redirect based on it, because with Nextjs's app router I can't do a SPA-style redirection unless I'm in a component and so can use its hook. On the other hand, if I try to do all the above logic in a
useEffect
of a component, there's a race condition -- it often runs too late, and so I don't reliably receive theHANDLE_REDIRECT_START
event, meaning I don't get theredirectStartPage
out.What I have is working, but it's very unpleasant. There must be a better way. Have I gone in totally the wrong direction? Did I miss something vital in the docs? How can I improve this?
Beta Was this translation helpful? Give feedback.
All reactions