Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor login and registration processes #61

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/MLS.Api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using MLS.Api.Controllers.BaseController;
using MLS.Application.Features.User.Commands.RegisterUserCommand;
using MLS.Application.Features.User.Queries.LoginUserByUserName;
using MLS.Application.Features.User.Queries.LoginUserByAuthentication;
using MLS.Application.Models.Identity;

namespace MLS.Api.Controllers;
Expand Down Expand Up @@ -35,7 +35,7 @@ public async Task<ActionResult> RegisterUser([FromBody] RegisterUserCommand user
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<AuthResponse>> LoginUser([FromBody] AuthRequest loginUser)
{
var user = await _mediator.Send(new LoginUserByUserNameQuery(loginUser));
var user = await _mediator.Send(new LoginUserByAuthenticationQuery(loginUser));
return Ok(user);
}
}
2 changes: 0 additions & 2 deletions src/MLS.Persistence/IdentityServicesRegistration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
Expand All @@ -19,7 +18,6 @@ public static IServiceCollection AddIdentityServices(this IServiceCollection ser
{
// Configure JWT settings
services.Configure<JwtSettings>(configuration.GetSection("JwtSettings"));
services.AddDbContext<MatLidStoreDatabaseContext>(options => { options.UseOracle(configuration.GetConnectionString("MatLidOracleDBConnectionString")); });

// Add Identity services with User and AppRole, using the Oracle database
services.AddIdentity<AppUser, AppRole>()
Expand Down
5 changes: 4 additions & 1 deletion src/MLS.Persistence/PersistenceServiceRegistration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MLS.Application.Contracts.Persistence.Common;
using MLS.Application.Contracts.Persistence.IRepositories;
using MLS.Persistence.DatabaseContext;
using MLS.Persistence.Repository;
using MLS.Persistence.Repository.Common;

Expand All @@ -11,6 +13,7 @@ public static class PersistenceServiceRegistration
{
public static IServiceCollection AddPersistenceServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<MatLidStoreDatabaseContext>(options => { options.UseOracle(configuration.GetConnectionString("MatLidOracleDBConnectionString")); });
services.AddCors(options => { options.AddPolicy("MatLidStoreUI", b => b.WithOrigins("https://localhost:4200", "http://localhost:4200").AllowAnyHeader().AllowAnyMethod()); });
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
services.AddScoped<IAddressRepository, AddressRepository>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
import { AccountService } from 'src/app/core/data/account.service';
import { LoginModel, MatLidStoreServices, UserDetailsDto } from 'src/app/core/data/mls-data.service';
import { AuthRequest, MatLidStoreServices, UserDetailsDto } from 'src/app/core/data/mls-data.service';

@Component({
selector: 'app-login-layout',
Expand All @@ -10,7 +10,7 @@ import { LoginModel, MatLidStoreServices, UserDetailsDto } from 'src/app/core/da
})
export class LoginLayoutComponent {
loggedIn = false;
model: LoginModel = new LoginModel();
model: AuthRequest = new AuthRequest();
user: UserDetailsDto | null = null;
private matlidapi = inject(MatLidStoreServices);
private router = inject(Router);
Expand Down
4 changes: 2 additions & 2 deletions src/MSL.Application/DTO/User/UserDtoValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public RegisterUserModelValidator()
RuleFor(x => x.LastName)
.NotEmpty().WithMessage("LastName cannot be empty.")
.MaximumLength(50).WithMessage("LastName must be less than 50 characters.");
RuleFor(x => x.Phone)
.NotEmpty().WithMessage("Phone cannot be empty.");
RuleFor(x => x.PhoneNumber)
.NotEmpty().WithMessage("PhoneNumber cannot be empty.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using MediatR;
using MLS.Application.Models.Identity;

namespace MLS.Application.Features.User.Queries.LoginUserByAuthentication;

public record LoginUserByAuthenticationQuery(AuthRequest model) : IRequest<AuthResponse>;
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
using MLS.Application.Features.User.Queries.GetUserDetails;
using MLS.Application.Models.Identity;

namespace MLS.Application.Features.User.Queries.LoginUserByUserName;
namespace MLS.Application.Features.User.Queries.LoginUserByAuthentication;

public class LoginUserByUserNameQueryHandler : IRequestHandler<LoginUserByUserNameQuery, AuthResponse>
public class LoginUserByAuthenticationQueryHandler : IRequestHandler<LoginUserByAuthenticationQuery, AuthResponse>
{
private readonly IAuthService _authService;
private readonly IAppLogger<GetUserDetailsQueryHandler> _logger;

public LoginUserByUserNameQueryHandler(IAuthService authService, IAppLogger<GetUserDetailsQueryHandler> logger)
public LoginUserByAuthenticationQueryHandler(IAuthService authService, IAppLogger<GetUserDetailsQueryHandler> logger)
{
_authService = authService;
_logger = logger;
}

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

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/MSL.Application/Models/Identity/RegistrationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class RegistrationRequest
{
public string Username { get; set; } = string.Empty;
[EmailAddress] public string Email { get; set; } = string.Empty;
[Phone] public string PhoneNumber { get; set; } = string.Empty; // User's phone number
[NotMapped] public string Password { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty; // User's FirstName
public string LastName { get; set; } = string.Empty; // User's LastName
[Phone] public string Phone { get; set; } = string.Empty; // User's phone number
}
Loading