title: How to call StateHasChanged() / NotifyAuthenticationStateChanged from a class in Blazor WASM
body: <p>In my project I created a class to handle http POST. The main method in the class first check if a JWT Token is available in local storage, check exp date, decide if a new toekn is needed and if so use the refresh token and finally do the POST.
  I inject this class with <code>builder.Services.AddTransient&lt;IMyHttp, MyHttp&gt;();</code></p>
  
  <p>Now I would like to notify the UI (<code>StateHasChanged()</code> or <code>NotifyAuthenticationStateChanged</code>) in case the refresh token is not valid so to log out the user immediately.
  The point is that I do not know how to raise the event from my http class (while from a controller is just a matter of calling this.StateHasChanged()).</p>
  
  <p>As suggested here you are the (pseudo)code:</p>
  
  <p>Index controller call the WebAPI to check weather:</p>
  
  <pre><code>(HttpResponseMessage, IEnumerable&lt;WeatherForecast&gt;) result = await MyHttp.PostPFAsync&lt;IEnumerable&lt;WeatherForecast&gt;&gt;("WeatherForecast/GetWeather", null);
  </code></pre>
  
  <p>This is <code>MyHttp.PostPFAsync</code> injected with <code>builder.Services.AddTransient&lt;IMyHttp, MyHttp&gt;();</code> in Program.cs</p>
  
  <pre><code>public async Task&lt;(HttpResponseMessage, T)&gt; PostPFAsync&lt;T&gt;(string requestUri, object data)
  {
      // I get my JWT Token from localstorage, set up auth headers, create StreamContent content serializing data and then
      HttpResponseMessage response = await _http.PostAsync(requestUri, content);
      if (response.IsSuccessStatusCode)
      {
          return (response, "Happiness");
      }
      else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
      {
          // Here I clear tokens from localstorage and I'd also like to logout user and update UI
      }
      else return (response, default(T));
  }
  </code></pre>
  
  <p>Obviosuly I could logout and update the UI in the Index controller but this would mean to make the check everywhere I call the WebAPI via MyHttp.PostPFAsync while I would like to centralize it as soon as I get a 401 (actually I'll do this if I fail to use the refresh token but keep things simple in this example).</p>