/me endpoint
This commit is contained in:
parent
30cb58e3f7
commit
9bbd2985c5
@ -18,6 +18,7 @@ public static class UserAccess
|
|||||||
MapPostRegister(routes);
|
MapPostRegister(routes);
|
||||||
MapPostLogout(routes);
|
MapPostLogout(routes);
|
||||||
|
|
||||||
|
MapGetMe(routes);
|
||||||
|
|
||||||
MapGetUserById(routes);
|
MapGetUserById(routes);
|
||||||
}
|
}
|
||||||
@ -118,6 +119,39 @@ public static class UserAccess
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static void MapGetMe(IEndpointRouteBuilder routes)
|
||||||
|
{
|
||||||
|
routes.MapGet("/me", async (HttpContext context, ClaimsPrincipal user, AppDbContext db) =>
|
||||||
|
{
|
||||||
|
var id = user.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
|
if (id is null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 401;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var u = await db.Users
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(x => x.Id == id)
|
||||||
|
.Select(x => new PublicUserDto(
|
||||||
|
x.Id,
|
||||||
|
x.Email,
|
||||||
|
x.Name,
|
||||||
|
x.ProfilePicture
|
||||||
|
))
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
await context.Response.WriteAsync(JsonSerializer.Serialize(u));
|
||||||
|
|
||||||
|
}).RequireAuthorization();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static void MapGetUserById(IEndpointRouteBuilder routes)
|
private static void MapGetUserById(IEndpointRouteBuilder routes)
|
||||||
{
|
{
|
||||||
routes.MapGet("/user/{id}", async context =>
|
routes.MapGet("/user/{id}", async context =>
|
||||||
@ -130,3 +164,10 @@ public static class UserAccess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed record PublicUserDto(
|
||||||
|
string Id,
|
||||||
|
string Name,
|
||||||
|
string Description,
|
||||||
|
|
||||||
|
string ProfilePicture
|
||||||
|
);
|
||||||
18
Program.cs
18
Program.cs
@ -49,6 +49,10 @@ builder.Services.AddAuthorization();
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
||||||
|
await DatabaseHelper.InitDatabaseAsync(app);
|
||||||
|
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
@ -73,4 +77,16 @@ app.Run();
|
|||||||
|
|
||||||
|
|
||||||
public sealed record RegisterDto(string Email, string Password);
|
public sealed record RegisterDto(string Email, string Password);
|
||||||
public sealed record LoginDto(string Email, string Password);
|
public sealed record LoginDto(string Email, string Password);
|
||||||
|
|
||||||
|
|
||||||
|
static class DatabaseHelper
|
||||||
|
{
|
||||||
|
public static async Task InitDatabaseAsync(WebApplication app)
|
||||||
|
{
|
||||||
|
using var scope = app.Services.CreateScope();
|
||||||
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||||
|
|
||||||
|
await db.Database.EnsureCreatedAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,4 +9,4 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user