Skip to content

Commit 908a0dc

Browse files
authored
Merge pull request #28 from KYJKY/master
Feat: 로그아웃 구현 (FCM 토큰, 리프레시 토큰 초기화)
2 parents 662dda6 + 924f00e commit 908a0dc

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

EveryPinApi.Presentation/Controllers/AuthController.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,14 @@ public async Task<IActionResult> Refresh([FromBody] TokenDto tokenDto)
136136
}
137137

138138
[HttpPost("logout")]
139-
public async Task<IActionResult> Logout()
139+
public async Task<IActionResult> Logout([FromBody] TokenDto tokenDto)
140140
{
141-
return StatusCode(501);
141+
var result = await _service.AuthenticationService.Logout(tokenDto);
142+
143+
if (result.Succeeded)
144+
return Ok();
145+
else
146+
return result.Errors.Any(e => e.Code == "InvalidToken") ? Unauthorized() : BadRequest();
142147
}
143148

144149
[HttpDelete("user")]

Service.Contracts/Models/IAuthenticationService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ public interface IAuthenticationService
1616
Task<TokenDto> CreateToken(bool populateExp);
1717
Task<TokenDto> CreateTokenWithUpdateFcmToken(string fcmToken, bool populateExp);
1818
Task<TokenDto> RefreshToken(TokenDto tokenDto);
19+
Task<IdentityResult> Logout(TokenDto tokenDto);
1920
}

Service/Models/AuthenticationService.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,18 @@ public async Task<TokenDto> RefreshToken(TokenDto tokenDto)
200200

201201
return await CreateToken(populateExp: false);
202202
}
203+
204+
public async Task<IdentityResult> Logout(TokenDto tokenDto)
205+
{
206+
var principal = GetPrincipalFromExpiredToken(tokenDto.AccessToken);
207+
var userEmail = principal.FindFirst(ClaimTypes.Email);
208+
var user = await _userManager.FindByEmailAsync(userEmail?.Value);
209+
210+
user.RefreshToken = null;
211+
user.FcmToken = null;
212+
213+
var result = await _userManager.UpdateAsync(_user);
214+
215+
return result;
216+
}
203217
}

0 commit comments

Comments
 (0)