-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7249aa4
commit 56b81e9
Showing
19 changed files
with
1,041 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using API.DTOs; | ||
using API.Services; | ||
using Domain; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace API.Controllers | ||
{ | ||
[AllowAnonymous] | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class AccountController : ControllerBase | ||
{ | ||
private readonly UserManager<AppUser> _userManager; | ||
private readonly SignInManager<AppUser> _signInManager; | ||
private readonly TokenService _tokenService; | ||
public AccountController(UserManager<AppUser> userManager, | ||
SignInManager<AppUser> signInManager, TokenService tokenService) | ||
{ | ||
_tokenService = tokenService; | ||
_signInManager = signInManager; | ||
_userManager = userManager; | ||
} | ||
|
||
[HttpPost("login")] | ||
public async Task<ActionResult<UserDto>> Login(LoginDto loginDto) | ||
{ | ||
var user = await _userManager.FindByEmailAsync(loginDto.Email); | ||
|
||
if (user == null) return Unauthorized(); | ||
|
||
var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false); | ||
|
||
if (result.Succeeded) | ||
{ | ||
return CreateUserObject(user); | ||
} | ||
return Unauthorized(); | ||
} | ||
|
||
[HttpPost("register")] | ||
public async Task<ActionResult<UserDto>> Register(RegisterDto registerDto) | ||
{ | ||
if(await _userManager.Users.AnyAsync(x => x.Email == registerDto.Email)) | ||
{ | ||
return BadRequest("Email taken"); | ||
} | ||
if(await _userManager.Users.AnyAsync(x => x.UserName == registerDto.UserName)) | ||
{ | ||
return BadRequest("Username taken"); | ||
} | ||
|
||
var user = new AppUser | ||
{ | ||
DisplayName = registerDto.DisplayName, | ||
Email = registerDto.Email, | ||
UserName = registerDto.UserName | ||
}; | ||
var result = await _userManager.CreateAsync(user, registerDto.Password); | ||
|
||
if(result.Succeeded) | ||
{ | ||
return CreateUserObject(user); | ||
}; | ||
|
||
return BadRequest("Problem registering user"); | ||
} | ||
|
||
[Authorize] | ||
[HttpGet] | ||
public async Task<ActionResult<UserDto>> GetCurrentUser() | ||
{ | ||
var user = await _userManager.FindByEmailAsync(User.FindFirstValue(ClaimTypes.Email)); | ||
|
||
return CreateUserObject(user); | ||
} | ||
|
||
private UserDto CreateUserObject(AppUser user) | ||
{ | ||
return new UserDto | ||
{ | ||
DisplayName = user.DisplayName, | ||
Image = null, | ||
Token = _tokenService.CreateToken(user), | ||
Username = user.UserName | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace API.DTOs | ||
{ | ||
public class LoginDto | ||
{ | ||
public string Email { get; set; } | ||
public string Password { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace API.DTOs | ||
{ | ||
public class RegisterDto | ||
{ | ||
[Required] | ||
public string DisplayName { get; set; } | ||
|
||
[Required] | ||
[EmailAddress] | ||
public string Email { get; set; } | ||
|
||
[Required] | ||
[RegularExpression("(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$",ErrorMessage = "Password must be complex")] | ||
public string Password { get; set; } | ||
[Required] | ||
public string UserName { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace API.DTOs | ||
{ | ||
public class UserDto | ||
{ | ||
public string DisplayName { get; set; } | ||
public string Token { get; set; } | ||
public string Username { get; set; } | ||
public string Image { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Text; | ||
using API.Services; | ||
using Domain; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Persistence; | ||
|
||
namespace API.Extensions | ||
{ | ||
public static class IdentityServiceExtensions | ||
{ | ||
public static IServiceCollection AddIdentityServices(this IServiceCollection services, | ||
IConfiguration config) | ||
{ | ||
services.AddIdentityCore<AppUser>(opt => | ||
{ | ||
opt.Password.RequireNonAlphanumeric = false; | ||
}) | ||
.AddEntityFrameworkStores<DataContext>() | ||
.AddSignInManager<SignInManager<AppUser>>(); | ||
|
||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["TokenKey"])); | ||
|
||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||
.AddJwtBearer(opt => | ||
{ | ||
opt.TokenValidationParameters = new TokenValidationParameters | ||
{ | ||
ValidateIssuerSigningKey = true, | ||
IssuerSigningKey = key, | ||
ValidateIssuer = false, | ||
ValidateAudience = false | ||
}; | ||
}); | ||
services.AddScoped<TokenService>(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Domain; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace API.Services | ||
{ | ||
public class TokenService | ||
{ | ||
private readonly IConfiguration _config; | ||
public TokenService(IConfiguration config) | ||
{ | ||
_config = config; | ||
|
||
} | ||
|
||
public string CreateToken(AppUser user) | ||
{ | ||
var claims = new List<Claim> | ||
{ | ||
new Claim(ClaimTypes.Name, user.UserName), | ||
new Claim(ClaimTypes.NameIdentifier, user.Id), | ||
new Claim(ClaimTypes.Email, user.Email), | ||
}; | ||
|
||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["TokenKey"])); | ||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); | ||
|
||
var tokenDescriptor = new SecurityTokenDescriptor | ||
{ | ||
Subject = new ClaimsIdentity(claims), | ||
Expires = DateTime.Now.AddDays(7), | ||
SigningCredentials = creds | ||
}; | ||
|
||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
|
||
var token = tokenHandler.CreateToken(tokenDescriptor); | ||
|
||
return tokenHandler.WriteToken(token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.AspNetCore.Identity; | ||
|
||
namespace Domain | ||
{ | ||
public class AppUser : IdentityUser | ||
{ | ||
public string DisplayName { get; set; } | ||
public string Bio { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.