1
- using AdminHubApi . Dtos . Auth ;
1
+ using System . Security . Claims ;
2
+ using AdminHubApi . Dtos . Auth ;
2
3
using AdminHubApi . Entities ;
3
4
using AdminHubApi . Interfaces ;
4
- using Microsoft . AspNetCore . Authorization ;
5
5
using Microsoft . AspNetCore . Identity ;
6
6
using Microsoft . AspNetCore . Mvc ;
7
7
using Microsoft . Extensions . Options ;
8
- using System . Security . Claims ;
9
8
10
9
namespace AdminHubApi . Controllers ;
11
10
12
11
[ ApiController ]
13
- [ Route ( "/ api/auth" ) ]
12
+ [ Route ( "api/auth" ) ]
14
13
public class AuthController : ControllerBase
15
14
{
16
15
private readonly UserManager < ApplicationUser > _userManager ;
@@ -33,32 +32,28 @@ public AuthController(
33
32
_jwtSettings = jwtSettings . Value ;
34
33
}
35
34
35
+ /// <summary>
36
+ /// Login with username and password
37
+ /// </summary>
36
38
[ HttpPost ( "login" ) ]
37
39
public async Task < IActionResult > Login ( [ FromBody ] AuthRequestDto request )
38
40
{
39
- // Check if model is valid
40
41
if ( ! ModelState . IsValid )
41
42
return BadRequest ( ModelState ) ;
42
43
43
- // Check if user exists
44
44
var user = await _userManager . FindByNameAsync ( request . Username ) ;
45
45
46
46
if ( user == null )
47
47
return Unauthorized ( new { message = "Username or password is incorrect" } ) ;
48
48
49
- // Verify password
50
49
var result = await _signinManager . CheckPasswordSignInAsync ( user , request . Password , false ) ;
51
50
52
- if ( ! result . Succeeded ) // FIXED: Check for NOT succeeded
51
+ if ( ! result . Succeeded )
53
52
return Unauthorized ( new { message = "Username or password is incorrect" } ) ;
54
53
55
- // Get user roles
56
54
var userRoles = await _userManager . GetRolesAsync ( user ) ;
57
-
58
- // Generate token
59
55
var token = await _tokenService . GenerateJwtTokenAsync ( user , userRoles ) ;
60
56
61
- // Return token and user info
62
57
return Ok ( new AuthResponseDto
63
58
{
64
59
Token = token ,
@@ -68,30 +63,28 @@ public async Task<IActionResult> Login([FromBody] AuthRequestDto request)
68
63
} ) ;
69
64
}
70
65
66
+ /// <summary>
67
+ /// Register a new user
68
+ /// </summary>
71
69
[ HttpPost ( "register" ) ]
72
70
public async Task < IActionResult > Register ( [ FromBody ] RegisterRequestDto request )
73
71
{
74
- // Check if model is valid
75
72
if ( ! ModelState . IsValid )
76
73
return BadRequest ( ModelState ) ;
77
74
78
- // Check if username already exists
79
75
if ( await _userManager . FindByNameAsync ( request . Username ) != null )
80
76
return BadRequest ( new { message = "Username already exists" } ) ;
81
77
82
- // Check if email already exists
83
78
if ( await _userManager . FindByEmailAsync ( request . Email ) != null )
84
79
return BadRequest ( new { message = "Email already exists" } ) ;
85
80
86
- // Create new user
87
81
var user = new ApplicationUser
88
82
{
89
83
UserName = request . Username ,
90
84
Email = request . Email ,
91
85
SecurityStamp = Guid . NewGuid ( ) . ToString ( )
92
86
} ;
93
87
94
- // Add user to database
95
88
var result = await _userManager . CreateAsync ( user , request . Password ) ;
96
89
97
90
if ( ! result . Succeeded )
@@ -100,7 +93,6 @@ public async Task<IActionResult> Register([FromBody] RegisterRequestDto request)
100
93
errors = result . Errors . Select ( e => e . Description )
101
94
} ) ;
102
95
103
- // Add user to default role
104
96
if ( ! await _roleManager . RoleExistsAsync ( "User" ) )
105
97
await _roleManager . CreateAsync ( new IdentityRole ( "User" ) ) ;
106
98
@@ -109,63 +101,11 @@ public async Task<IActionResult> Register([FromBody] RegisterRequestDto request)
109
101
return Ok ( new { message = "User registered successfully" } ) ;
110
102
}
111
103
112
- [ HttpGet ( "me" ) ]
113
- [ Authorize ]
114
- public async Task < IActionResult > GetCurrentUser ( )
115
- {
116
- var userId = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
117
-
118
- if ( string . IsNullOrEmpty ( userId ) )
119
- return Unauthorized ( ) ;
120
-
121
- var user = await _userManager . FindByIdAsync ( userId ) ;
122
-
123
- if ( user == null )
124
- return NotFound ( ) ;
125
-
126
- var roles = await _userManager . GetRolesAsync ( user ) ;
127
- var claims = await _userManager . GetClaimsAsync ( user ) ;
128
-
129
- return Ok ( new
130
- {
131
- Id = user . Id ,
132
- Username = user . UserName ,
133
- Email = user . Email ,
134
- Roles = roles ,
135
- Claims = claims . Select ( c => new { Type = c . Type , Value = c . Value } )
136
- } ) ;
137
- }
138
-
139
- [ HttpPost ( "change-password" ) ]
140
- [ Authorize ]
141
- public async Task < IActionResult > ChangePassword ( [ FromBody ] ChangePasswordRequestDto request )
142
- {
143
- if ( ! ModelState . IsValid )
144
- return BadRequest ( ModelState ) ;
145
-
146
- var userId = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
147
-
148
- if ( string . IsNullOrEmpty ( userId ) )
149
- return Unauthorized ( ) ;
150
-
151
- var user = await _userManager . FindByIdAsync ( userId ) ;
152
-
153
- if ( user == null )
154
- return NotFound ( ) ;
155
-
156
- var result = await _userManager . ChangePasswordAsync ( user , request . CurrentPassword , request . NewPassword ) ;
157
-
158
- if ( ! result . Succeeded )
159
- return BadRequest ( new {
160
- message = "Password change failed" ,
161
- errors = result . Errors . Select ( e => e . Description )
162
- } ) ;
163
-
164
- return Ok ( new { message = "Password changed successfully" } ) ;
165
- }
166
-
104
+ /// <summary>
105
+ /// Request a password reset link
106
+ /// </summary>
167
107
[ HttpPost ( "forgot-password" ) ]
168
- public async Task < IActionResult > ForgotPassword ( [ FromBody ] ChangePasswordRequestDto request )
108
+ public async Task < IActionResult > ForgotPassword ( [ FromBody ] ForgotPasswordRequestDto request )
169
109
{
170
110
if ( ! ModelState . IsValid )
171
111
return BadRequest ( ModelState ) ;
@@ -176,7 +116,6 @@ public async Task<IActionResult> ForgotPassword([FromBody] ChangePasswordRequest
176
116
if ( user == null )
177
117
return Ok ( new { message = "If your email is registered, you will receive a password reset link" } ) ;
178
118
179
- // Generate password reset token
180
119
var token = await _userManager . GeneratePasswordResetTokenAsync ( user ) ;
181
120
182
121
// In a real app, you would send an email with the token
@@ -188,6 +127,9 @@ public async Task<IActionResult> ForgotPassword([FromBody] ChangePasswordRequest
188
127
} ) ;
189
128
}
190
129
130
+ /// <summary>
131
+ /// Reset password using token
132
+ /// </summary>
191
133
[ HttpPost ( "reset-password" ) ]
192
134
public async Task < IActionResult > ResetPassword ( [ FromBody ] ResetPasswordRequestDto request )
193
135
{
@@ -210,6 +152,9 @@ public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordRequestDt
210
152
return Ok ( new { message = "Password reset successful" } ) ;
211
153
}
212
154
155
+ /// <summary>
156
+ /// Refresh an authentication token
157
+ /// </summary>
213
158
[ HttpPost ( "refresh-token" ) ]
214
159
public async Task < IActionResult > RefreshToken ( [ FromBody ] RefreshTokenRequestDto request )
215
160
{
0 commit comments