Skip to content

Conversation

Arnab-Developer
Copy link
Contributor

I have added author name in the blog post.

Related to #253

@Arnab-Developer
Copy link
Contributor Author

@linkdotnet please review this PR.

Copy link
Owner

@linkdotnet linkdotnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will checkout your branch and play with it later to get some better grasp on your ideas.

Copy link
Owner

@linkdotnet linkdotnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still confident that we don't need any custom dialog or HttpContext in the first place to make that feature work.

Here I set the name on my IDP (Auth0):

Image

Which then I got via the claim and displayed in the blog:

Image

For that we don't need much things:

  1. We have to tell OIDC that we want profile information: options.Scope.Add("profile");
  2. I wrote a small service that gets the user's name:
public async ValueTask<string?> GetDisplayNameAsync()
{
    var user = (await authenticationStateProvider.GetAuthenticationStateAsync()).User;
    if (user?.Identity is not { IsAuthenticated: true })
    {
        return null;
    }

    // Check a few (not sure what order is the best and if covers all but worked when I spiked something together)
    var name = user.FindFirst("name")?.Value
               ?? user.FindFirst("preferred_username")?.Value
               ?? user.FindFirst("nickname")?.Value;

    return string.IsNullOrWhiteSpace(name) ? null : name;
}

The advantage here is that we don't need HttpContext at all and we solely rely on how Blazor handles things aka AuthenticationStateProvider. This service than can be used in all sorts of places - that needs a bit design.

In the screenshot you are seeing, I really just did this:

@inject ICurrentUserService CurrentUser

...

@code {
    protected override async Task OnInitializedAsync()
    {
        AuthStateProvider.AuthenticationStateChanged += OnAuthStateChanged;
        displayName = await CurrentUser.GetDisplayNameAsync();
    }

Of course we need to check for authentication changes and so on, but that is "fine-tuning".

Let me know your thoughts about this one. I am absolutely open to discuss any direction, I would just like that it is "the standard of doing things" so also in a year folks understand the code and why it is there.

public async Task ShouldAuthorNameNullWhenNotGiven()
{
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, tags: new[] { "Tag 1", "Tag 2" });
await DbContext.BlogPosts.AddAsync(blogPost, TestContext.Current.CancellationToken);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just from a style point of view: The other tests are using directly the Repository object rather than the DbContext.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test to validate if the AuthorName is not saved in the db, then at the time of loading with GetByIdAsync() of the Repository it should come as null.

First it adds a BlogPost entity in the db which don't have AuthorName in it with the DbContext. Then it pulls the data with GetByIdAsync() of the Repository and validate the AuthorName is null or not.

This same pattern has been followed in the ShouldLoadBlogPost test as well.

@Arnab-Developer
Copy link
Contributor Author

@linkdotnet thanks for the detail explanation. I have understood your approach now. I have made the code changes as per your suggestion.

Copy link
Owner

@linkdotnet linkdotnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good work - we are almost there!


var cut = Render<AccessControl>();

cut.FindAll("label:contains('Test Author')").ShouldHaveSingleItem();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the Test Author coming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done the setup in the constructor.

@linkdotnet
Copy link
Owner

Good job on this one - looking good

@Arnab-Developer
Copy link
Contributor Author

If you update the appsettings.json with proper idp info in your local and then run the app, is the AuthorName is coming properly?

"Authentication": {
	"Provider": "Auth0",
	"Domain": "",
	"ClientId": "",
	"ClientSecret": ""
},

Also you need to update the Program.cs like below.

- if (builder.Environment.IsDevelopment())
- {
-     builder.Services.UseDummyAuthentication();
- }
- else
- {
-     builder.Services.UseAuthentication();
- }
+ builder.Services.UseAuthentication();

@linkdotnet
Copy link
Owner

linkdotnet commented Sep 2, 2025

That works:
image

The only thing is probably I would drop the name from "Logout" - so for me no "Logout Steven Giesel". I understand that we should have the name somewhere.

I was also playing with the idea of:

  1. Having the Author name in the preview:
image
  1. And somewhere here:
image

EDIT: Of course that is if the multi author mode is enabled.

edit 2: But somewhere it might be useful to show the username in the UI.

@Arnab-Developer
Copy link
Contributor Author

@linkdotnet it is good news that the solution is working with identity provider's claim information. Let me know when you will find a good place to show the AuthorName. We will update the PR based on that.

@linkdotnet
Copy link
Owner

Sorry swamped with work - if you have other ideas rather than next to "Logout"/"Login" - I am open. On top of my head, we can keep the Lock icon and instead of "Logout" we put the Name in.

@Arnab-Developer
Copy link
Contributor Author

When multi user mode is enable, then we can show the author name at the place of logout. If someone clicks on the author name then a drop down will open and logout link will be inside that. Do you like this idea?

@linkdotnet
Copy link
Owner

When multi user mode is enable, then we can show the author name at the place of logout. If someone clicks on the author name then a drop down will open and logout link will be inside that. Do you like this idea?

Yeah - that sounds good. That in combination with the two places where the author should be visible and the feature is done

@Arnab-Developer
Copy link
Contributor Author

Do you also want to show the author name in the blog preview page?

@linkdotnet
Copy link
Owner

Do you also want to show the author name in the blog preview page?

Yes indeed - otherwise the feature would not have much impact at all.
image

And:
image

@linkdotnet
Copy link
Owner

If the flag is true

@Arnab-Developer
Copy link
Contributor Author

Ok, I will update the PR accordingly.

@Arnab-Developer
Copy link
Contributor Author

I have made the code changes. Please review.

@linkdotnet
Copy link
Owner

Really good. Sorry for the late reply, I am unfortunately a bit sick. I only want one last change to align to the current style:
image

Here an icon is missing. I would take the same as in the menuband:

<i class="user-tie"></i> @AuthorName

The same is true for the Blog Post Page itself.

@Arnab-Developer
Copy link
Contributor Author

Icon added. Please review when you feel better.

@linkdotnet
Copy link
Owner

LGTM - there are some super minor things with the alignment, but I will fix them on the master branch.

@linkdotnet linkdotnet force-pushed the 253-add-author-name-in-blog-post branch from 6c1d848 to dfa563b Compare September 9, 2025 18:40
@linkdotnet linkdotnet merged commit db23467 into linkdotnet:master Sep 9, 2025
@linkdotnet linkdotnet mentioned this pull request Sep 9, 2025
@samcov
Copy link

samcov commented Sep 9, 2025

Great job guys!!!

@Arnab-Developer Arnab-Developer deleted the 253-add-author-name-in-blog-post branch September 10, 2025 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants