Implement base controller for shared functionality; update existing controllers to inherit from BaseController and adjust user ID handling. Add JWT authentication support and modify item retrieval methods in GameDataController.

This commit is contained in:
2025-02-15 21:57:59 -03:00
parent 6d81ff1564
commit b84599b370
10 changed files with 72 additions and 31 deletions
+23
View File
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using PetCompanion.Data;
using PetCompanion.Repositories;
using PetCompanion.Services;
@@ -12,6 +14,10 @@ namespace PetCompanion
{
var builder = WebApplication.CreateBuilder(args);
// Read Firebase configuration from environment variables
var firebaseAppId = Environment.GetEnvironmentVariable("FIREBASE_APP_ID") ??
throw new InvalidOperationException("FIREBASE_APP_ID environment variable is not set");
// Add services to the container.
builder.Services.AddControllers()
.AddJsonOptions(options =>
@@ -57,6 +63,21 @@ namespace PetCompanion
});
});
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = $"https://securetoken.google.com/{firebaseAppId}";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = $"https://securetoken.google.com/{firebaseAppId}",
ValidateAudience = true,
ValidAudience = firebaseAppId,
ValidateLifetime = true
};
});
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -67,6 +88,8 @@ namespace PetCompanion
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
// After app builder is created