Path: redb/redb.Templates/templates/redb-identity-app/RedbIdentityApp.Web/Services/ModuleClient.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-identity-app/RedbIdentityApp.Web/Services/ModuleClient.cs
----
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using Microsoft.AspNetCore.Components.Authorization;
using RedbIdentityApp.Contracts;
using RedbIdentityApp.Web.Auth;
namespace RedbIdentityApp.Web.Services;
///
/// The module's API, called with the signed-in user's access token. The HttpClient comes configured from
/// Program.cs (the module's address). What the module answers becomes something a page can show:
///
/// 401: the token is no longer good (revoked at Identity): the session ends,
/// ;
/// 403: the user lacks the right ();
/// 503: Identity is down ().
///
/// The user is taken from the circuit's authentication state, which the client follows through
/// from its creation until the circuit's scope
/// disposes it, so a call never goes out with a state that has changed since.
///
public sealed class ModuleClient : IDisposable
{
private readonly HttpClient http;
private readonly AuthenticationStateProvider authentication;
private readonly AccessTokens tokens;
private readonly UserTokenStore store;
private Task? state;
public ModuleClient(HttpClient http, AuthenticationStateProvider authentication, AccessTokens tokens, UserTokenStore store)
{
this.http = http;
this.authentication = authentication;
this.tokens = tokens;
this.store = store;
authentication.AuthenticationStateChanged += OnAuthenticationStateChanged;
}
public void Dispose() => authentication.AuthenticationStateChanged -= OnAuthenticationStateChanged;
private void OnAuthenticationStateChanged(Task changed) => state = changed;
public Task MeAsync() => SendAsync(HttpMethod.Get, "api/me", null);
public Task> NotesAsync() => SendAsync>(HttpMethod.Get, "api/notes", null);
public Task AddNoteAsync(NoteDto note) => SendAsync(HttpMethod.Post, "api/notes", note);
public Task DeleteNoteAsync(long id) => SendAsync