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 System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Todo.Core.Interfaces.Persistence; using Todo.Core.Interfaces.Persistence;
@ -26,6 +28,35 @@ namespace Todo.Api.Controllers
return Ok(user); 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 public record RegisterUserRequest
{ {
[Required] public string Email { get; init; } [Required] public string Email { get; init; }

View File

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

View File

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

View File

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

View File

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