Files
mixtape/zero.Backoffice/Endpoints/Account/AccountController.cs
T

72 lines
1.6 KiB
C#
Raw Normal View History

2021-12-03 14:45:49 +01:00
using Microsoft.AspNetCore.Mvc;
using zero.Api.Filters;
using zero.Identity.Models;
namespace zero.Backoffice.Endpoints.Account;
[ZeroSystemApi]
public class AccountController : ZeroBackofficeController
{
readonly IAuthenticationService AuthService;
public AccountController(IAuthenticationService authService)
{
AuthService = authService;
}
[HttpGet("user")]
2021-12-14 11:25:32 +01:00
public async Task<ActionResult<object>> GetUser()
2021-12-03 14:45:49 +01:00
{
ZeroUser user = await AuthService.GetUser();
if (user == null)
{
2021-12-14 11:25:32 +01:00
return Unauthorized();
2021-12-03 14:45:49 +01:00
}
return Mapper.Map<ZeroUser, UserModel>(user);
}
[HttpGet("loggedin")]
[ZeroAuthorize(false)]
2021-12-13 16:11:52 +01:00
public ActionResult<LoggedInResponseModel> LoggedIn() => new LoggedInResponseModel() { LoggedIn = AuthService.IsLoggedIn() };
2021-12-03 14:45:49 +01:00
[HttpPost("login")]
[ZeroAuthorize(false)]
//[ValidateAntiForgeryToken]
2021-12-13 16:11:52 +01:00
public async Task<ActionResult<Result>> Login(LoginModel model)
2021-12-03 14:45:49 +01:00
{
LoginResult result = await AuthService.Login(model.Email, model.Password, model.IsPersistent);
if (result != LoginResult.Success)
{
2021-12-13 16:11:52 +01:00
return Result.Fail(result.ToString());
2021-12-03 14:45:49 +01:00
}
2021-12-13 16:11:52 +01:00
ZeroUser user = await AuthService.GetUser();
return Result<LoginSuccessModel>.Success(new()
{
User = Mapper.Map<ZeroUser, UserModel>(user),
ApiKey = "myapikey"
});
2021-12-03 14:45:49 +01:00
}
[HttpPost("logout")]
public async Task<ActionResult> Logout()
{
await AuthService.Logout();
return Ok();
}
//[HttpPost, ZeroAuthorize]
//public async Task<Result> SwitchApp(string appId)
//{
// return Result.Maybe(await Api.TrySwitchApp(appId));
//}
}