Skip to content

Commit

Permalink
Merge pull request #15 from moheladwy/AddFirstPageInClient
Browse files Browse the repository at this point in the history
Update CORS Configurations for the api
  • Loading branch information
moheladwy authored Jan 1, 2025
2 parents dc5837f + 1358adb commit 28521e7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 4 additions & 2 deletions server/Todo.Api/Configurations/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Todo.Api.Configurations;

public static class Constants
{
public const string ClientCrossOriginPolicyName = "AllowReactApp";
public const string ClientCrossOriginPolicyURL = "http://localhost:5173";
public const string ClientCrossOriginPolicyProductionName = "AllowProductionReactApp";
public const string ClientCrossOriginPolicyDevName = "AllowDevelopmentReactApp";
public const string ClientCrossOriginPolicyDevURL = "http://localhost:5173";
public const string ClientCrossOriginPolicyProductionURL = "https://todo.aladawy.duckdns.org";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static void InitializeConfigurations(this WebApplication app)
else
app.ProductionMode();

app.UseCors(Constants.ClientCrossOriginPolicyName);
app.UseHttpsRedirection();
app.UseHttpLogging();
app.UseRouting();
Expand Down Expand Up @@ -64,6 +63,7 @@ private static async void UseAutoMigrationAtStartup(this WebApplication app)
private static void DevelopmentMode(this WebApplication app)
{
app.UseDeveloperExceptionPage();
app.UseCors(Constants.ClientCrossOriginPolicyDevName);
}

/// <summary>
Expand All @@ -75,5 +75,6 @@ private static void DevelopmentMode(this WebApplication app)
private static void ProductionMode(this WebApplication app)
{
app.UseExceptionHandler("/error");
app.UseCors(Constants.ClientCrossOriginPolicyProductionName);
}
}
12 changes: 10 additions & 2 deletions server/Todo.Api/Configurations/StartupBuilderConfigurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ public static void AddCorsService(this WebApplicationBuilder builder)
{
builder.Services.AddCors(options =>
{
options.AddPolicy(Constants.ClientCrossOriginPolicyName, builder =>
options.AddPolicy(Constants.ClientCrossOriginPolicyDevName, builder =>
{
builder
.WithOrigins(Constants.ClientCrossOriginPolicyURL)
.WithOrigins(Constants.ClientCrossOriginPolicyDevURL)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
options.AddPolicy(Constants.ClientCrossOriginPolicyProductionName, builder =>
{
builder
.WithOrigins(Constants.ClientCrossOriginPolicyProductionURL)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
Expand Down

0 comments on commit 28521e7

Please sign in to comment.