diff --git a/Knots/Hubs/ChatHub.cs b/Knots/Hubs/ChatHub.cs index 37ca56e..0d5da9b 100644 --- a/Knots/Hubs/ChatHub.cs +++ b/Knots/Hubs/ChatHub.cs @@ -4,7 +4,7 @@ using Microsoft.AspNetCore.SignalR; namespace Knots.Hubs; [Authorize] -public class ChatHub : Hub +public class ChatHub(KnotsDbContext db, AutoMapper.IMapper mapper) : Hub { // Rejoindre une conversation (room) public async Task JoinConversation(string conversationId) @@ -21,14 +21,18 @@ public class ChatHub : Hub // Envoyer un message à une conversation public async Task SendMessage(string conversationId, string content) { - var message = new + var message = new Models.Message { - SenderId = Context.UserIdentifier, - Content = content, - SentAt = DateTime.UtcNow + // adapte aux noms réels de ton modèle (AuthorId, Contenu, Date...) + AuthorId = int.Parse(Context.UserIdentifier!), + Contenu = content, + Date = DateTime.UtcNow, + DiscussionId = int.Parse(conversationId) }; - // Diffuse à tous les membres de la conversation + db.Messages.Add(message); + await db.SaveChangesAsync(); + await Clients.Group(conversationId).SendAsync("ReceiveMessage", message); } diff --git a/Knots/Models/Message.cs b/Knots/Models/Message.cs index ba2d05c..f100cf5 100644 --- a/Knots/Models/Message.cs +++ b/Knots/Models/Message.cs @@ -9,6 +9,8 @@ public class Message [Required] public DateTime Date { get; set; } [Required] public Boolean Type { get; set; } + public int AuthorId { get; set; } + public int UserId { get; set; } public User User { get; set; } = null!; diff --git a/Knots/Program.cs b/Knots/Program.cs index 4bcb268..f81ecc8 100644 --- a/Knots/Program.cs +++ b/Knots/Program.cs @@ -21,7 +21,8 @@ builder.Services.AddCors(options => policyBuilder .WithOrigins("http://localhost:5250", "http://localhost:4200") .WithMethods("GET", "POST", "PUT", "PATCH", "DELETE") - .AllowAnyHeader(); + .AllowAnyHeader() + .AllowCredentials(); }); }); @@ -47,7 +48,24 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) ValidAudience = builder.Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!)) + }; + + options.Events = new JwtBearerEvents + { + OnMessageReceived = context => + { + var accessToken = context.Request.Query["access_token"]; + var path = context.HttpContext.Request.Path; + if (!string.IsNullOrEmpty(accessToken) && + path.StartsWithSegments("/hubs")) + { + context.Token = accessToken; + } + return Task.CompletedTask; + } + }; + }); builder.Services.AddAuthorization(); @@ -58,8 +76,6 @@ builder.Services.AddAutoMapper(cfg => { }, typeof(Program).Assembly); // On construit l'application en lui donnant vie WebApplication app = builder.Build(); -app.MapHub("hubs/chat"); - app.UseCors(); app.UseHttpsRedirection(); @@ -73,5 +89,6 @@ app.UseAuthentication() } ).UseSwaggerGen(); +app.MapHub("hubs/chat"); app.Run(); \ No newline at end of file diff --git a/Knots/bin/Debug/net8.0/Knots.dll b/Knots/bin/Debug/net8.0/Knots.dll index 94d3a74..ba7ec4c 100644 Binary files a/Knots/bin/Debug/net8.0/Knots.dll and b/Knots/bin/Debug/net8.0/Knots.dll differ diff --git a/Knots/bin/Debug/net8.0/Knots.exe b/Knots/bin/Debug/net8.0/Knots.exe index 562fd1b..ed5d014 100644 Binary files a/Knots/bin/Debug/net8.0/Knots.exe and b/Knots/bin/Debug/net8.0/Knots.exe differ diff --git a/Knots/bin/Debug/net8.0/Knots.pdb b/Knots/bin/Debug/net8.0/Knots.pdb index a7dde11..3c9398e 100644 Binary files a/Knots/bin/Debug/net8.0/Knots.pdb and b/Knots/bin/Debug/net8.0/Knots.pdb differ diff --git a/Knots/bin/Debug/net8.0/System.Net.WebSockets.WebSocketProtocol.dll b/Knots/bin/Debug/net8.0/System.Net.WebSockets.WebSocketProtocol.dll new file mode 100644 index 0000000..f3832ed Binary files /dev/null and b/Knots/bin/Debug/net8.0/System.Net.WebSockets.WebSocketProtocol.dll differ diff --git a/Knots/obj/Debug/net8.0/Knots.AssemblyInfo.cs b/Knots/obj/Debug/net8.0/Knots.AssemblyInfo.cs index bea9ec1..4243247 100644 --- a/Knots/obj/Debug/net8.0/Knots.AssemblyInfo.cs +++ b/Knots/obj/Debug/net8.0/Knots.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("Knots")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0b9e01c92578a51eb81cd673aaae95ac56d7c4af")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4f994ba183ab4a079dd7e762d3b7cacca26f8f75")] [assembly: System.Reflection.AssemblyProductAttribute("Knots")] [assembly: System.Reflection.AssemblyTitleAttribute("Knots")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache b/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache index 8ab89e9..e2541b2 100644 --- a/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache +++ b/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache @@ -1 +1 @@ -b62edb6f2119aec2fe98bcce877c4ef16135c025b03ceefc31d7509622b283ee +aa1c1119a88f3e3b5738f6ea6f67686474d5c6d7e7b2612859a07f3ee65ef199 diff --git a/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache b/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache index fce52b4..6e6fa9a 100644 --- a/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache +++ b/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -6edaa06a6de0e0e58dbcac8a2a5db11b7c3fa5582c067ae51bfae956cf647dc5 +bab9e1ed6ec5ae3422f4377cfa8a1795b3c9881cbfdada74a84426e15b5819ff diff --git a/Knots/obj/Debug/net8.0/Knots.csproj.FileListAbsolute.txt b/Knots/obj/Debug/net8.0/Knots.csproj.FileListAbsolute.txt index 5d29bfd..178a047 100644 --- a/Knots/obj/Debug/net8.0/Knots.csproj.FileListAbsolute.txt +++ b/Knots/obj/Debug/net8.0/Knots.csproj.FileListAbsolute.txt @@ -518,3 +518,4 @@ C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\refint\Knots.dll C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\Knots.pdb C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\Knots.genruntimeconfig.cache C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\ref\Knots.dll +C:\Users\dogge\RiderProjects\Knots\Knots\bin\Debug\net8.0\System.Net.WebSockets.WebSocketProtocol.dll diff --git a/Knots/obj/Debug/net8.0/Knots.dll b/Knots/obj/Debug/net8.0/Knots.dll index 0352e3e..ba7ec4c 100644 Binary files a/Knots/obj/Debug/net8.0/Knots.dll and b/Knots/obj/Debug/net8.0/Knots.dll differ diff --git a/Knots/obj/Debug/net8.0/Knots.pdb b/Knots/obj/Debug/net8.0/Knots.pdb index a7dde11..3c9398e 100644 Binary files a/Knots/obj/Debug/net8.0/Knots.pdb and b/Knots/obj/Debug/net8.0/Knots.pdb differ diff --git a/Knots/obj/Debug/net8.0/apphost.exe b/Knots/obj/Debug/net8.0/apphost.exe index f2acea3..ed5d014 100644 Binary files a/Knots/obj/Debug/net8.0/apphost.exe and b/Knots/obj/Debug/net8.0/apphost.exe differ diff --git a/Knots/obj/Debug/net8.0/ref/Knots.dll b/Knots/obj/Debug/net8.0/ref/Knots.dll index 04beb3e..5e72baf 100644 Binary files a/Knots/obj/Debug/net8.0/ref/Knots.dll and b/Knots/obj/Debug/net8.0/ref/Knots.dll differ diff --git a/Knots/obj/Debug/net8.0/refint/Knots.dll b/Knots/obj/Debug/net8.0/refint/Knots.dll index 04beb3e..5e72baf 100644 Binary files a/Knots/obj/Debug/net8.0/refint/Knots.dll and b/Knots/obj/Debug/net8.0/refint/Knots.dll differ