From 12fef7cbaf2073f9cc349ed765ea140be0259d8e Mon Sep 17 00:00:00 2001 From: Paweł Bernaciak Date: Sat, 21 Oct 2023 11:55:33 +0200 Subject: Basic API auth and Google login --- backend/Elements.Backend/Program.cs | 53 +++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'backend/Elements.Backend/Program.cs') 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(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(); -- cgit v1.2.3