Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/Api/Controllers/AuthController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,19 +48,16 @@ public async Task<ActionResult<Result<LoginResult>>> Login([FromBody] LoginModel
return Ok(Result<LoginResult>.Succeed(loginResult));
}

[Authorize]
[HttpPost]
public async Task<IActionResult> 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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Common/Interfaces/IIdentityService.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,5 +8,5 @@ public interface IIdentityService
Task<bool> Validate(string token, string refreshToken);
Task<AuthenticationResult> RefreshTokenAsync(string token, string refreshToken);
Task<(AuthenticationResult AuthResult, UserDto UserCredentials)> LoginAsync(string email, string password);
Task<bool> LogoutAsync(string token, string refreshToken);
Task LogoutAsync(string token, string refreshToken);
}
14 changes: 4 additions & 10 deletions src/Infrastructure/Identity/IdentityService.cs
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Data;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Authentication;
Expand DownExpand Up@@ -185,15 +186,10 @@ public async Task<AuthenticationResult> 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)
Expand All@@ -213,7 +209,7 @@ public async Task<AuthenticationResult> RefreshTokenAsync(string token, string r
return (await GenerateAuthenticationResultForUserAsync(user), _mapper.Map<UserDto>(user));
}

public async Task<bool> LogoutAsync(string token, string refreshToken)
public async Task LogoutAsync(string token, string refreshToken)
{
var validatedToken = GetPrincipalFromToken(token);

Expand All@@ -226,7 +222,7 @@ public async Task<bool> 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))
{
Expand All@@ -235,8 +231,6 @@ public async Task<bool> LogoutAsync(string token, string refreshToken)

_context.RefreshTokens.Remove(storedRefreshToken);
await _context.SaveChangesAsync();

return true;
}

private async Task<AuthenticationResult> GenerateAuthenticationResultForUserAsync(User user)
Expand Down