Skip to content

Commit 7a2d89f

Browse files
Move extension method
Move extension method to get the user Id out of the API module and into the authentication module.
1 parent 9c0a32d commit 7a2d89f

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/TodoApp/ApiModule.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Martin Costello, 2021. All rights reserved.
22
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
33

4-
using System.Security.Claims;
54
using Microsoft.AspNetCore.Builder;
65
using Microsoft.AspNetCore.Http;
76
using Microsoft.AspNetCore.Routing;
@@ -100,9 +99,6 @@ public static IEndpointRouteBuilder MapApiRoutes(this IEndpointRouteBuilder buil
10099
private static string GetItemId(this HttpContext context)
101100
=> (string)context.GetRouteValue("id")!;
102101

103-
private static string GetUserId(this HttpContext context)
104-
=> context.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
105-
106102
private static ITodoService GetTodoService(this HttpContext context)
107103
=> context.RequestServices.GetRequiredService<ITodoService>();
108104
}

src/TodoApp/AuthenticationModule.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public static IServiceCollection AddGitHubAuthentication(this IServiceCollection
6060
public static string GetProfileUrl(this ClaimsPrincipal user)
6161
=> user.FindFirst(GitHubProfileClaim)!.Value;
6262

63+
public static string GetUserId(this HttpContext context)
64+
=> context.User.GetUserId();
65+
66+
public static string GetUserId(this ClaimsPrincipal user)
67+
=> user.FindFirst(ClaimTypes.NameIdentifier)!.Value;
68+
6369
public static string GetUserName(this ClaimsPrincipal user)
6470
=> user.FindFirst(GitHubAuthenticationConstants.Claims.Name)!.Value;
6571

0 commit comments

Comments
 (0)