Skip to content

Commit

Permalink
Backend Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasAunvik committed Apr 25, 2024
1 parent 95d6f41 commit 4625295
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
40 changes: 34 additions & 6 deletions Web/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Query;
using System.IO;

namespace CliveBot.Web.Controllers
{
/// <summary>
/// Creates redirect urls and challanges, and signing out
/// </summary>
public class AuthenticationController : Controller
public class AuthenticationController(IConfiguration config) : Controller
{
/// <summary>
/// Signing in to the application trough Cookie
Expand All @@ -18,10 +19,19 @@ public class AuthenticationController : Controller
[HttpGet("~/signin")]
public IActionResult SignIn(string? redirect)
{
var frontEndUrl = config.GetValue<string>("FrontendUrl");

var redirectUri = "/";
if(IsLocalUrl(redirect))
if (frontEndUrl != null)
{
redirectUri = frontEndUrl;
}

if (IsLocalUrl(redirect) && frontEndUrl != null)
{
redirectUri = redirect;
Uri newUri = new(new(frontEndUrl), redirect);

redirectUri = newUri.AbsoluteUri;
}

return Challenge(new AuthenticationProperties { RedirectUri = redirectUri }, "Discord");
Expand All @@ -48,13 +58,31 @@ private static bool IsLocalUrl(string? url)
/// <returns></returns>
[HttpGet("~/signout")]
[HttpPost("~/signout")]
public IActionResult SignOutCurrentUser()
public IActionResult SignOutCurrentUser(string redirect)
{
// Instruct the cookies middleware to delete the local cookie created
// when the user agent is redirected from the external identity provider
// after a successful authentication flow (e.g Google or Facebook).
return SignOut(new AuthenticationProperties { RedirectUri = "/" },
CookieAuthenticationDefaults.AuthenticationScheme);

var frontEndUrl = config.GetValue<string>("FrontendUrl");

var redirectUri = "/";
if (frontEndUrl != null)
{
redirectUri = frontEndUrl;
}

if (IsLocalUrl(redirect) && frontEndUrl != null)
{
Uri newUri = new(new(frontEndUrl), redirect);

redirectUri = newUri.AbsoluteUri;
}

return SignOut(
new AuthenticationProperties { RedirectUri = redirectUri },
CookieAuthenticationDefaults.AuthenticationScheme
);
}
}
}
7 changes: 7 additions & 0 deletions Web/Controllers/CharacterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CliveBot.Web.Policies;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace CliveBot.Web.Controllers
{
Expand All @@ -20,13 +21,17 @@ public class CharacterController : ApiBaseController
/// </summary>
/// <returns>List of Characters</returns>
[HttpGet]

[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ActionResult<List<CharacterDto>>))]
public async Task<List<CharacterDto>> GetAllCharacters()
{
return await Mediator.Send(new CharacterList.Query());
}

[HttpGet("{id}")]

[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ActionResult<CharacterDto>))]
public async Task<CharacterDto> GetCharacter(int id)
{
Expand Down Expand Up @@ -98,6 +103,8 @@ public async Task<ActionResult<CharacterVariantDto>> UpdateVariantPreviewImage(i

// Notes
[HttpGet("{characterId}/notes")]

[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ActionResult<List<CharacterNoteDto>>))]
public async Task<List<CharacterNoteDto>> CreateNote(int characterId, CharacterNoteList.Query query)
{
Expand Down
3 changes: 2 additions & 1 deletion Web/Controllers/SkillController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task<ActionResult<SkillDto>> SearchSkill(string skillName)
[HttpPost]
[ModAuthorize(ManageSkills: true)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SkillDto))]
public async Task<ActionResult<SkillDto>> EditSkill(SkillCreate.Command skill)
public async Task<ActionResult<SkillDto>> CreateSkill(SkillCreate.Command skill)
{
return await Mediator.Send(skill);
}
Expand All @@ -83,6 +83,7 @@ public async Task<ActionResult<SkillDto>> EditSkill(int id, SkillEdit.Command sk
}

[HttpGet("{id}/languages")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<SkillLanguageDto>))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<List<SkillLanguageDto>> GetSkillLanguages(int id)
Expand Down
18 changes: 0 additions & 18 deletions Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
options.AccessDeniedPath = "/error/accessdenied";
options.ClientId = discordClientId;
options.ClientSecret = discordClientSecret;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, (options) =>
{
Expand All @@ -89,23 +88,6 @@
{
options.Cookie.Domain = cookieDomain;
}
options.Events.OnSignedIn = (ctx) => {
if (string.IsNullOrEmpty(frontendUrl))
{
return Task.CompletedTask;
}
ctx.Response.Redirect(frontendUrl);
return Task.CompletedTask;
};
options.Events.OnSigningOut = (ctx) => {
if (string.IsNullOrEmpty(frontendUrl))
{
return Task.CompletedTask;
}
ctx.Response.Redirect(frontendUrl);
return Task.CompletedTask;
};
});
//.AddBearerToken();

Expand Down

0 comments on commit 4625295

Please sign in to comment.