11
11
namespace api . Controllers . Auth . v1 ;
12
12
13
13
[ ApiController ]
14
- [ Route ( "api/auth" ) ]
14
+ [ Route ( "api/v{version:apiVersion}/ auth" ) ]
15
15
[ ApiVersion ( "1" ) ]
16
- public class AuthController ( IAuthService service ) : ControllerBase
16
+ public class AuthController ( IAuthService service , ILogger < AuthController > logger ) : ControllerBase
17
17
{
18
18
private readonly IAuthService _service = service ?? throw new ArgumentNullException ( nameof ( service ) ) ;
19
+ private readonly ILogger < AuthController > _logger = logger ;
19
20
20
21
21
22
[ HttpGet ( "me" ) ]
22
23
[ Authorize ]
24
+ [ ProducesResponseType ( typeof ( UserDto ) , 200 ) ]
25
+ [ ProducesResponseType ( 401 ) ]
26
+ [ ProducesResponseType ( 404 ) ]
23
27
public async Task < IActionResult > AuthMe ( )
24
28
{
25
29
26
30
27
31
var userIdClaim = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
28
- if ( string . IsNullOrEmpty ( userIdClaim ) || ! Guid . TryParse ( userIdClaim , out Guid userId ) )
32
+ if ( string . IsNullOrEmpty ( userIdClaim ) )
29
33
{
30
- return Unauthorized ( "Invalid token" ) ;
34
+ return Unauthorized ( "Invalid or missing user token" ) ;
35
+ }
36
+
37
+ if ( ! Guid . TryParse ( userIdClaim , out Guid userId ) )
38
+ {
39
+ return Unauthorized ( "Invalid user identifier in token" ) ;
31
40
}
32
41
33
42
UserDto ? response = await _service . GetById ( userId ) ;
@@ -39,66 +48,127 @@ public async Task<IActionResult> AuthMe()
39
48
}
40
49
41
50
[ HttpPost ( "register" ) ]
51
+ [ ProducesResponseType ( typeof ( UserDto ) , 200 ) ]
52
+ [ ProducesResponseType ( 400 ) ]
53
+ [ ProducesResponseType ( 500 ) ]
42
54
public async Task < IActionResult > Register ( [ FromBody ] RegisterRequestDto requestDto )
43
55
{
44
56
if ( ! ModelState . IsValid )
45
57
{
46
58
return BadRequest ( ModelState ) ;
47
59
}
48
60
49
- var existingUser = await _service . GetByEmail ( requestDto . email ) ;
50
- if ( existingUser is not null )
61
+ try
51
62
{
52
- return BadRequest ( "User with this email already exists" ) ;
53
- }
63
+ var existingUser = await _service . GetByEmail ( requestDto . email ) ;
64
+ if ( existingUser is not null )
65
+ {
66
+ return BadRequest ( "Registration failed. Please check your details and try again." ) ;
67
+ }
54
68
55
- var response = await _service . Register ( requestDto . lastName , requestDto . firstName , requestDto . password , requestDto . email ) ;
56
- if ( response == null )
69
+ var response = await _service . Register ( requestDto . lastName , requestDto . firstName , requestDto . password , requestDto . email ) ;
70
+ if ( response == null )
71
+ {
72
+ return StatusCode ( 500 , "Registration failed. Please try again later." ) ;
73
+ }
74
+
75
+
76
+ return Ok ( response ) ;
77
+ }
78
+ catch ( Exception ex )
57
79
{
58
- return StatusCode ( 500 , "Something went wrong while creating the user" ) ;
80
+ _logger . LogError ( ex , "Error during user registration for email: {Email}" , requestDto . email ) ;
81
+ return StatusCode ( 500 , "Registration failed. Please try again later." ) ;
59
82
}
60
83
61
-
62
- return Ok ( response ) ;
63
84
}
64
85
65
86
66
87
[ HttpPost ( "login" ) ]
88
+ [ ProducesResponseType ( 200 ) ]
89
+ [ ProducesResponseType ( 400 ) ]
90
+ [ ProducesResponseType ( 429 ) ]
67
91
public async Task < IActionResult > Login ( [ FromBody ] LoginRequestDto requestDto )
68
92
{
69
93
if ( ! ModelState . IsValid )
70
94
{
71
95
return BadRequest ( ModelState ) ;
72
96
}
73
97
74
- var response = await _service . Authenticate ( requestDto . email , requestDto . password ) ;
98
+ try
99
+ {
100
+ var response = await _service . Authenticate ( requestDto . email , requestDto . password ) ;
101
+
102
+ if ( response == null )
103
+ {
104
+ return BadRequest ( "Invalid credentials. Please check your email and password." ) ;
105
+ }
75
106
76
- if ( response == null )
107
+ return Ok ( response ) ;
108
+ }
109
+ catch ( Exception ex )
77
110
{
78
- return BadRequest ( "Invalid email or password" ) ;
111
+ _logger . LogError ( ex , "Error during authentication for email: {Email}" , requestDto . email ) ;
112
+ return StatusCode ( 500 , "Authentication failed. Please try again later." ) ;
79
113
}
80
114
81
- return Ok ( response ) ;
115
+
82
116
}
83
117
84
118
[ Authorize ]
85
119
[ HttpPost ( "logout" ) ]
120
+ [ ProducesResponseType ( 200 ) ]
121
+ [ ProducesResponseType ( 401 ) ]
122
+ [ ProducesResponseType ( 500 ) ]
86
123
public async Task < IActionResult > Logout ( )
87
124
88
125
{
89
126
var userIdClaim = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
90
- if ( string . IsNullOrEmpty ( userIdClaim ) || ! Guid . TryParse ( userIdClaim , out Guid userId ) )
127
+
128
+ if ( string . IsNullOrEmpty ( userIdClaim ) )
91
129
{
92
- return Unauthorized ( "Invalid token" ) ;
130
+ return Unauthorized ( "Invalid or missing user token" ) ;
93
131
}
94
132
95
- bool response = await _service . Logout ( userId ) ;
96
- if ( ! response )
133
+ if ( ! Guid . TryParse ( userIdClaim , out Guid userId ) )
97
134
{
98
- return StatusCode ( 500 , "Logout failed ") ;
135
+ return Unauthorized ( "Invalid user identifier in token ") ;
99
136
}
100
137
101
- return Ok ( new { message = "Logged out successfully" } ) ;
138
+ try
139
+ {
140
+ bool response = await _service . Logout ( userId ) ;
141
+ if ( ! response )
142
+ {
143
+ return StatusCode ( 500 , "Logout failed. Please try again." ) ;
144
+ }
145
+
146
+ return Ok ( new { message = "Successfully logged out" , timestamp = DateTime . UtcNow } ) ;
147
+ }
148
+ catch ( Exception ex )
149
+ {
150
+ _logger . LogError ( ex , "Error during logout for user: {UserId}" , userId ) ;
151
+ return StatusCode ( 500 , "Logout failed. Please try again." ) ;
152
+ }
153
+
154
+ }
155
+
156
+ [ HttpGet ( "status" ) ]
157
+ [ Authorize ]
158
+ [ ProducesResponseType ( 200 ) ]
159
+ [ ProducesResponseType ( 401 ) ]
160
+ public IActionResult GetAuthStatus ( )
161
+ {
162
+ var userIdClaim = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
163
+ var emailClaim = User . FindFirst ( ClaimTypes . Email ) ? . Value ;
164
+
165
+ return Ok ( new
166
+ {
167
+ isAuthenticated = true ,
168
+ userId = userIdClaim ,
169
+ email = emailClaim ,
170
+ timestamp = DateTime . UtcNow
171
+ } ) ;
102
172
}
103
173
104
174
0 commit comments