-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
172 lines (147 loc) · 5.16 KB
/
Copy pathProgram.cs
File metadata and controls
172 lines (147 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using ClinicBooking.API.Contracts;
using ClinicBooking.API.Data;
using ClinicBooking.API.Entities;
using ClinicBooking.API.Repository;
using ClinicBooking.API.Services;
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Scalar.AspNetCore;
using System.Text;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
// Database
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(connectionString);
});
// identity
builder.Services.AddIdentity<ApplicationUser, IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
//jwt
var jwtSettings = builder.Configuration.GetSection("JWT");
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSettings["Issuer"],
ValidAudience = jwtSettings["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(jwtSettings["Key"]))
};
// Read JWT from HttpOnly cookie instead of Authorization header
o.Events = new Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerEvents
{
OnMessageReceived = context =>
{
var token = context.Request.Cookies["access_token"];
if (!string.IsNullOrEmpty(token))
context.Token = token;
return Task.CompletedTask;
}
};
});
// Controllers + JSON options
builder.Services
.AddControllers(options =>
{
options.Filters.Add<ClinicBooking.API.Filters.ValidationFilter>();
})
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters
.Add(new JsonStringEnumConverter());
});
// OpenAPI (.NET 10 native)
builder.Services.AddOpenApi(options =>
{
options.AddDocumentTransformer((document, context, ct) =>
{
document.Info.Title = "ClinicBooking API";
document.Info.Version = "v1";
return Task.CompletedTask;
});
});
// FluentValidation
builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
// Repositories & UnitOfWork
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
// Services
builder.Services.AddScoped<IAppointmentService, AppointmentService>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IEmailService, EmailService>();
// Registers the policy
builder.Services.AddCors(options => {
options.AddPolicy("Frontend", policy => {
policy.WithOrigins("http://localhost:3000")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials(); // ← critical for HttpOnly cookies
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi(); // serves /openapi/v1.json
app.MapScalarApiReference(options => // serves /scalar/v1
{
options.Title = "ClinicBooking API";
options.Theme = Scalar.AspNetCore.ScalarTheme.Purple;
});
}
app.UseMiddleware<ClinicBooking.API.Middleware.GlobalExceptionMiddleware>();
app.UseHttpsRedirection();
// Uses it in the pipeline (before auth)
app.UseCors("Frontend");
app.UseStaticFiles(); // serves wwwroot/certificates/
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// Seed roles
using (var scope = app.Services.CreateScope())
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole<Guid>>>();
foreach (var role in new[] { "Admin", "Doctor", "Patient" })
{
if (!await roleManager.RoleExistsAsync(role))
await roleManager.CreateAsync(new IdentityRole<Guid>(role));
}
// Seed default Admin user
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
const string adminEmail = "admin@clinicsync.com";
const string adminPassword = "Admin@123456";
var adminUser = await userManager.FindByEmailAsync(adminEmail);
if (adminUser == null)
{
adminUser = new ApplicationUser
{
Id = Guid.NewGuid(),
FullName = "Super Admin",
Email = adminEmail,
UserName = adminEmail,
EmailConfirmed = true,
CreatedAt = DateTime.UtcNow
};
var result = await userManager.CreateAsync(adminUser, adminPassword);
if (result.Succeeded)
await userManager.AddToRoleAsync(adminUser, "Admin");
}
}
app.Run();