Skip to content

Commit 4d3895d

Browse files
Merge pull request #61 from phamtiendungcw/dungcw
Refactor login and registration processes
2 parents 2f2b81f + 62cf734 commit 4d3895d

File tree

9 files changed

+21
-20
lines changed

9 files changed

+21
-20
lines changed

src/MLS.Api/Controllers/AccountController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.AspNetCore.Mvc;
44
using MLS.Api.Controllers.BaseController;
55
using MLS.Application.Features.User.Commands.RegisterUserCommand;
6-
using MLS.Application.Features.User.Queries.LoginUserByUserName;
6+
using MLS.Application.Features.User.Queries.LoginUserByAuthentication;
77
using MLS.Application.Models.Identity;
88

99
namespace MLS.Api.Controllers;
@@ -35,7 +35,7 @@ public async Task<ActionResult> RegisterUser([FromBody] RegisterUserCommand user
3535
[ProducesResponseType(StatusCodes.Status404NotFound)]
3636
public async Task<ActionResult<AuthResponse>> LoginUser([FromBody] AuthRequest loginUser)
3737
{
38-
var user = await _mediator.Send(new LoginUserByUserNameQuery(loginUser));
38+
var user = await _mediator.Send(new LoginUserByAuthenticationQuery(loginUser));
3939
return Ok(user);
4040
}
4141
}

src/MLS.Persistence/IdentityServicesRegistration.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.AspNetCore.Authentication.JwtBearer;
22
using Microsoft.AspNetCore.Identity;
3-
using Microsoft.EntityFrameworkCore;
43
using Microsoft.Extensions.Configuration;
54
using Microsoft.Extensions.DependencyInjection;
65
using Microsoft.IdentityModel.Tokens;
@@ -19,7 +18,6 @@ public static IServiceCollection AddIdentityServices(this IServiceCollection ser
1918
{
2019
// Configure JWT settings
2120
services.Configure<JwtSettings>(configuration.GetSection("JwtSettings"));
22-
services.AddDbContext<MatLidStoreDatabaseContext>(options => { options.UseOracle(configuration.GetConnectionString("MatLidOracleDBConnectionString")); });
2321

2422
// Add Identity services with User and AppRole, using the Oracle database
2523
services.AddIdentity<AppUser, AppRole>()

src/MLS.Persistence/PersistenceServiceRegistration.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using Microsoft.Extensions.Configuration;
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Configuration;
23
using Microsoft.Extensions.DependencyInjection;
34
using MLS.Application.Contracts.Persistence.Common;
45
using MLS.Application.Contracts.Persistence.IRepositories;
6+
using MLS.Persistence.DatabaseContext;
57
using MLS.Persistence.Repository;
68
using MLS.Persistence.Repository.Common;
79

@@ -11,6 +13,7 @@ public static class PersistenceServiceRegistration
1113
{
1214
public static IServiceCollection AddPersistenceServices(this IServiceCollection services, IConfiguration configuration)
1315
{
16+
services.AddDbContext<MatLidStoreDatabaseContext>(options => { options.UseOracle(configuration.GetConnectionString("MatLidOracleDBConnectionString")); });
1417
services.AddCors(options => { options.AddPolicy("MatLidStoreUI", b => b.WithOrigins("https://localhost:4200", "http://localhost:4200").AllowAnyHeader().AllowAnyMethod()); });
1518
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
1619
services.AddScoped<IAddressRepository, AddressRepository>();

src/MLS.WebUI/src/app/theme/pages/login-layout/login-layout.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, inject } from '@angular/core';
22
import { Router } from '@angular/router';
33
import { AccountService } from 'src/app/core/data/account.service';
4-
import { LoginModel, MatLidStoreServices, UserDetailsDto } from 'src/app/core/data/mls-data.service';
4+
import { AuthRequest, MatLidStoreServices, UserDetailsDto } from 'src/app/core/data/mls-data.service';
55

66
@Component({
77
selector: 'app-login-layout',
@@ -10,7 +10,7 @@ import { LoginModel, MatLidStoreServices, UserDetailsDto } from 'src/app/core/da
1010
})
1111
export class LoginLayoutComponent {
1212
loggedIn = false;
13-
model: LoginModel = new LoginModel();
13+
model: AuthRequest = new AuthRequest();
1414
user: UserDetailsDto | null = null;
1515
private matlidapi = inject(MatLidStoreServices);
1616
private router = inject(Router);

src/MSL.Application/DTO/User/UserDtoValidator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public RegisterUserModelValidator()
9797
RuleFor(x => x.LastName)
9898
.NotEmpty().WithMessage("LastName cannot be empty.")
9999
.MaximumLength(50).WithMessage("LastName must be less than 50 characters.");
100-
RuleFor(x => x.Phone)
101-
.NotEmpty().WithMessage("Phone cannot be empty.");
100+
RuleFor(x => x.PhoneNumber)
101+
.NotEmpty().WithMessage("PhoneNumber cannot be empty.");
102102
}
103103
}
104104

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using MediatR;
2+
using MLS.Application.Models.Identity;
3+
4+
namespace MLS.Application.Features.User.Queries.LoginUserByAuthentication;
5+
6+
public record LoginUserByAuthenticationQuery(AuthRequest model) : IRequest<AuthResponse>;

src/MSL.Application/Features/User/Queries/LoginUserByUserName/LoginUserByUserNameQueryHandler.cs src/MSL.Application/Features/User/Queries/LoginUserByAuthentication/LoginUserByAuthenticationQueryHandler.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
using MLS.Application.Features.User.Queries.GetUserDetails;
55
using MLS.Application.Models.Identity;
66

7-
namespace MLS.Application.Features.User.Queries.LoginUserByUserName;
7+
namespace MLS.Application.Features.User.Queries.LoginUserByAuthentication;
88

9-
public class LoginUserByUserNameQueryHandler : IRequestHandler<LoginUserByUserNameQuery, AuthResponse>
9+
public class LoginUserByAuthenticationQueryHandler : IRequestHandler<LoginUserByAuthenticationQuery, AuthResponse>
1010
{
1111
private readonly IAuthService _authService;
1212
private readonly IAppLogger<GetUserDetailsQueryHandler> _logger;
1313

14-
public LoginUserByUserNameQueryHandler(IAuthService authService, IAppLogger<GetUserDetailsQueryHandler> logger)
14+
public LoginUserByAuthenticationQueryHandler(IAuthService authService, IAppLogger<GetUserDetailsQueryHandler> logger)
1515
{
1616
_authService = authService;
1717
_logger = logger;
1818
}
1919

20-
public async Task<AuthResponse> Handle(LoginUserByUserNameQuery request, CancellationToken cancellationToken)
20+
public async Task<AuthResponse> Handle(LoginUserByAuthenticationQuery request, CancellationToken cancellationToken)
2121
{
2222
var response = await _authService.Login(request.model);
2323

src/MSL.Application/Features/User/Queries/LoginUserByUserName/LoginUserByUserNameQuery.cs

-6
This file was deleted.

src/MSL.Application/Models/Identity/RegistrationRequest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public class RegistrationRequest
77
{
88
public string Username { get; set; } = string.Empty;
99
[EmailAddress] public string Email { get; set; } = string.Empty;
10+
[Phone] public string PhoneNumber { get; set; } = string.Empty; // User's phone number
1011
[NotMapped] public string Password { get; set; } = string.Empty;
1112
public string FirstName { get; set; } = string.Empty; // User's FirstName
1213
public string LastName { get; set; } = string.Empty; // User's LastName
13-
[Phone] public string Phone { get; set; } = string.Empty; // User's phone number
1414
}

0 commit comments

Comments
 (0)