1- using jjodel_persistence . Models . Entity ;
1+ using jjodel_persistence . Models . Dto ;
2+ using jjodel_persistence . Models . Entity ;
23using jjodel_persistence . Models . Mail ;
3- using jjodel_persistence . Models . Dto ;
4+ using jjodel_persistence . Models . Settings ;
45using jjodel_persistence . Services ;
56using Microsoft . AspNetCore . Authorization ;
6- using Microsoft . AspNetCore . Mvc ;
77using Microsoft . AspNetCore . Identity ;
8+ using Microsoft . AspNetCore . Mvc ;
89using Microsoft . EntityFrameworkCore ;
9- using System . Text ;
10- using System . Security . Claims ;
11- using Microsoft . IdentityModel . Tokens ;
12- using jjodel_persistence . Models . Settings ;
1310using Microsoft . Extensions . Options ;
11+ using Microsoft . IdentityModel . Tokens ;
1412using System . IdentityModel . Tokens . Jwt ;
13+ using System . Security . Claims ;
14+ using System . Text ;
15+ using static System . Runtime . InteropServices . JavaScript . JSType ;
1516
1617
1718namespace jjodel_persistence . Controllers . API {
@@ -238,6 +239,8 @@ public async Task<IActionResult> GetRoles() {
238239 [ HttpPost ( "[action]" ) ]
239240 public async Task < IActionResult > Login ( [ FromBody ] LoginRequest loginRequest ) {
240241 try {
242+ LoginResponse response = new LoginResponse ( ) ;
243+
241244 if ( ModelState . IsValid ) {
242245
243246 _logger . LogInformation ( "Login request by user: " + loginRequest . Email ) ;
@@ -246,14 +249,18 @@ public async Task<IActionResult> Login([FromBody] LoginRequest loginRequest) {
246249
247250 if ( user == null || user . IsDeleted || ! user . EmailConfirmed ) {
248251 _logger . LogWarning ( "User: " + loginRequest . Email + " does not exists, is deleted or is not confirmed." ) ;
249- return BadRequest ( ) ;
252+ response . Title = "Login failed" ;
253+ response . Description = "Your account is not ready to use. Please confirm your email via the link we sent. Or, if already confirmed, reset your password to continue. " ;
254+ return BadRequest ( response ) ;
250255 }
251256
252257 var result = await _signInManager . PasswordSignInAsync ( user , loginRequest . Password , false , false ) ;
253258
254259 if ( ! result . Succeeded ) {
255260 _logger . LogWarning ( "User failed login: " + loginRequest . Email ) ;
256- return BadRequest ( ) ;
261+ response . Title = "Login failed" ;
262+ response . Description = "Invalid credentials." ;
263+ return BadRequest ( response ) ;
257264 }
258265 _logger . LogInformation ( "User " + loginRequest . Email + " login successfully" ) ;
259266
@@ -283,9 +290,10 @@ public async Task<IActionResult> Login([FromBody] LoginRequest loginRequest) {
283290 expires : expiry ,
284291 signingCredentials : creds
285292 ) ;
286-
287- string t = new JwtSecurityTokenHandler ( ) . WriteToken ( token ) ;
288- return Ok ( new TokenResponse ( ) { Token = t , Expires = expiry } ) ;
293+
294+ response . Token = new JwtSecurityTokenHandler ( ) . WriteToken ( token ) ;
295+ response . Expires = expiry ;
296+ return Ok ( response ) ;
289297 }
290298 return BadRequest ( ) ;
291299 }
@@ -299,9 +307,13 @@ public async Task<IActionResult> Login([FromBody] LoginRequest loginRequest) {
299307 [ AllowAnonymous ]
300308 [ HttpPost ( "register" ) ]
301309 public async Task < IActionResult > Register ( [ FromBody ] RegisterRequest request ) {
310+ RegisterResponse response = new RegisterResponse ( ) ;
311+ response . Title = "Registration completed" ;
312+ response . Description = "A confirmation email has been sent to your email address. Please confirm your account before logging in." ;
302313 try {
314+
303315 ApplicationUser existingUser = await this . _userManager . FindByEmailAsync ( request . Email ) ;
304- if ( ModelState . IsValid && existingUser == null ) {
316+ if ( ModelState . IsValid && ( existingUser == null || existingUser . IsDeleted ) ) {
305317 var user = new ApplicationUser {
306318 Id = Guid . NewGuid ( ) . ToString ( ) ,
307319 _Id = request . _Id ,
@@ -323,8 +335,12 @@ public async Task<IActionResult> Register([FromBody] RegisterRequest request) {
323335 // create user.
324336 var result = await _userManager . CreateAsync ( user , request . Password ) ;
325337 if ( ! result . Succeeded ) {
326- _logger . LogWarning ( "Registration process failed for user " + request . Email + ": " + string . Join ( ";" , result . Errors . Select ( e => "Code: " + e . Code + " Description" + e . Description ) ) ) ;
327- return BadRequest ( ) ;
338+ this . _logger . LogWarning ( "Registration process failed for user " + request . Email + ": " + string . Join ( ";" , result . Errors . Select ( e => "Code: " + e . Code + " Description" + e . Description ) ) ) ;
339+
340+ response . Title = "Registration process failed" ;
341+ response . Description = "The following fields are invalid: " + string . Join ( " " , result . Errors . Select ( e => e . Description ) ) ;
342+
343+ return BadRequest ( response ) ;
328344 }
329345 _logger . LogInformation ( "User " + request . Email + " was registered" ) ;
330346 // assign user role.
@@ -343,23 +359,28 @@ await _mailService.SendEmail(
343359 Token = confirmToken ,
344360 Id = user . Id ,
345361 Url = this . _configuration [ "FrontendEndpoint" ]
346-
347-
348362 } ) ;
349363 return Ok ( result ) ;
350364 }
351365 else {
352- if ( existingUser != null ) {
353- _logger . LogWarning ( "Registration process failed: user " + request . Email + " already exists." ) ;
354- return BadRequest ( "Registration process failed: user " + request . Email + " already exists." ) ;
355-
366+ response . Title = "Registration process failed" ;
367+ var errorFields = ModelState . Where ( ms => ms . Value . Errors . Count > 0 ) . Select ( ms => ms . Key ) ;
368+ response . Description = "The following fields are invalid: " + string . Join ( ", " , errorFields ) + "." ;
369+
370+ if ( existingUser != null && ! existingUser . IsDeleted ) {
371+ this . _logger . LogWarning ( "Registration process failed: user " + request . Email + " already exists." ) ;
372+ response . Description = "An account with this email address already exists." ;
373+ return BadRequest ( response ) ;
356374 }
357- return BadRequest ( ) ;
375+
376+ return BadRequest ( response ) ;
358377 }
359378 }
360379 catch ( Exception ex ) {
361380 this . _logger . LogError ( "Register error: " + ex . Message ) ;
362- return BadRequest ( ) ;
381+ response . Title = "Registration error" ;
382+ response . Description = "An error occurred during registration. Please try again later." ;
383+ return BadRequest ( response ) ;
363384 }
364385 }
365386
0 commit comments