migrate to identity for authentication
All checks were successful
Build and Deploy API / build-and-deploy (push) Successful in 12s
All checks were successful
Build and Deploy API / build-and-deploy (push) Successful in 12s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
using agologumApi.Models;
|
||||
using agologumApi.Services;
|
||||
@@ -9,11 +10,17 @@ using agologumApi.Services;
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController : ControllerBase {
|
||||
|
||||
// identity things
|
||||
private readonly UserManager<User> userManager_;
|
||||
private readonly SignInManager<User> signInManager_;
|
||||
|
||||
private readonly UserService users_;
|
||||
private readonly JwtService jwt_;
|
||||
|
||||
public AuthController(UserService users, JwtService jwt)
|
||||
{
|
||||
public AuthController(UserManager<User> userManager, SignInManager<User> signInManager, UserService users, JwtService jwt) {
|
||||
|
||||
userManager_ = userManager;
|
||||
signInManager_ = signInManager;
|
||||
users_ = users;
|
||||
jwt_ = jwt;
|
||||
}
|
||||
@@ -21,10 +28,9 @@ public class AuthController : ControllerBase {
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult> Register(RegisterDto dto) {
|
||||
var user = new User {
|
||||
Name = dto.Username,
|
||||
UserName = dto.UserName,
|
||||
Email = dto.Email,
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword(dto.Password), // TODO: secondary hashing stage in client
|
||||
Role = "user",
|
||||
CreatedAt = DateTime.UtcNow // yeah why not utc
|
||||
};
|
||||
|
||||
@@ -39,11 +45,13 @@ public class AuthController : ControllerBase {
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult> Login(LoginDto dto)
|
||||
{
|
||||
var user = await users_.Get(dto.Username);
|
||||
var user = await users_.Get(dto.UserName);
|
||||
|
||||
if (user == null || !BCrypt.Net.BCrypt.Verify(dto.Password, user.PasswordHash)) {
|
||||
return Unauthorized();
|
||||
}
|
||||
if (user == null) return Unauthorized();
|
||||
|
||||
var result = await signInManager_.CheckPasswordSignInAsync(user, dto.Password, false);
|
||||
|
||||
if(!result.Succeeded) return Unauthorized();
|
||||
|
||||
var token = jwt_.GenerateJwt(user);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user