About Current User

Learn how to use current user in BlazorForKids

Accessing the Current User in BlazorForKids

BlazorForKids makes it easy to access information about the currently logged-in user across your entire application. This is made possible by the CurrentUser property, which is automatically available in all generated components, pages, and layouts.

This simplifies tasks like displaying the user’s name, restricting access based on roles, or filtering data based on who is logged in.

How It Works

BlazorForKids injects the current user as a cascading value using a simple component named <BkUserInfoCascadingValue>. This component wraps the entire application layout inside MainLayout.razor:


            <BkUserInfoCascadingValue>
                @Body
            </BkUserInfoCascadingValue>
    

Since all other layouts in the app inherit from or are nested inside MainLayout, the current user is available throughout the entire application — no extra setup required.

What Is CurrentUser?

The CurrentUser is an instance of BkUserInfo, a struct provided by the framework that contains basic information about the logged-in user:


            public struct BkUserInfo
            {
                public string? Id { get; private set; }                  // Optional mapped ID (e.g. EmployeeId)
                public string? ApplicationUserId { get; private set; }   // From Identity system
                public string? DisplayName { get; private set; }         // User’s display name
                public string? Email { get; private set; }               // User’s email
            }
    

This data is extracted from the user’s ClaimsPrincipal at login time and is automatically kept available in your app through dependency injection.

Accessing CurrentUser

You can access CurrentUser from any generated component, page, or layout because all of them inherit from BkComponentBase:

@code {
                [CascadingParameter(Name = "BkUserInfo")]
                public BkUserInfo CurrentUser { get; set; }
                private void ShowUserInfo()
                {
                    Console.WriteLine(CurrentUser.DisplayName);
                    Console.WriteLine(CurrentUser.Email);
                }
            }
    

✅ You can also use CurrentUser.Id in data filters or logic to ensure users only see or edit the data they are allowed to.

Mapping to Your Functional User (e.g., Employee)

BlazorForKids knows how to extract basic information from the identity system (e.g., username, email), but in many applications, you’ll want to connect the logged-in user to a related domain entity — like an Employee or a Customer.

To support this, the framework provides a special interface named IBkUserMapping. If you implement and register this interface, the framework will automatically use it to enrich the CurrentUser info.

Example: Mapping a Logged-In User to an Employee


                public class FakeUserMapping(IBkMediatorDomain mediatorDomain) : IBkUserMapping
                {
                    public async Task<(string? Id, string? DisplayName)> MapApplicationUserToFunctionalUser(string? id, string? userName)
                    {
                        if (id is null) return (null, null);

                        // Example logic: find the first Employee record linked to this user
                        var employee = await MediatorExtensions.FirstOrDefault<EmployeeModel, Employee>(mediatorDomain.EmployeeQuery);

                        // Return a functional ID (e.g., EmployeeId) and a friendly display name
                        return employee is null
                            ? (id, userName)
                            : (employee.Id.ToString(), $"{employee.FirstName} {employee.LastName}");
                    }
                }
    

In this example, you map the identity user (from the auth system) to your domain-level user (Employee), and return the correct Id and DisplayName to be stored in CurrentUser.

Registering Your Mapping

Once you’ve created your own IBkUserMapping implementation, register it in Program.cs like this:


            builder.Services.AddScoped<IBkUserMapping, FakeUserMapping>();
    

BlazorForKids will detect your implementation and use it to populate the CurrentUser struct at runtime.

Why Is This Useful?

Having a fully enriched CurrentUser makes it easy to:

  • Personalize the UI (e.g., display "Welcome John Doe")
  • Filter data automatically (e.g., show only records related to the user)
  • Restrict access to features based on role or functional ID

You can access CurrentUser in:

  • Pages (e.g., filter the data displayed)
  • Layouts (e.g., display user info in menus)
  • Components (e.g., hide or show buttons)

Summary

  • CurrentUser is automatically provided throughout your app via <BkUserInfoCascadingValue>.
  • It gives access to user identity and (optionally) domain-level IDs like EmployeeId.
  • You can implement IBkUserMapping to connect the identity system to your domain model.
  • The enriched BkUserInfo helps you build personalized and secure features.

🧠 Tip for Beginners: If you're not sure where to start, begin by accessing CurrentUser.DisplayName and showing it in a page or layout. Then, map your user to a domain model like Employee when needed.

🔒 Your app knows who’s logged in — and BlazorForKids makes it easy to use that info everywhere you need it.

An unhandled error has occurred. Reload 🗙