Skip to content

Commit 59943bc

Browse files
Hans Jakob HålandHans Jakob Håland
authored andcommitted
Refactors Login to return 401 unauthorized with message
1 parent b83fe95 commit 59943bc

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ private static IResult Register(RegisterRequestDTO request, IRepository<User> se
9595

9696
[ProducesResponseType(StatusCodes.Status200OK)]
9797
[ProducesResponseType(StatusCodes.Status400BadRequest)]
98+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
9899
private static IResult Login(LoginRequestDTO request, IRepository<User> service, IConfigurationSettings config, IMapper mapper)
99100
{
100101
//if (string.IsNullOrEmpty(request.username)) request.username = request.email;
@@ -117,8 +118,11 @@ private static IResult Login(LoginRequestDTO request, IRepository<User> service,
117118

118119
if (!BCrypt.Net.BCrypt.Verify(request.password, user.PasswordHash))
119120
{
120-
// should probably be 401 unauthorized
121-
return Results.BadRequest(new ResponseDTO<string>() { Message = "Invalid email and/or password provided" });
121+
// TypedResults.Unauthorized did not support Message. "Custom" solution which includes message.
122+
return Results.Json(new ResponseDTO<string>
123+
{
124+
Message = "Invalid email and/or password provided"
125+
}, statusCode: StatusCodes.Status401Unauthorized);
122126
}
123127

124128
string token = CreateToken(user, config);

0 commit comments

Comments
 (0)