Unable to get users in Cache session in Custom Authenticator

Hi,

I am trying to implement a custom authenticator for username/password flow, the goal is to call my own service for authentication and user management. Following the documentation here: Server Developer Guide, I was able to successfully trigger the overriden authenticate function in my CustomAuthenticator where I added the user in the context.session as well.

When I try to get user from the session it returns null. Exploring it a little in the debugger I found out that the user is missing from the cache session.

This is the line of code that runs:

    public UserModel getAuthenticatedUser() {
        return entity.getAuthUserId() == null ? null : session.users().getUserById(getRealm(), entity.getAuthUserId());    }

Here session.users().getUserById(getRealm(), entity.getAuthUserId()) returns null. I am unable to figure out how to set this user. Can I get some help here?

Thanks.