Skip to content

Commit

Permalink
add multiple passport api
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Feb 6, 2024
1 parent 4724b18 commit 61e04f4
Showing 1 changed file with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public GithubAuthorizationController(IServiceProvider serviceProvider)
}

[Authorize]
[HttpGet("RedirectLogin")]
[HttpGet("Github/RedirectLogin")]
public async Task<IActionResult> RedirectLoginAsync()
{
HutaoUser? user = await this.GetUserAsync(appDbContext.Users).ConfigureAwait(false);
Expand Down Expand Up @@ -142,7 +142,25 @@ public async Task<IActionResult> 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<IActionResult> UnAuthorizeAsync()
{
int userId = this.GetUserId();
int count = await appDbContext.GithubIdentities.Where(g => g.UserId == userId).ExecuteDeleteAsync().ConfigureAwait(false);
return Response<UnAuthorizeResult>.Success("操作完成", new() { Count = count });
}

[Authorize]
[HttpGet("Github/AuthorizationStatus")]
public async Task<IActionResult> GetAuthorizationStatusAsync()
{
int userId = this.GetUserId();
bool isAuthorized = await appDbContext.GithubIdentities.AnyAsync(g => g.UserId == userId).ConfigureAwait(false);
return Response<IsAuthorizedResult>.Success("查询成功", new() { IsAuthorized = isAuthorized });
}

private string EncryptState(string state)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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; }
}
}

0 comments on commit 61e04f4

Please sign in to comment.