Add unlink route

This commit is contained in:
Zoe Roux 2024-03-08 21:24:51 +01:00
parent a6c3ab33b1
commit c0acf1c1a0
2 changed files with 25 additions and 5 deletions

View File

@ -19,9 +19,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
@ -186,6 +183,23 @@ namespace Kyoo.Authentication.Views
);
}
/// <summary>
/// Unlink account
/// </summary>
/// <remarks>
/// Unlink your account from an external account.
/// </remarks>
/// <param name="provider">The provider code.</param>
/// <returns>Your updated user account</returns>
[HttpDelete("login/{provider}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[UserOnly]
public Task<User> UnlinkAccount(string provider)
{
Guid id = User.GetIdOrThrow();
return users.DeleteExternalToken(id, provider);
}
/// <summary>
/// Login.
/// </summary>
@ -257,7 +271,6 @@ namespace Kyoo.Authentication.Views
/// <returns>A new access and refresh token.</returns>
/// <response code="403">The given refresh token is invalid.</response>
[HttpGet("refresh")]
[UserOnly]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden, Type = typeof(RequestError))]
public async Task<ActionResult<JwtToken>> Refresh([FromQuery] string token)

View File

@ -23,7 +23,6 @@ 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;
@ -118,4 +117,12 @@ public class UserRepository(
await database.SaveChangesAsync();
return user;
}
public async Task<User> DeleteExternalToken(Guid userId, string provider)
{
User user = await GetWithTracking(userId);
user.ExternalId.Remove(provider);
await database.SaveChangesAsync();
return user;
}
}