refactor: manually create endpoints for auth
This commit is contained in:
parent
abd7a798c9
commit
e2baf275ff
41
Controllers/AuthController.cs
Normal file
41
Controllers/AuthController.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Microsoft.AspNetCore.Identity.Data;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Trazo.Model;
|
||||||
|
|
||||||
|
namespace Trazo.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public class AuthController(
|
||||||
|
SignInManager<User> _signInManager,
|
||||||
|
UserManager<User> _userManager,
|
||||||
|
IConfiguration _configuration
|
||||||
|
) : ControllerBase
|
||||||
|
{
|
||||||
|
|
||||||
|
[HttpPost("login")]
|
||||||
|
public async Task<IActionResult> Login([FromBody] LoginRequest request)
|
||||||
|
{
|
||||||
|
var result = await _signInManager.PasswordSignInAsync(
|
||||||
|
request.Email,
|
||||||
|
request.Password,
|
||||||
|
false,
|
||||||
|
false);
|
||||||
|
|
||||||
|
if (!result.Succeeded)
|
||||||
|
return Unauthorized();
|
||||||
|
|
||||||
|
// Generate your JWT here
|
||||||
|
return Ok(new { token = "your-jwt-token" });
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpPost("logout")]
|
||||||
|
public async Task<IActionResult> Logout()
|
||||||
|
{
|
||||||
|
await _signInManager.SignOutAsync();
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
}
|
27
Program.cs
27
Program.cs
@ -9,16 +9,29 @@ builder.Services.AddControllers();
|
|||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddAuthorization();
|
builder.Services.AddAuthorization();
|
||||||
builder.Services.AddAuthentication().AddCookie(IdentityConstants.ApplicationScheme);
|
|
||||||
|
|
||||||
builder.Services.AddIdentityCore<User>()
|
builder.Services.AddIdentity<User, IdentityRole<Guid>>(options =>
|
||||||
.AddRoles<IdentityRole<Guid>>()
|
{
|
||||||
|
options.SignIn.RequireConfirmedAccount = true;
|
||||||
|
options.Password.RequiredLength = 8;
|
||||||
|
})
|
||||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||||
.AddApiEndpoints();
|
.AddSignInManager();
|
||||||
|
|
||||||
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||||||
options.UseNpgsql(builder.Configuration.GetConnectionString("Database")));
|
options.UseNpgsql(builder.Configuration.GetConnectionString("Database")));
|
||||||
|
|
||||||
|
// CORS
|
||||||
|
builder.Services.AddCors(options =>
|
||||||
|
{
|
||||||
|
options.AddPolicy("ApiPolicy", builder =>
|
||||||
|
{
|
||||||
|
builder.WithOrigins("http://localhost")
|
||||||
|
.AllowAnyMethod()
|
||||||
|
.AllowAnyHeader();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen(options =>
|
builder.Services.AddSwaggerGen(options =>
|
||||||
@ -39,11 +52,11 @@ if (app.Environment.IsDevelopment())
|
|||||||
app.MapScalarApiReference();
|
app.MapScalarApiReference();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.UseCors("ApiPolicy");
|
||||||
app.MapIdentityApi<User>();
|
app.UseAuthentication();
|
||||||
|
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
/*app.MapIdentityApi<User>();*/
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
Loading…
Reference in New Issue
Block a user