Skip to content

Commit 7f6a777

Browse files
committed
Logout api endpoint
1 parent 908baf6 commit 7f6a777

5 files changed

Lines changed: 50 additions & 0 deletions

File tree

Modules/Auth/Controllers/AuthController.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using cubets_core.Modules.Auth.DTOs;
33
using cubets_core.Modules.Auth.Models;
44
using cubets_core.Modules.Auth.Services;
5+
using CubetsCore.Modules.Auth.DTOs;
56
using Microsoft.AspNetCore.Mvc;
67
using Microsoft.EntityFrameworkCore;
78

@@ -39,5 +40,13 @@ public async Task<IActionResult> Guest()
3940
var result = await _authService.GuestLoginAsync();
4041
return Ok(_response.Success(result, "Login as Guest Successful!"));
4142
}
43+
44+
[HttpDelete("logout")]
45+
public async Task<IActionResult> Logout([FromBody] RevokedTokenRequestDto dto)
46+
{
47+
var result = await _authService.LogoutAsync(dto);
48+
return Ok(_response.Success(result, "Anda berhasil keluar!"));
49+
}
50+
4251
}
4352
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CubetsCore.Modules.Auth.DTOs
2+
{
3+
public class LogoutResponseDto
4+
{
5+
public bool Success { get; set; }
6+
public string? Message { get; set; }
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CubetsCore.Modules.Auth.DTOs
2+
{
3+
public class RevokedTokenRequestDto
4+
{
5+
public string? Token { get; }
6+
}
7+
}

Modules/Auth/Services/AuthService.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using cubets_core.Helpers;
22
using cubets_core.Modules.Auth.DTOs;
33
using cubets_core.Modules.Auth.Models;
4+
using CubetsCore.Modules.Auth.DTOs;
5+
using CubetsCore.Modules.Auth.Models;
46
using Microsoft.EntityFrameworkCore;
57
using System.Security.Cryptography;
68
using System.Text;
@@ -97,6 +99,27 @@ public async Task<AuthResponseDto> GuestLoginAsync()
9799
};
98100
}
99101

102+
public async Task<LogoutResponseDto> LogoutAsync(RevokedTokenRequestDto dto)
103+
{
104+
if (string.IsNullOrWhiteSpace(dto.Token))
105+
throw new Exception("Token diperlukan!");
106+
107+
_dbContext.RevokedTokens.Add(new RevokedToken
108+
{
109+
Token = dto.Token,
110+
RevokedAt = DateTime.UtcNow
111+
});
112+
113+
await _dbContext.SaveChangesAsync();
114+
115+
return new LogoutResponseDto
116+
{
117+
Success = true,
118+
Message = "Logout berhasil."
119+
};
120+
}
121+
122+
100123
private string HashPassword(string password)
101124
{
102125
using var sha = SHA256.Create();

Modules/Auth/Services/IAuthService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using cubets_core.Modules.Auth.DTOs;
2+
using CubetsCore.Modules.Auth.DTOs;
23

34
namespace cubets_core.Modules.Auth.Services
45
{
@@ -7,5 +8,7 @@ public interface IAuthService
78
Task<AuthResponseDto> RegisterAsync(RegisterRequestDto dto);
89
Task<AuthResponseDto> LoginAsync(LoginRequestDto dto);
910
Task<AuthResponseDto> GuestLoginAsync();
11+
12+
Task<LogoutResponseDto> LogoutAsync(RevokedTokenRequestDto dto);
1013
}
1114
}

0 commit comments

Comments
 (0)