using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Text;

namespace MeteoWebApp.Helpers
{
    public class LoginAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            byte[] sessionKey;

            if (filterContext.HttpContext.Session.TryGetValue("Authorized", out sessionKey))
            {
                if (Encoding.UTF8.GetString(sessionKey) == "true")
                {
                    // User is authorized - do nothing
                    return;
                }
            }
            // User not authorized redirect to default login page
            string loginUrl = "/Home/Login";
            filterContext.Result = new RedirectResult(loginUrl, true);
        }
    }
}
