Skip to content

Commit

Permalink
Merge pull request #28 from KYJKY/master
Browse files Browse the repository at this point in the history
Feat: 로그아웃 구현 (FCM 토큰, 리프레시 토큰 초기화)
  • Loading branch information
KYJKY authored Feb 12, 2025
2 parents 662dda6 + 924f00e commit 908a0dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions EveryPinApi.Presentation/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,14 @@ public async Task<IActionResult> Refresh([FromBody] TokenDto tokenDto)
}

[HttpPost("logout")]
public async Task<IActionResult> Logout()
public async Task<IActionResult> Logout([FromBody] TokenDto tokenDto)
{
return StatusCode(501);
var result = await _service.AuthenticationService.Logout(tokenDto);

if (result.Succeeded)
return Ok();
else
return result.Errors.Any(e => e.Code == "InvalidToken") ? Unauthorized() : BadRequest();
}

[HttpDelete("user")]
Expand Down
1 change: 1 addition & 0 deletions Service.Contracts/Models/IAuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface IAuthenticationService
Task<TokenDto> CreateToken(bool populateExp);
Task<TokenDto> CreateTokenWithUpdateFcmToken(string fcmToken, bool populateExp);
Task<TokenDto> RefreshToken(TokenDto tokenDto);
Task<IdentityResult> Logout(TokenDto tokenDto);
}
14 changes: 14 additions & 0 deletions Service/Models/AuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,18 @@ public async Task<TokenDto> RefreshToken(TokenDto tokenDto)

return await CreateToken(populateExp: false);
}

public async Task<IdentityResult> Logout(TokenDto tokenDto)
{
var principal = GetPrincipalFromExpiredToken(tokenDto.AccessToken);
var userEmail = principal.FindFirst(ClaimTypes.Email);
var user = await _userManager.FindByEmailAsync(userEmail?.Value);

user.RefreshToken = null;
user.FcmToken = null;

var result = await _userManager.UpdateAsync(_user);

return result;
}
}

0 comments on commit 908a0dc

Please sign in to comment.