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")] public async Task> GetUser() { ZeroUser user = await AuthService.GetUser(); if (user == null) { return Unauthorized(); } return Mapper.Map(user); } [HttpGet("loggedin")] [ZeroAuthorize(false)] public ActionResult LoggedIn() => new LoggedInResponseModel() { LoggedIn = AuthService.IsLoggedIn() }; [HttpPost("login")] [ZeroAuthorize(false)] //[ValidateAntiForgeryToken] public async Task> Login(LoginModel model) { LoginResult result = await AuthService.Login(model.Email, model.Password, model.IsPersistent); if (result != LoginResult.Success) { return Result.Fail(result.ToString()); } ZeroUser user = await AuthService.GetUser(); return Result.Success(new() { User = Mapper.Map(user), ApiKey = "myapikey" }); } [HttpPost("logout")] public async Task Logout() { await AuthService.Logout(); return Ok(); } //[HttpPost, ZeroAuthorize] //public async Task SwitchApp(string appId) //{ // return Result.Maybe(await Api.TrySwitchApp(appId)); //} }