Add frontend login
Some checks failed
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is failing

This commit is contained in:
Kasper Juul Hermansen 2021-11-16 23:07:51 +01:00
parent 9af64e859a
commit cb43207502
Signed by: kjuulh
GPG Key ID: DCD9397082D97069
5 changed files with 37 additions and 1 deletions

View File

@ -1,5 +1,7 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Todo.Core.Interfaces.Persistence;
@ -25,6 +27,35 @@ namespace Todo.Api.Controllers
return Ok(user);
}
[HttpGet("login")]
public async Task<IActionResult> Login([FromQuery] string returnUrl)
{
var props = new AuthenticationProperties
{
RedirectUri = Url.Action(nameof(Callback)),
Items =
{
{"returnUrl", returnUrl}
}
};
return Challenge(props);
}
[HttpGet]
public async Task<IActionResult> Callback()
{
// read external identity from the temporary cookie
var result =
await HttpContext.AuthenticateAsync("oidc");
if (result?.Succeeded != true)
{
throw new Exception("External authentication error");
}
var returnUrl = result.Properties?.Items["returnUrl"] ?? "~/";
return Redirect(returnUrl);
}
public record RegisterUserRequest
{

View File

@ -1,10 +1,12 @@
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using Todo.Api.Hubs.Models;
using Todo.Core.Interfaces.Persistence;
namespace Todo.Api.Hubs
{
[Authorize]
public class TodoHub : Hub
{
private readonly ITodoRepository _todoRepository;

View File

@ -24,7 +24,7 @@
"MONGODB__Port": "27017",
"GITEA__Url": "https://git.front.kjuulh.io",
"GITEA__ClientId": "6982ef4f-cfc1-431c-a442-fad98355a059",
"GITEA__": "stabKPEZ6di0VfPjYT4rb0jRGLA2gPSd2NEkGoBi0xLO"
"GITEA__ClientSecret": "hXUrUz5xPhC7IE3dQKft9lHboBEwhNC8yFjSzKgF9Nyr"
}
}
}

View File

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
namespace Todo.Infrastructure;

View File

@ -70,6 +70,8 @@ export const SocketProvider: FC = (props) => {
connection.start().then(() => {
setConn(connection);
}).catch(e => {
window.location.href = `${serverUrl}/api/auth/login?returnUrl=${window.location.href}`
});
}, []);