diff --git a/src/Api/Controllers/AuthController.cs b/src/Api/Controllers/AuthController.cs index da4bef58..b8d8a03c 100644 --- a/src/Api/Controllers/AuthController.cs +++ b/src/Api/Controllers/AuthController.cs @@ -48,19 +48,16 @@ public async Task>> Login([FromBody] LoginModel return Ok(Result.Succeed(loginResult)); } - [Authorize] [HttpPost] public async Task Logout() { var refreshToken = Request.Cookies[nameof(RefreshToken)]; var jweToken = Request.Cookies["JweToken"]; - - var loggedOut = await _identityService.LogoutAsync(jweToken!, refreshToken!); - - if (!loggedOut) return Ok(); RemoveJweToken(); RemoveRefreshToken(); + + await _identityService.LogoutAsync(jweToken!, refreshToken!); return Ok(); } diff --git a/src/Application/Common/Interfaces/IIdentityService.cs b/src/Application/Common/Interfaces/IIdentityService.cs index 20fab955..048300af 100644 --- a/src/Application/Common/Interfaces/IIdentityService.cs +++ b/src/Application/Common/Interfaces/IIdentityService.cs @@ -8,5 +8,5 @@ public interface IIdentityService Task Validate(string token, string refreshToken); Task RefreshTokenAsync(string token, string refreshToken); Task<(AuthenticationResult AuthResult, UserDto UserCredentials)> LoginAsync(string email, string password); - Task LogoutAsync(string token, string refreshToken); + Task LogoutAsync(string token, string refreshToken); } \ No newline at end of file diff --git a/src/Infrastructure/Identity/IdentityService.cs b/src/Infrastructure/Identity/IdentityService.cs index cb38b55c..eff63f43 100644 --- a/src/Infrastructure/Identity/IdentityService.cs +++ b/src/Infrastructure/Identity/IdentityService.cs @@ -1,3 +1,4 @@ +using System.Data; using System.Globalization; using System.IdentityModel.Tokens.Jwt; using System.Security.Authentication; @@ -185,15 +186,10 @@ public async Task RefreshTokenAsync(string token, string r return principal; } - catch (SecurityTokenExpiredException ex) + catch { return null; } - catch (Exception exception) - { - Console.WriteLine(exception.StackTrace); - return null; - } } public async Task<(AuthenticationResult, UserDto)> LoginAsync(string email, string password) @@ -213,7 +209,7 @@ public async Task RefreshTokenAsync(string token, string r return (await GenerateAuthenticationResultForUserAsync(user), _mapper.Map(user)); } - public async Task LogoutAsync(string token, string refreshToken) + public async Task LogoutAsync(string token, string refreshToken) { var validatedToken = GetPrincipalFromToken(token); @@ -226,7 +222,7 @@ public async Task LogoutAsync(string token, string refreshToken) var storedRefreshToken = await _context.RefreshTokens.SingleOrDefaultAsync(x => x.Token.Equals(Guid.Parse(refreshToken))); - if (storedRefreshToken is null) return true; + if (storedRefreshToken is null) return; if (!storedRefreshToken!.JwtId.Equals(jti)) { @@ -235,8 +231,6 @@ public async Task LogoutAsync(string token, string refreshToken) _context.RefreshTokens.Remove(storedRefreshToken); await _context.SaveChangesAsync(); - - return true; } private async Task GenerateAuthenticationResultForUserAsync(User user)