Skip to content

Commit 57625e2

Browse files
author
Vadim Belov
committed
Refactor: Centralize refresh token cookie name
Replaced hardcoded "refresh_token" strings with a constant `CookieRefreshTokenName` in `BaseAuthController` to improve maintainability and consistency. Updated all references to use the new constant, including cookie retrieval and appending operations in the `RefreshToken` method and various login flows. This change simplifies future updates to the cookie name and reduces the risk of inconsistencies.
1 parent 9ec1c5e commit 57625e2

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public abstract class BaseAuthController(
2424
IPasswordHashService _passwordHasher,
2525
ITokenProvider _tokenProvider) : ControllerBase
2626
{
27+
private const string CookieRefreshTokenName = "ee_refresh_token";
28+
2729
/// <summary>
2830
/// Gets the IP address from which the current request originated.
2931
/// </summary>
@@ -76,7 +78,7 @@ public async Task<IActionResult> RefreshToken([FromBody] RefreshTokenRequestDto?
7678
bool useCookie = string.IsNullOrWhiteSpace(request?.RefreshToken);
7779
if (useCookie)
7880
{
79-
if (Request.Cookies.TryGetValue("refresh_token", out string? cookieRefreshToken))
81+
if (Request.Cookies.TryGetValue(CookieRefreshTokenName, out string? cookieRefreshToken))
8082
{
8183
request = request is not null
8284
? request with { RefreshToken = cookieRefreshToken }
@@ -99,7 +101,7 @@ public async Task<IActionResult> RefreshToken([FromBody] RefreshTokenRequestDto?
99101
var roles = await GetUserRolesAsync(userId.Value);
100102
string accessToken = CreateAccessToken(userId.Value, roles);
101103
await SaveAndRevokeRefreshTokenAsync(userId.Value, request.RefreshToken, newRefreshToken, AuthType.Unknown);
102-
Response.Cookies.Append("refresh_token", newRefreshToken, new()
104+
Response.Cookies.Append(CookieRefreshTokenName, newRefreshToken, new()
103105
{
104106
Secure = true,
105107
HttpOnly = true,
@@ -155,7 +157,7 @@ public async Task<IActionResult> Login([FromBody] LoginRequestDto request)
155157
string refreshToken = StringHelpers.CreateRandomString(64);
156158
await SaveAndRevokeRefreshTokenAsync(userId.Value, string.Empty, refreshToken, AuthType.Credentials);
157159
await OnUserLoggingInAsync(userId.Value, AuthType.Credentials, AuthRejectionType.None);
158-
Response.Cookies.Append("refresh_token", refreshToken, new()
160+
Response.Cookies.Append(CookieRefreshTokenName, refreshToken, new()
159161
{
160162
Secure = true,
161163
HttpOnly = true,
@@ -211,7 +213,7 @@ public async Task<IActionResult> LoginWithGoogle([FromQuery] string token)
211213
string refreshToken = StringHelpers.CreateRandomString(64);
212214
await SaveAndRevokeRefreshTokenAsync(userId.Value, string.Empty, refreshToken, AuthType.Google);
213215
await OnUserLoggingInAsync(userId.Value, AuthType.Google, AuthRejectionType.None);
214-
Response.Cookies.Append("refresh_token", refreshToken, new()
216+
Response.Cookies.Append(CookieRefreshTokenName, refreshToken, new()
215217
{
216218
Secure = true,
217219
HttpOnly = true,

0 commit comments

Comments
 (0)