Implemented new discord linking system
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Logging.Net;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moonlight.App.Services;
|
||||
using Moonlight.App.Services.Sessions;
|
||||
|
||||
namespace Moonlight.App.Http.Controllers.Api.Moonlight;
|
||||
|
||||
@@ -11,12 +12,41 @@ public class OAuth2Controller : Controller
|
||||
private readonly UserService UserService;
|
||||
private readonly OAuth2Service OAuth2Service;
|
||||
private readonly DateTimeService DateTimeService;
|
||||
private readonly IdentityService IdentityService;
|
||||
|
||||
public OAuth2Controller(UserService userService, OAuth2Service oAuth2Service, DateTimeService dateTimeService)
|
||||
public OAuth2Controller(
|
||||
UserService userService,
|
||||
OAuth2Service oAuth2Service,
|
||||
DateTimeService dateTimeService,
|
||||
IdentityService identityService)
|
||||
{
|
||||
UserService = userService;
|
||||
OAuth2Service = oAuth2Service;
|
||||
DateTimeService = dateTimeService;
|
||||
IdentityService = identityService;
|
||||
}
|
||||
|
||||
[HttpGet("{id}/start")]
|
||||
public async Task<ActionResult> Start([FromRoute] string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (OAuth2Service.Providers.ContainsKey(id))
|
||||
{
|
||||
return Redirect(await OAuth2Service.GetUrl(id));
|
||||
}
|
||||
|
||||
Logger.Warn($"Someone tried to start an oauth2 flow using the id '{id}' which is not registered");
|
||||
|
||||
return Redirect("/");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Warn($"Error starting oauth2 flow for id: {id}");
|
||||
Logger.Warn(e);
|
||||
|
||||
return Redirect("/");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
@@ -24,6 +54,18 @@ public class OAuth2Controller : Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentUser = await IdentityService.Get();
|
||||
|
||||
if (currentUser != null)
|
||||
{
|
||||
if (await OAuth2Service.CanBeLinked(id))
|
||||
{
|
||||
await OAuth2Service.LinkToUser(id, currentUser, code);
|
||||
|
||||
return Redirect("/profile");
|
||||
}
|
||||
}
|
||||
|
||||
var user = await OAuth2Service.HandleCode(id, code);
|
||||
|
||||
Response.Cookies.Append("token", await UserService.GenerateToken(user), new()
|
||||
|
||||
Reference in New Issue
Block a user