From 61e04f40d39b09f3e11fd2f7a50890481a8cf22a Mon Sep 17 00:00:00 2001 From: Lightczx <1686188646@qq.com> Date: Tue, 6 Feb 2024 14:09:47 +0800 Subject: [PATCH] add multiple passport api --- .../GithubAuthorizationController.cs | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/GithubAuthorizationController.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/GithubAuthorizationController.cs index b84a421..215bacb 100644 --- a/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/GithubAuthorizationController.cs +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/GithubAuthorizationController.cs @@ -33,7 +33,7 @@ public GithubAuthorizationController(IServiceProvider serviceProvider) } [Authorize] - [HttpGet("RedirectLogin")] + [HttpGet("Github/RedirectLogin")] public async Task RedirectLoginAsync() { HutaoUser? user = await this.GetUserAsync(appDbContext.Users).ConfigureAwait(false); @@ -142,7 +142,25 @@ public async Task HandleAuthorizationCallbackAsync([FromQuery(Nam // Authorized string token = passportService.CreateTokenByUserId(identity.UserId); - return Redirect($"https://passport.snapgenshin.com/api/login?token={token}"); + return Redirect($"https://passport.snapgenshin.cn/api/login?token={token}"); + } + + [Authorize] + [HttpGet("Github/UnAuthorize")] + public async Task UnAuthorizeAsync() + { + int userId = this.GetUserId(); + int count = await appDbContext.GithubIdentities.Where(g => g.UserId == userId).ExecuteDeleteAsync().ConfigureAwait(false); + return Response.Success("操作完成", new() { Count = count }); + } + + [Authorize] + [HttpGet("Github/AuthorizationStatus")] + public async Task GetAuthorizationStatusAsync() + { + int userId = this.GetUserId(); + bool isAuthorized = await appDbContext.GithubIdentities.AnyAsync(g => g.UserId == userId).ConfigureAwait(false); + return Response.Success("查询成功", new() { IsAuthorized = isAuthorized }); } private string EncryptState(string state) @@ -202,7 +220,7 @@ private string DecryptState(string state) private RedirectResult RedirectToError(ReturnCode errorCode) { - return Redirect($"https://passport.snapgenshin.com/auth/error?code={errorCode:D}"); + return Redirect($"https://passport.snapgenshin.cn/auth/error?code={errorCode:D}"); } private sealed class UserIdentity @@ -239,4 +257,14 @@ private sealed class GithubUserResponse [JsonPropertyName("node_id")] public string NodeId { get; set; } = default!; } + + private sealed class UnAuthorizeResult + { + public int Count { get; set; } + } + + private sealed class IsAuthorizedResult + { + public bool IsAuthorized { get; set; } + } } \ No newline at end of file