Select the UaaExtendedUser

I tried selecting the current user on the base page with:

const user = useSelector(UaaSelectors.user)

but that has returned

Uncaught Error: State contains an invalid uaa slice.

I ended up writing my own Selector

export const uaaExtendedUser: Selector<UaaExtendedOauth2User | undefined> = createSliceSelector(
    "uaa",
    isUaaSlice,
    selectUaaUser
);

However, this approach feels incorrect. Is there something I missed or did wrong?

hi @soeren-round-ridge,

I stumbled over similar issue last week. on my side it was caused by userinfo header that was rendered before uaa slice was created. so have a look into your basepage and check if you try to render the application frame (where userinfo header is passed in project template) before the authentication check is done.

EDIT1:
Sorry misread a bit. But root cause should be same even if you do the select on your own and not by userinfo header.
So check: “if you render that component before the authentication check is done.”
If so i guess you need to create some sub component that includes the selector and is rendered after the auth check

Thanks for your quick response! Unfortunately, you are somewhat right.

What I wanted to achieve was to pass the email into my generic components, which I render afterward. If I call that hook in the components (even though I don’t need to), it works, and I retrieve the user.

Doesn’t that feel like a bug to you? My selector is working correctly.

I guess your selector does not throw any error since isUaaSlice contains an or check inside that returns true for logged in and not logged in states.
In comparison the UAASelectors.user does only check for logged in and therefore fails when you are not authenticated at that moment.

See @com.mgmtp.a12.uaa/uaa-authentication-client/src/internal/selectors.ts for implementation.

You are absolutely correct, thats where I got the inspiration from! :), However I dont see any issues with my implementation - do you? If there is something wrong, I’d love to know about it.

Currently my log looks like this:

false // isAuthenticaated
undefined // user
true // isAuthenticaated
{"id_token":"eyJhbGciOiJS// user

Let me know if I’m mistaken, but thats what I would have expected in the first place :sweat_smile:
Therefore, it feels like an implementation that isn’t entirely failproof.

Hi @soeren-round-ridge,

thanks for clarification and the logged uaa slice informations.

thats where I got the inspiration from

We are always happy to inspirate the projects by our libs :grinning:

However I dont see any issues with my implementation - do you

No this should be fine as long as you are aware that the user could be not properly defined in your later code if not authenticated.

but thats what I would have expected in the first place

I do not know why the uaa implements throwing an error on that selector instead of returning an undefined for User selector.

it feels like an implementation that isn’t entirely failproof

But I guess this should ensure implementation of proper error handling on project side for cases the user is not available/authenticated and on the same side will not expect an undefined check on every user select call.
To conclude from that uaa expects an authentication check (UAASelectors.state) on the root level of the components branch that should be protected to work with the user selector in a none throwing way.

Further information
By Documentation the UAA user slice is described as user logged in (UaaUser | UaaOidcUser) (A12 Docs - UAA - UaaSelectors). There is nothing mentioned about internal handling of “error” cases in general and what developers can expect in such cases.
If you have the feeling this should be changed in documentation or even adapated in the implementation you are invited to file an issue in A12 Support Portal on getA12.com

Thanks for sharing :slight_smile: