mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Allow existing accounts to be linked
This commit is contained in:
parent
079a2cdbe3
commit
a6c3ab33b1
@ -110,4 +110,10 @@ public class OidcController(
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<User> LinkAccount(Guid userId, string provider, string code)
|
||||
{
|
||||
(_, ExternalToken extToken) = await _TranslateCode(provider, code);
|
||||
return await users.AddExternalToken(userId, provider, extToken);
|
||||
}
|
||||
}
|
||||
|
@ -175,7 +175,10 @@ namespace Kyoo.Authentication.Views
|
||||
if (code == null)
|
||||
return BadRequest(new RequestError("Invalid code."));
|
||||
|
||||
User user = await oidc.LoginViaCode(provider, code);
|
||||
Guid? userId = User.GetId();
|
||||
User user = userId.HasValue
|
||||
? await oidc.LinkAccount(userId.Value, provider, code)
|
||||
: await oidc.LoginViaCode(provider, code);
|
||||
return new JwtToken(
|
||||
tokenController.CreateAccessToken(user, out TimeSpan expireIn),
|
||||
await tokenController.CreateRefreshToken(user),
|
||||
|
@ -23,6 +23,7 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Abstractions.Models.Exceptions;
|
||||
using Kyoo.Abstractions.Models.Permissions;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
using Kyoo.Postgresql;
|
||||
@ -109,4 +110,12 @@ public class UserRepository(
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
public async Task<User> AddExternalToken(Guid userId, string provider, ExternalToken token)
|
||||
{
|
||||
User user = await GetWithTracking(userId);
|
||||
user.ExternalId[provider] = token;
|
||||
await database.SaveChangesAsync();
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user