using System.Text.Json;
using XFEExtension.NetCore.ServerInteractive.Attributes;
using XFEExtension.NetCore.ServerInteractive.Models.UserModels;
using XFEExtension.NetCore.ServerInteractive.Utilities.Helpers;
namespace XFEExtension.NetCore.ServerInteractive.Utilities.Server.Services.CoreService;
/// 撤销当前会话或当前用户的全部会话。
public partial class UserLogoutService : ServerCoreUserServiceBase
{
/// 撤销当前不透明会话令牌。
[EntryPoint("user/logout")]
public async Task Logout()
{
var user = User;
EncryptedUserLoginModel? session;
lock (SessionStoreSynchronization.Gate)
{
session = GetEncryptedUserLoginModelFunction().FirstOrDefault(candidate =>
candidate.UserLoginModel.Uid == user.Id && SessionTokenHelper.Matches(Session, candidate.TokenHash));
if (session is not null) RemoveEncryptedUserLoginModelFunction(session);
}
await ReturnArgs.CloseJson(new { revoked = session is not null }, JsonSerializerOptions);
}
/// 撤销当前用户在所有设备上的会话。
[EntryPoint("user/logout/all")]
public async Task LogoutAll()
{
var user = User;
var revoked = 0;
lock (SessionStoreSynchronization.Gate)
{
foreach (var session in GetEncryptedUserLoginModelFunction().Where(candidate =>
candidate.UserLoginModel.Uid == user.Id).ToArray())
{
RemoveEncryptedUserLoginModelFunction(session);
revoked++;
}
}
await ReturnArgs.CloseJson(new { revoked }, JsonSerializerOptions);
}
}