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 6e6a499..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.pdb b/Knots/bin/Debug/net8.0/Knots.pdb index e0df184..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/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache b/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache index 6681d87..6e6fa9a 100644 --- a/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache +++ b/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -dab059269bab79457b7504e70cb873c79efe1561e6d178c96e5a58f0a7d08788 +bab9e1ed6ec5ae3422f4377cfa8a1795b3c9881cbfdada74a84426e15b5819ff diff --git a/Knots/obj/Debug/net8.0/Knots.csproj.FileListAbsolute.txt b/Knots/obj/Debug/net8.0/Knots.csproj.FileListAbsolute.txt index 573cb12..178a047 100644 --- a/Knots/obj/Debug/net8.0/Knots.csproj.FileListAbsolute.txt +++ b/Knots/obj/Debug/net8.0/Knots.csproj.FileListAbsolute.txt @@ -518,4 +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\Carte\RiderProjects\Knots\Knots\bin\Debug\net8.0\System.Net.WebSockets.WebSocketProtocol.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 6e6a499..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 e0df184..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/ref/Knots.dll b/Knots/obj/Debug/net8.0/ref/Knots.dll index 69e9da9..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 69e9da9..5e72baf 100644 Binary files a/Knots/obj/Debug/net8.0/refint/Knots.dll and b/Knots/obj/Debug/net8.0/refint/Knots.dll differ