diff options
| author | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-10-21 11:55:33 +0200 |
|---|---|---|
| committer | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-10-21 11:55:33 +0200 |
| commit | 12fef7cbaf2073f9cc349ed765ea140be0259d8e (patch) | |
| tree | 83412d050139bcdbd11eedffa61e1d0c9862dbed /backend/Elements.Backend/Program.cs | |
| parent | 17785848eb53f8d0420f6b6b3be00a0d10be4e1e (diff) | |
Basic API auth and Google login
Diffstat (limited to 'backend/Elements.Backend/Program.cs')
| -rw-r--r-- | backend/Elements.Backend/Program.cs | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/backend/Elements.Backend/Program.cs b/backend/Elements.Backend/Program.cs index 15eacee..50e5255 100644 --- a/backend/Elements.Backend/Program.cs +++ b/backend/Elements.Backend/Program.cs @@ -1,23 +1,54 @@ +using System.Text.Json;
+using Elements.Data;
+using Microsoft.AspNetCore.Authentication.Cookies;
+using Microsoft.AspNetCore.Authentication.Google;
+using Microsoft.EntityFrameworkCore;
+
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
+builder.Services.AddRouting(options => options.LowercaseUrls = true);
+builder.Services.AddControllers().AddJsonOptions(options =>
+{
+ options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
+});
-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())
+if (builder.Environment.IsDevelopment())
{
- app.UseSwagger();
- app.UseSwaggerUI();
+ builder.Services.AddDbContext<ApplicationDbContext>(options =>
+ {
+ options.UseSqlite("Data Source=elements.db");
+ });
}
+builder.Services
+.AddAuthentication(options =>
+{
+ options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
+})
+.AddCookie(options =>
+{
+ options.Events = new()
+ {
+ OnRedirectToLogin = (ctx) =>
+ {
+ ctx.Response.StatusCode = 401;
+ return Task.CompletedTask;
+ },
+ OnRedirectToAccessDenied = (ctx) =>
+ {
+ ctx.Response.StatusCode = 401;
+ return Task.CompletedTask;
+ }
+
+ };
+});
+
+var app = builder.Build();
+
app.UseHttpsRedirection();
+app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
|
