diff --git a/back/src/Kyoo.Authentication/Kyoo.Authentication.csproj b/back/src/Kyoo.Authentication/Kyoo.Authentication.csproj index b273ee03..9ce67f54 100644 --- a/back/src/Kyoo.Authentication/Kyoo.Authentication.csproj +++ b/back/src/Kyoo.Authentication/Kyoo.Authentication.csproj @@ -10,6 +10,7 @@ + diff --git a/back/src/Kyoo.Authentication/Views/AuthApi.cs b/back/src/Kyoo.Authentication/Views/AuthApi.cs index caf7a5f8..59fd987f 100644 --- a/back/src/Kyoo.Authentication/Views/AuthApi.cs +++ b/back/src/Kyoo.Authentication/Views/AuthApi.cs @@ -19,7 +19,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Net.Http; using System.Net.Http.Json; using System.Text; @@ -32,13 +31,11 @@ using Kyoo.Abstractions.Models.Permissions; using Kyoo.Abstractions.Models.Utils; using Kyoo.Authentication.Models; using Kyoo.Authentication.Models.DTO; +using Kyoo.Core.Controllers; using Kyoo.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Newtonsoft.Json.Linq; using static Kyoo.Abstractions.Models.Utils.Constants; using BCryptNet = BCrypt.Net.BCrypt; @@ -51,7 +48,7 @@ namespace Kyoo.Authentication.Views [Route("auth")] [ApiDefinition("Authentication", Group = UsersGroup)] public class AuthApi( - IRepository users, + UserRepository users, ITokenController tokenController, IThumbnailsManager thumbs, IHttpClientFactory clientFactory, @@ -184,11 +181,8 @@ namespace Kyoo.Authentication.Views User newUser = new(); if (profile.Email is not null) newUser.Email = profile.Email; - if (profile.Username is not null) - newUser.Username = profile.Username; - else if (profile.Name is not null) - newUser.Username = profile.Name; - else + string? username = profile.Username ?? profile.Name; + if (username is null) { return BadRequest( new RequestError( @@ -196,12 +190,11 @@ namespace Kyoo.Authentication.Views ) ); } + newUser.Username = username; newUser.Slug = Utils.Utility.ToSlug(newUser.Username); newUser.ExternalId.Add(provider, extToken); - User? user = await users.GetOrDefault( - new Filter.Lambda(x => x.ExternalId[provider].Id == extToken.Id) - ); + User? user = await users.GetByExternalId(provider, extToken.Id); if (user == null) user = await users.Create(newUser); return new JwtToken( diff --git a/back/src/Kyoo.Core/Controllers/Repositories/UserRepository.cs b/back/src/Kyoo.Core/Controllers/Repositories/UserRepository.cs index c351a420..7cd219f9 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/UserRepository.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/UserRepository.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; +using System.Data.Common; using System.Linq; using System.Threading.Tasks; using Kyoo.Abstractions.Controllers; @@ -35,10 +36,12 @@ namespace Kyoo.Core.Controllers; /// /// Create a new /// -/// The database handle to use -/// The thumbnail manager used to store images. -public class UserRepository(DatabaseContext database, IThumbnailsManager thumbs) - : LocalRepository(database, thumbs) +public class UserRepository( + DatabaseContext database, + DbConnection db, + SqlVariableContext context, + IThumbnailsManager thumbs +) : LocalRepository(database, thumbs) { /// /// The database handle @@ -84,4 +87,26 @@ public class UserRepository(DatabaseContext database, IThumbnailsManager thumbs) await _database.SaveChangesAsync(); await base.Delete(obj); } + + public Task GetByExternalId(string provider, string id) + { + // language=PostgreSQL + return db.QuerySingle( + $""" + select + u.* -- User as u + /* includes */ + from + users as u + where + u.external_id->{provider}->>'id' = {id} + """, + new() { ["u"] = typeof(User) }, + (items) => (items[0] as User)!, + context, + null, + null, + null + ); + } }