Backend login and register
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>9c947c10-2373-4590-92a9-e5fe6b759c69</UserSecretsId>
|
||||
<SpaRoot>..\ClientApp\</SpaRoot>
|
||||
<SpaProxyServerUrl>http://localhost:44492</SpaProxyServerUrl>
|
||||
<SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
|
||||
@@ -16,4 +17,8 @@
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Application\Application.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
using API.Extension;
|
||||
using Application.DTOs;
|
||||
using Application.Interfaces;
|
||||
using Application.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
public class AuthController(IAuthenticationService authService) : BaseApiController
|
||||
{
|
||||
[HttpPost("register")]
|
||||
public async Task<IResult> Register(RegisterRequest registerRequest)
|
||||
{
|
||||
var response = await authService.RegisterAsync(registerRequest);
|
||||
return response.ToHttpResponse();
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public async Task<IResult> Login(LoginRequest loginRequest)
|
||||
{
|
||||
var response = await authService.LoginAsync(loginRequest);
|
||||
return response.ToHttpResponse();
|
||||
}
|
||||
|
||||
[HttpPost("refresh-token")]
|
||||
public async Task<IResult> RefreshToken(RefreshTokenRequest refreshTokenRequest)
|
||||
{
|
||||
var response = await authService.RefreshTokensAsync(refreshTokenRequest);
|
||||
return response.ToHttpResponse();
|
||||
}
|
||||
|
||||
[HttpPost("send-reset-email/{email}")]
|
||||
public async Task<IResult> SendResetEmail(string email)
|
||||
{
|
||||
var response = await authService.SendResetEmailAsync(email);
|
||||
return response.ToHttpResponse();
|
||||
}
|
||||
|
||||
[HttpPost("reset-password")]
|
||||
public async Task<IResult> ResetPassword(ResetPasswordDto resetPasswordDto)
|
||||
{
|
||||
var response = await authService.ResetPasswordAsync(resetPasswordDto);
|
||||
return response.ToHttpResponse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using API.Extension;
|
||||
using Application.Interfaces;
|
||||
using Application.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
public class UserController(IUserService userService) : BaseApiController
|
||||
{
|
||||
[Authorize(Roles = "SuperAdmin, Admin")]
|
||||
[HttpGet]
|
||||
public async Task<IResult> GetAllUsers(
|
||||
[FromQuery] int pageNumber = 1,
|
||||
[FromQuery] int pageSize = 10)
|
||||
{
|
||||
var users = await userService.GetAsync(pageNumber, pageSize);
|
||||
return users.ToHttpResponse();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPut]
|
||||
public async Task<IResult> UpdateUser([FromBody] UserUpdateRequest userUpdateRequest)
|
||||
{
|
||||
var result = await userService.UpdateAsync(userUpdateRequest);
|
||||
return result.ToHttpResponse();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IResult> DeleteUser(int id)
|
||||
{
|
||||
var currentUserId = int.Parse(User.FindFirst("UserId")!.Value);
|
||||
var result = await userService.DeleteAsync(id, currentUserId);
|
||||
return result.ToHttpResponse();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IResult> GetUserById(int id)
|
||||
{
|
||||
var user = await userService.GetUserByIdAsync(id);
|
||||
return user.ToHttpResponse();
|
||||
}
|
||||
|
||||
[Authorize(Roles = "SuperAdmin")]
|
||||
[HttpPost("assign-role")]
|
||||
public async Task<IResult> AssignRole([FromBody] AssingRoleRequest roleRequest)
|
||||
|
||||
{
|
||||
var result = await userService.AssignRoleAsync(roleRequest);
|
||||
return result.ToHttpResponse();
|
||||
}
|
||||
|
||||
[Authorize(Roles = "SuperAdmin")]
|
||||
[HttpDelete("revoke-role")]
|
||||
public async Task<IResult> RevokeRole([FromBody] AssingRoleRequest roleRequest)
|
||||
{
|
||||
var result = await userService.RevokeRoleAsync(roleRequest);
|
||||
return result.ToHttpResponse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Application.Common.Results;
|
||||
|
||||
namespace API.Extension;
|
||||
|
||||
public static class ResultExtension
|
||||
{
|
||||
public static IResult ToHttpResponse(this Result result)
|
||||
{
|
||||
if (result.IsSuccess) return Results.Ok(result);
|
||||
|
||||
return MapErrorResponse(result.Error, result);
|
||||
}
|
||||
|
||||
public static IResult ToHttpResponse<T>(this Result<T> result)
|
||||
{
|
||||
if (result.IsSuccess)
|
||||
return Results.Ok(result);
|
||||
return MapErrorResponse(result.Error, result);
|
||||
}
|
||||
|
||||
private static IResult MapErrorResponse(Error? error, object result)
|
||||
{
|
||||
return error?.Code switch
|
||||
{
|
||||
ErrorTypeConstant.ValidationError => Results.BadRequest(result),
|
||||
ErrorTypeConstant.NotFound => Results.NotFound(result),
|
||||
ErrorTypeConstant.Forbidden => Results.Forbid(),
|
||||
ErrorTypeConstant.Unauthorized => Results.Unauthorized(),
|
||||
_ => Results.Problem(error?.Message, statusCode: 500)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,8 @@ namespace API.Extension;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddWebServices(this IServiceCollection services)
|
||||
public static IServiceCollection AddWebServices(this IServiceCollection services,
|
||||
ConfigurationManager builderConfiguration)
|
||||
{
|
||||
services.AddSwaggerGen(options =>
|
||||
{
|
||||
|
||||
+17
-1
@@ -1,4 +1,9 @@
|
||||
using API.Extension;
|
||||
using Application.Extensions;
|
||||
using Infrastructure.Context;
|
||||
using Infrastructure.Extensions;
|
||||
using Infrastructure.Utilities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
@@ -7,7 +12,18 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
// Add services to the container.
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddWebServices();
|
||||
builder.Services.AddWebServices(builder.Configuration);
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
builder.Services.AddApplication();
|
||||
|
||||
// PostgreSql Database for development
|
||||
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||||
{
|
||||
var postgreSqlSettings =
|
||||
builder.Configuration.GetRequiredSection("PostgreSqlSettings").Get<PostgreSqlSettings>();
|
||||
var connectionString = postgreSqlSettings?.ConnectionString;
|
||||
options.UseNpgsql(connectionString);
|
||||
});
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
|
||||
@@ -5,5 +5,18 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"PostgreSqlSettings": {
|
||||
"Database": "template_db",
|
||||
"Username": "natlinux"
|
||||
},
|
||||
"jwt": {
|
||||
"Key": "veryveryveryveryveryveryverysecretkey",
|
||||
"Issuer": "https://localhost:7091",
|
||||
"Audience": "http://localhost:5184"
|
||||
},
|
||||
"EmailSettings": {
|
||||
"SmtpServer": "smtp.gmail.com",
|
||||
"Port": 465
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user