Polyrific-Inc / Playground-Core

The Playground app with dotnet core
0 stars 3 forks source link

The app should have Login and Logout endpoints. #7

Closed affand closed 6 years ago

affand commented 6 years ago

In order to make user authentication works, The app should have endpoints for Login & Logout. we can utilize SignInManager service from AspNetCore.Identity.

The AccountController willl look like:

public class AccountController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly SignInManager<ApplicationUser> _signInManager;
        private readonly IUserProfileService _userProfileService;

        public AccountController(UserManager<ApplicationUser> userManager, 
        IUserProfileService userProfileService, 
        SignInManager<ApplicationUser> signInManager)
        {
            _userManager = userManager;
            _userProfileService = userProfileService;
            _signInManager = signInManager;
        }

        [HttpPost("Login")]
        public async Task<IActionResult> Login([FromBody] LoginDto model)
        {
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);

            if (result.Succeeded)
            {
                return Ok("You have been successfully logged in");
            }

            return BadRequest("Login failed");
        }

        [HttpPost("Logout")]
        public async Task<IActionResult> Logout()
        {
            await _signInManager.SignOutAsync();

            return Ok("You have been successfully logged out");
        }

.........
}
frandi commented 6 years ago

@affand Thanks for suggesting this, but it isn't intended to use Login/Logout mechanism for authentication. It uses token based auth, which it must include a previously retrieve token in every request.

https://github.com/Polyrific-Inc/Playground-Core/blob/d8e4b3f47e1e9f3162b34098b4b39ec1ac1f0562/src/PG.Api/Startup.cs#L38