22using Inventory_Backend_NET . Database ;
33using Inventory_Backend_NET . Fitur . _Logic . Services ;
44using Inventory_Backend_NET . Fitur . Autentikasi . _Dto . Response ;
5+ using JetBrains . Annotations ;
56using Microsoft . AspNetCore . Authorization ;
67using Microsoft . AspNetCore . Mvc ;
8+ using Swashbuckle . AspNetCore . Annotations ;
9+ using Swashbuckle . AspNetCore . Filters ;
710
811namespace Inventory_Backend_NET . Fitur . Autentikasi . Login
912{
@@ -22,7 +25,11 @@ IJwtTokenService jwtTokenService
2225
2326 [ HttpPost ]
2427 [ AllowAnonymous ]
25- public IActionResult Login ( [ FromBody ] LoginBodyDto user )
28+ [ SwaggerResponse ( StatusCodes . Status200OK , "Login Sukses" , typeof ( LoginSucceedResponse ) ) ]
29+ [ SwaggerResponseExample ( StatusCodes . Status200OK , typeof ( LoginSucceedResponseExample ) ) ]
30+ [ SwaggerResponse ( StatusCodes . Status400BadRequest , "Login gagal" , typeof ( LoginFailedResponse ) ) ]
31+ [ SwaggerResponseExample ( StatusCodes . Status400BadRequest , typeof ( LoginFailedResponseExample ) ) ]
32+ public IActionResult Login ( [ FromBody ] LoginRequest user )
2633 {
2734 var currentUser = _db . Users . FirstOrDefault (
2835 predicate : aUser =>
@@ -32,28 +39,56 @@ public IActionResult Login([FromBody] LoginBodyDto user)
3239
3340 if ( currentUser == null )
3441 {
35- return BadRequest ( new { message = "Username atau password salah" } ) ;
42+ return BadRequest ( new LoginFailedResponse ( Message : "Username atau password salah" ) ) ;
3643 } else
3744 {
38- return Ok ( new
39- {
40- message = "Sukses" ,
41- token = _jwtTokenService . GenerateNewToken ( currentUser ) ,
42- user = GetUserDto . From ( currentUser )
43- } ) ;
45+ return Ok ( new LoginSucceedResponse
46+ (
47+ Message : "Sukses" ,
48+ Token : _jwtTokenService . GenerateNewToken ( currentUser ) ,
49+ User : GetUserDto . From ( currentUser )
50+ ) ) ;
4451 }
4552 }
4653
4754
4855 }
56+
57+ internal record LoginSucceedResponse (
58+ [ property: JsonPropertyName ( "token" ) ] [ UsedImplicitly ] string Token ,
59+ [ property: JsonPropertyName ( "user" ) ] [ UsedImplicitly ] GetUserDto User ,
60+ [ property: JsonPropertyName ( "message" ) ] [ UsedImplicitly ] string Message ) ;
61+
62+ internal record LoginFailedResponse (
63+ [ property: JsonPropertyName ( "message" ) ] [ UsedImplicitly ] string Message ) ;
4964
50- public class LoginBodyDto
65+ public class LoginRequest
5166 {
5267 [ JsonPropertyName ( "username" ) ]
5368 public string Username { get ; set ; } = null ! ;
5469
5570 [ JsonPropertyName ( "password" ) ]
5671 public string Password { get ; set ; } = null ! ;
5772 }
73+
74+ file class LoginSucceedResponseExample : IExamplesProvider < LoginSucceedResponse >
75+ {
76+ public LoginSucceedResponse GetExamples ( )
77+ {
78+ return new LoginSucceedResponse (
79+ Token : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6ImFkbWluIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiQWRtaW4iLCJleHAiOjE3NjgyNzE0MDEsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTE1NCIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6MzAwMCJ9.H46pIQejekT9s-GuXsEauIqGjK2zu2vitYyWAhLRu8Y" ,
80+ User : new GetUserDto ( id : 1 , username : "admin" , isAdmin : true ) ,
81+ Message : "Sukses"
82+ ) ;
83+ }
84+ }
85+
86+ file class LoginFailedResponseExample : IExamplesProvider < LoginFailedResponse >
87+ {
88+ public LoginFailedResponse GetExamples ( )
89+ {
90+ return new LoginFailedResponse ( Message : "Username atau password salah" ) ;
91+ }
92+ }
5893}
5994
0 commit comments