Merge branch 'master' into dev/multiple-notes

This commit is contained in:
Ryan Peters
2023-05-17 21:22:48 -04:00
9 changed files with 122 additions and 9 deletions

View File

@ -0,0 +1,54 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
namespace BinaryDad.Notes.Controllers
{
public class LoginController : Controller
{
[Route("login")]
public IActionResult Login()
{
return View();
}
[ActionName(nameof(Login))]
[Route("login")]
[HttpPost]
public async Task<IActionResult> LoginPost([Required] string passphrase, string returnUrl)
{
if (ModelState.IsValid)
{
var appPassphrase = Environment.GetEnvironmentVariable("APP_PASSPHRASE");
if (passphrase == appPassphrase)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.GivenName, "Ryan")
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity));
return Redirect(returnUrl);
}
ModelState.AddModelError("", "Invalid login");
}
return View();
}
[Route("logout")]
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync();
return Redirect("/");
}
}
}

View File

@ -1,14 +1,16 @@
using BinaryDad.Notes.Models;
using BinaryDad.Notes.Models;
using BinaryDad.Notes.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BinaryDad.Notes.Controllers;
public class HomeController : Controller
[Authorize]
public class NoteController : Controller
{
private readonly INoteService noteService;
public HomeController(INoteService noteService)
public NoteController(INoteService noteService)
{
this.noteService = noteService;
}