Skip to content

Commit 9ec1c5e

Browse files
author
Vadim Belov
committed
Make RefreshToken method handle nullable requests
Updated the `RefreshToken` method to support nullable `request` parameters, ensuring robustness when the request object is not provided. Improved null safety by using the null-conditional operator (`?.`) for accessing `RefreshToken`. Added logic to create a new `RefreshTokenRequestDto` when `request` is null and a cookie is available. Enhanced validation to prevent potential `NullReferenceException` errors.
1 parent bbc1b93 commit 9ec1c5e

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

Sources/EasyExtensions.AspNetCore.Authorization/Controllers/BaseAuthController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,20 @@ public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequest
7171
/// <returns>An <see cref="IActionResult"/> containing the new access token if the refresh token is valid; otherwise, a
7272
/// 404 Not Found result if the token is invalid or revoked.</returns>
7373
[HttpPost("refresh")]
74-
public async Task<IActionResult> RefreshToken([FromBody] RefreshTokenRequestDto request)
74+
public async Task<IActionResult> RefreshToken([FromBody] RefreshTokenRequestDto? request)
7575
{
76-
bool useCookie = string.IsNullOrWhiteSpace(request.RefreshToken);
76+
bool useCookie = string.IsNullOrWhiteSpace(request?.RefreshToken);
7777
if (useCookie)
7878
{
7979
if (Request.Cookies.TryGetValue("refresh_token", out string? cookieRefreshToken))
8080
{
81-
request = request with { RefreshToken = cookieRefreshToken };
81+
request = request is not null
82+
? request with { RefreshToken = cookieRefreshToken }
83+
: new RefreshTokenRequestDto { RefreshToken = cookieRefreshToken };
8284
}
8385
}
8486

85-
if (string.IsNullOrWhiteSpace(request.RefreshToken))
87+
if (string.IsNullOrWhiteSpace(request?.RefreshToken))
8688
{
8789
return this.ApiNotFound("Refresh token was not found");
8890
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.ComponentModel.DataAnnotations;
2-
3-
namespace EasyExtensions.AspNetCore.Authorization.Models.Dto
1+
namespace EasyExtensions.AspNetCore.Authorization.Models.Dto
42
{
53
/// <summary>
64
/// Represents a request to refresh an authentication token using the current password.
@@ -10,7 +8,6 @@ public record RefreshTokenRequestDto
108
/// <summary>
119
/// Gets or sets the refresh token used to obtain a new access token when the current one expires.
1210
/// </summary>
13-
[Required(AllowEmptyStrings = true)]
1411
public string? RefreshToken { get; set; }
1512
}
1613
}

0 commit comments

Comments
 (0)