Files
mixtape/zero.Web/Controllers/AuthenticationController.cs
T

62 lines
1.4 KiB
C#
Raw Normal View History

2020-04-15 15:13:38 +02:00
using Microsoft.AspNetCore.Mvc;
2020-04-08 13:07:15 +02:00
using System.Threading.Tasks;
using zero.Core;
using zero.Core.Api;
2020-04-15 15:13:38 +02:00
using zero.Core.Entities;
2020-04-16 00:56:22 +02:00
using zero.Core.Identity;
2020-04-15 15:13:38 +02:00
using zero.Web.Models;
2020-04-08 13:07:15 +02:00
namespace zero.Web.Controllers
{
2020-04-15 15:13:38 +02:00
[ZeroAuthorize(false)]
2020-04-08 13:07:15 +02:00
public class AuthenticationController : BackofficeController
{
private IAuthenticationApi Api { get; set; }
public AuthenticationController(IZeroConfiguration config, IAuthenticationApi api) : base(config)
{
Api = api;
}
2020-05-11 11:30:56 +02:00
/// <summary>
/// Returns the current user
/// </summary>
public async Task<IActionResult> GetUser()
{
return await As<User, UserEditModel>(await Api.GetUser());
}
2020-04-08 13:07:15 +02:00
/// <summary>
2020-04-15 15:13:38 +02:00
/// If a user is logged in
/// </summary>
public IActionResult IsLoggedIn()
2020-04-08 13:07:15 +02:00
{
2020-04-15 15:13:38 +02:00
return Json(EntityResult.Maybe(Api.IsLoggedIn()));
2020-04-08 13:07:15 +02:00
}
/// <summary>
/// Tries a login for a user with username/password
/// </summary>
2020-04-15 15:13:38 +02:00
[HttpPost]
public async Task<IActionResult> LoginUser([FromBody] LoginModel model)
2020-04-08 13:07:15 +02:00
{
2020-04-15 15:13:38 +02:00
EntityResult result = await Api.Login(model.Email, model.Password, model.IsPersistent);
return Json(result);
2020-04-08 13:07:15 +02:00
}
/// <summary>
/// Logout for the current user
/// </summary>
2020-04-15 15:13:38 +02:00
[HttpPost, ZeroAuthorize]
2020-04-08 13:07:15 +02:00
public async Task<IActionResult> LogoutUser()
{
2020-04-15 15:13:38 +02:00
await Api.Logout();
return Json(EntityResult.Success());
2020-04-08 13:07:15 +02:00
}
}
}