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.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
|
|
|
|
|
{
|
2020-05-11 15:34:47 +02:00
|
|
|
IAuthenticationApi Api;
|
2020-05-21 13:41:09 +02:00
|
|
|
IApplicationContext AppContext;
|
2020-04-08 13:07:15 +02:00
|
|
|
|
2020-05-21 13:41:09 +02:00
|
|
|
|
|
|
|
|
public AuthenticationController(IAuthenticationApi api, IApplicationContext appContext)
|
2020-04-08 13:07:15 +02:00
|
|
|
{
|
|
|
|
|
Api = api;
|
2020-05-21 13:41:09 +02:00
|
|
|
AppContext = appContext;
|
2020-04-08 13:07:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EditModel<User>> GetUser() => Edit(await Api.GetUser());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public EntityResult IsLoggedIn() => EntityResult.Maybe(Api.IsLoggedIn());
|
2020-05-11 11:30:56 +02:00
|
|
|
|
|
|
|
|
|
2020-04-15 15:13:38 +02:00
|
|
|
[HttpPost]
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EntityResult> LoginUser([FromBody] LoginModel model) => await Api.Login(model.Email, model.Password, model.IsPersistent);
|
2020-04-08 13:07:15 +02:00
|
|
|
|
|
|
|
|
|
2020-04-15 15:13:38 +02:00
|
|
|
[HttpPost, ZeroAuthorize]
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EntityResult> LogoutUser()
|
2020-04-08 13:07:15 +02:00
|
|
|
{
|
2020-04-15 15:13:38 +02:00
|
|
|
await Api.Logout();
|
2020-10-27 15:47:23 +01:00
|
|
|
return EntityResult.Success();
|
2020-04-08 13:07:15 +02:00
|
|
|
}
|
2020-05-21 13:41:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost, ZeroAuthorize]
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EntityResult> SwitchApp(string appId)
|
2020-05-21 13:41:09 +02:00
|
|
|
{
|
|
|
|
|
User user = await Api.GetUser();
|
2020-10-27 15:47:23 +01:00
|
|
|
return EntityResult.Maybe(await AppContext.TrySwitchForUser(user, appId));
|
2020-05-21 13:41:09 +02:00
|
|
|
}
|
2020-04-08 13:07:15 +02:00
|
|
|
}
|
|
|
|
|
}
|