aboutsummaryrefslogtreecommitdiff
path: root/API/Startup.cs
diff options
context:
space:
mode:
Diffstat (limited to 'API/Startup.cs')
-rw-r--r--API/Startup.cs60
1 files changed, 60 insertions, 0 deletions
diff --git a/API/Startup.cs b/API/Startup.cs
index b3d0769..49dd794 100644
--- a/API/Startup.cs
+++ b/API/Startup.cs
@@ -5,7 +5,17 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
+<<<<<<< HEAD
+using Microsoft.OpenApi.Models;
+using Data.Models.Classes;
+using Data.Models.Options;
+using Microsoft.IdentityModel.Tokens;
+using Microsoft.AspNetCore.Authentication.JwtBearer;
+using System.Text;
+using System.Threading.Tasks;
+=======
using API.Extensions;
+>>>>>>> 8bd7295dc4694c1c0ed6fbc05d390223bfc4ef05
namespace API
{
@@ -23,6 +33,56 @@ namespace API
{
services.AddControllers();
+ services.AddDbContext<DevHiveContext>(options =>
+ options.UseNpgsql(Configuration.GetConnectionString("DEV")));
+
+ services.AddIdentity<User, UserRoles>()
+ .AddEntityFrameworkStores<DevHiveContext>();
+
+ services.Configure<IdentityOptions>(options =>
+ {
+ options.User.RequireUniqueEmail = true;
+
+ options.Password.RequiredLength = 5;
+ });
+
+ services.AddSingleton<JWTOptions>(
+ new JWTOptions(Configuration.GetSection("AppSettings").GetSection("Secret").Value));
+
+ // Get key from appsettings.json
+ var key = Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings").GetSection("Secret").Value);
+ // Setup Jwt Authentication
+ services.AddAuthentication(x =>
+ {
+ x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
+ x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
+ })
+ .AddJwtBearer(x =>
+ {
+ x.Events = new JwtBearerEvents
+ {
+ OnTokenValidated = context =>
+ {
+ // TODO: add more authentication
+ return Task.CompletedTask;
+ }
+ };
+ x.RequireHttpsMetadata = false;
+ x.SaveToken = true;
+ x.TokenValidationParameters = new TokenValidationParameters
+ {
+ ValidateIssuerSigningKey = true,
+ IssuerSigningKey = new SymmetricSecurityKey(key),
+ ValidateIssuer = false,
+ ValidateAudience = false
+ };
+ });
+
+ services.AddSwaggerGen(c =>
+ {
+ c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
+ });
+
services.DatabaseConfiguration(Configuration);
services.SwaggerConfiguration();
services.JWTConfiguration();