massive work on user auth

This commit is contained in:
Вячеслав 2025-04-08 19:08:17 +05:00
parent 35331a87f1
commit c2bcaf0832
17 changed files with 186 additions and 19 deletions

View file

@ -8,7 +8,7 @@ namespace SurveyBackend.Controllers;
public class AuthController : ControllerBase
{
[HttpPost("login")]
public async Task<IActionResult> GetToken([FromBody] UserLoginDTO loginData)
public async Task<IActionResult> GetToken([FromBody] UserLoginDto loginData)
{
return Ok();
}

View file

@ -1,6 +1,6 @@
namespace SurveyBackend.DTOs;
public record UserLoginDTO
public record UserLoginDto
{
public required string Email { get; set; }
public required string Password { get; set; }

View file

@ -1,6 +1,6 @@
namespace SurveyBackend.DTOs;
public record UserRegistrationDTO
public record UserRegistrationDto
{
public string Email { get; set; }
public string Username { get; set; }

View file

@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Identity;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using SurveyBackend.Core.Models;
using Microsoft.IdentityModel.Tokens;
using SurveyBackend.Infrastructure;
using SurveyBackend.Infrastructure.Data;
namespace SurveyBackend;
@ -11,33 +13,44 @@ public class Program
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
AuthOptions.MakeOptions(builder.Configuration, Environment.GetEnvironmentVariable("JWT_SECRET_KEY"));
builder.Services.AddAuthorization();
builder.Services.AddDbContext<DataContext>(options =>
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"));
});
builder.Services.AddIdentity<User, IdentityRole<int>>(options => { })
.AddEntityFrameworkStores<DataContext>()
.AddDefaultTokenProviders();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = AuthOptions.Issuer,
ValidAudience = AuthOptions.Audience,
IssuerSigningKey = AuthOptions.SymmetricSecurityKey
};
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

View file

@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.14" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>

View file

@ -7,5 +7,11 @@
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=Application.db"
},
"JwtSettings": {
"SecretKey": "sigma_super_secret_key_for_jwt_tokens_yo",
"Issuer": "SurveyBackend",
"Audience": "SurveyClient",
"ExpiresInMinutes": 600
}
}

View file

@ -8,5 +8,11 @@
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=Application.db"
},
"JwtSettings": {
"SecretKey": "sigma_super_secret_key_for_jwt_tokens_yo_that_should_be_stored_in_ENV",
"Issuer": "SurveyBackend",
"Audience": "SurveyClient",
"ExpiresInMinutes": 600
}
}