1
1
using Microsoft . AspNetCore . Mvc ;
2
+ using Microsoft . AspNetCore . Authorization ;
2
3
using OsmoDoc . API . Models ;
3
4
using OsmoDoc . API . Helpers ;
4
- using Microsoft . AspNetCore . Authorization ;
5
+ using OsmoDoc . Services ;
5
6
6
7
namespace OsmoDoc . API . Controllers ;
7
8
8
9
[ Route ( "api" ) ]
9
10
[ ApiController ]
10
11
public class LoginController : ControllerBase
11
12
{
13
+ private readonly IRedisTokenStoreService _tokenStoreSerivce ;
12
14
private readonly ILogger < LoginController > _logger ;
13
15
14
- public LoginController ( ILogger < LoginController > logger )
16
+ public LoginController ( IRedisTokenStoreService tokenStoreService , ILogger < LoginController > logger )
15
17
{
18
+ this . _tokenStoreSerivce = tokenStoreService ;
16
19
this . _logger = logger ;
17
20
}
18
21
19
22
[ HttpPost ]
20
23
[ Route ( "login" ) ]
21
24
[ AllowAnonymous ]
22
- public ActionResult < BaseResponse > Login ( [ FromBody ] LoginRequestDTO loginRequest )
25
+ public async Task < ActionResult < BaseResponse > > Login ( [ FromBody ] LoginRequestDTO loginRequest )
23
26
{
24
27
BaseResponse response = new BaseResponse ( ResponseStatus . Fail ) ;
25
28
try
26
29
{
27
30
string token = AuthenticationHelper . JwtTokenGenerator ( loginRequest . Email ) ;
31
+ await this . _tokenStoreSerivce . StoreTokenAsync ( token , loginRequest . Email ) ;
28
32
29
33
response . Status = ResponseStatus . Success ;
30
34
response . AuthToken = token ;
@@ -40,4 +44,27 @@ public ActionResult<BaseResponse> Login([FromBody] LoginRequestDTO loginRequest)
40
44
return this . StatusCode ( StatusCodes . Status500InternalServerError , response ) ;
41
45
}
42
46
}
47
+
48
+ [ HttpPost ]
49
+ [ Route ( "revoke" ) ]
50
+ public async Task < ActionResult < BaseResponse > > RevokeToken ( [ FromBody ] RevokeTokenRequestDTO request )
51
+ {
52
+ BaseResponse response = new BaseResponse ( ResponseStatus . Fail ) ;
53
+ try
54
+ {
55
+ await this . _tokenStoreSerivce . RevokeTokenAsync ( request . Token ) ;
56
+
57
+ response . Status = ResponseStatus . Success ;
58
+ response . Message = "Token revoked" ;
59
+ return this . Ok ( response ) ;
60
+ }
61
+ catch ( Exception ex )
62
+ {
63
+ response . Status = ResponseStatus . Error ;
64
+ response . Message = ex . Message ;
65
+ this . _logger . LogError ( ex . Message ) ;
66
+ this . _logger . LogError ( ex . StackTrace ) ;
67
+ return this . StatusCode ( StatusCodes . Status500InternalServerError , response ) ;
68
+ }
69
+ }
43
70
}
0 commit comments