Typescript client get roles in AuthenticateResponse

Hi all,

I would like to get the user roles returned to an Angular 4 client using the ServiceStack Typescript client during authentication and can’t seem to find / put the roles in the right place to be able to see them… sure I must be missing something simple.

Using a CustomAuthSession as below:

 public class CustomAuthSession : AuthUserSession
{
    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        if (!session.Roles.Any())
        {
            throw new HttpError(HttpStatusCode.Forbidden, "User must have a role");
        }
        base.OnAuthenticated(authService, session, tokens, authInfo);            
        authService.SaveSession(session); 
    }
}

I’d like to be able to get the roles on the AuthenticationResponse:

login() {
    this.authenticationService.login(username, password).then( authResponse => { authResponse.Roles??? });
};

Any help appreciated,
Thanks.

The AuthenticateResponse DTO is a fixed class (like every other DTO), it doesn’t return your User Session, only the properties that are on the DTO. The only extension slot you can return extra info is in the Meta string dictionary, which you can add in a Response Filter by checking for the AuthenticateResponse Response DTO or if you’re using a custom AuthProvider you can implement IAuthResponseFilter where the callback gives you an opportunity to modify the returned AuthenticateResponse.

But I’d recommend just calling your own Custom Service that way you can return a DTO with the appropriate data structure instead of trying to slot it in the fixed AuthenticateResponse.

Thanks for the quick response mythz.

I’ll go down the Custom Service route as recommended.

1 Like