mirror of
https://github.com/sphildreth/roadie
synced 2024-11-26 22:20:21 +00:00
155 lines
No EOL
5.5 KiB
C#
155 lines
No EOL
5.5 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Roadie.Api.Models;
|
|
using Roadie.Api.Services;
|
|
using Roadie.Library.Identity;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Roadie.Api.Controllers
|
|
{
|
|
[Produces("application/json")]
|
|
[Route("auth")]
|
|
[ApiController]
|
|
[AllowAnonymous]
|
|
public class AccountController : ControllerBase
|
|
{
|
|
private readonly IConfiguration configuration;
|
|
private readonly ILogger<AccountController> logger;
|
|
private readonly SignInManager<ApplicationUser> signInManager;
|
|
private readonly ITokenService tokenService;
|
|
private readonly UserManager<ApplicationUser> userManager;
|
|
|
|
public AccountController(
|
|
UserManager<ApplicationUser> userManager,
|
|
SignInManager<ApplicationUser> signInManager,
|
|
IConfiguration configuration,
|
|
ILogger<AccountController> logger,
|
|
ITokenService tokenService)
|
|
{
|
|
this.userManager = userManager;
|
|
this.signInManager = signInManager;
|
|
this.configuration = configuration;
|
|
this.logger = logger;
|
|
this.tokenService = tokenService;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("token")]
|
|
public async Task<IActionResult> CreateToken([FromBody]LoginModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
// Login user
|
|
var loginResult = await signInManager.PasswordSignInAsync(model.Username, model.Password, isPersistent: false, lockoutOnFailure: false);
|
|
if (!loginResult.Succeeded)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
var user = await userManager.FindByNameAsync(model.Username);
|
|
var now = DateTime.UtcNow;
|
|
user.LastLogin = now;
|
|
user.LastUpdated = now;
|
|
await userManager.UpdateAsync(user);
|
|
var t = await this.tokenService.GenerateToken(user, this.userManager);
|
|
return Ok(new
|
|
{
|
|
Id = user.RoadieId,
|
|
Username = user.UserName,
|
|
user.Email,
|
|
user.LastLogin,
|
|
Token = t
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Eror in CreateToken");
|
|
return BadRequest();
|
|
}
|
|
}
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPost]
|
|
[Route("refreshtoken")]
|
|
public async Task<IActionResult> RefreshToken()
|
|
{
|
|
var username = User.Identity.Name ??
|
|
User.Claims.Where(c => c.Properties.ContainsKey("unique_name")).Select(c => c.Value).FirstOrDefault();
|
|
|
|
if (!String.IsNullOrWhiteSpace(username))
|
|
{
|
|
var user = await userManager.FindByNameAsync(username);
|
|
return Ok(await this.tokenService.GenerateToken(user, this.userManager));
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("Authentication", "Authentication failed!");
|
|
return BadRequest(ModelState);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("register")]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> Register([FromBody] RegisterModel registerModel)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var user = new ApplicationUser
|
|
{
|
|
UserName = registerModel.Username,
|
|
Email = registerModel.Email,
|
|
CreatedDate = DateTime.UtcNow
|
|
};
|
|
|
|
var identityResult = await this.userManager.CreateAsync(user, registerModel.Password);
|
|
if (identityResult.Succeeded)
|
|
{
|
|
await signInManager.SignInAsync(user, isPersistent: false);
|
|
return Ok(this.tokenService.GenerateToken(user, this.userManager));
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(identityResult.Errors);
|
|
}
|
|
}
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordModel resetPasswordModel)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var user = new ApplicationUser
|
|
{
|
|
UserName = resetPasswordModel.Username,
|
|
Email = resetPasswordModel.Email,
|
|
CreatedDate = DateTime.UtcNow
|
|
};
|
|
|
|
var identityResult = await this.userManager.ResetPasswordAsync(user, resetPasswordModel.Token, resetPasswordModel.Password);
|
|
if (identityResult.Succeeded)
|
|
{
|
|
await signInManager.SignInAsync(user, isPersistent: false);
|
|
return Ok(this.tokenService.GenerateToken(user, this.userManager));
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(identityResult.Errors);
|
|
}
|
|
}
|
|
return BadRequest(ModelState);
|
|
}
|
|
}
|
|
} |