-
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.
Merge pull request #19 from Alper-Soy/develop
Prod - 08/20/2024
- Loading branch information
Showing
40 changed files
with
1,418 additions
and
143 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,9 @@ | ||
namespace API.Contracts.User; | ||
|
||
public class AuthUserDto | ||
{ | ||
public string DisplayName { get; set; } | ||
public string Token { get; set; } | ||
public string Image { get; set; } | ||
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,7 @@ | ||
namespace API.Contracts.Auth; | ||
|
||
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,16 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace API.Contracts.Auth; | ||
|
||
public class RegisterDto | ||
{ | ||
[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 DisplayName { 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,83 @@ | ||
using System.Security.Claims; | ||
using API.Contracts.Auth; | ||
using API.Contracts.User; | ||
using API.Services.Auth; | ||
using Domain.Entities; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace API.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class AccountController(UserManager<User> userManager, TokenService tokenService) : ControllerBase | ||
{ | ||
[AllowAnonymous] | ||
[HttpPost("login")] | ||
public async Task<ActionResult<AuthUserDto>> Login(LoginDto loginDto) | ||
{ | ||
var user = await userManager.FindByEmailAsync(loginDto.Email); | ||
|
||
if (user == null) return Unauthorized(); | ||
|
||
var result = await userManager.CheckPasswordAsync(user, loginDto.Password); | ||
|
||
if (result) return CreateUserObject(user); | ||
|
||
return Unauthorized(); | ||
} | ||
|
||
[AllowAnonymous] | ||
[HttpPost("register")] | ||
public async Task<ActionResult<AuthUserDto>> Register(RegisterDto registerDto) | ||
{ | ||
if (await userManager.Users.AnyAsync(x => x.UserName == registerDto.Username)) | ||
{ | ||
ModelState.AddModelError("username", "Username taken"); | ||
return ValidationProblem(); | ||
} | ||
|
||
|
||
if (await userManager.Users.AnyAsync(x => x.Email == registerDto.Email)) | ||
{ | ||
ModelState.AddModelError("email", "Email taken"); | ||
return ValidationProblem(); | ||
} | ||
|
||
|
||
var user = new User | ||
{ | ||
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(result.Errors); | ||
} | ||
|
||
[Authorize] | ||
[HttpGet] | ||
public async Task<ActionResult<AuthUserDto>> GetCurrentUser() | ||
{ | ||
var user = await userManager.FindByEmailAsync(User.FindFirstValue(ClaimTypes.Email)); | ||
|
||
return CreateUserObject(user); | ||
} | ||
|
||
private AuthUserDto CreateUserObject(User user) | ||
{ | ||
return new AuthUserDto | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace API.Controllers | ||
namespace API.Controllers; | ||
|
||
public class BuggyController : BaseApiController | ||
{ | ||
public class BuggyController : BaseApiController | ||
[HttpGet("not-found")] | ||
public ActionResult GetNotFound() | ||
{ | ||
[HttpGet("not-found")] | ||
public ActionResult GetNotFound() | ||
{ | ||
return NotFound(); | ||
} | ||
return NotFound(); | ||
} | ||
|
||
[HttpGet("bad-request")] | ||
public ActionResult GetBadRequest() | ||
{ | ||
return BadRequest("This is a bad request"); | ||
} | ||
[HttpGet("bad-request")] | ||
public ActionResult GetBadRequest() | ||
{ | ||
return BadRequest("This is a bad request"); | ||
} | ||
|
||
[HttpGet("server-error")] | ||
public ActionResult GetServerError() | ||
{ | ||
throw new Exception("This is a server error"); | ||
} | ||
[HttpGet("server-error")] | ||
public ActionResult GetServerError() | ||
{ | ||
throw new Exception("This is a server error"); | ||
} | ||
|
||
[HttpGet("unauthorised")] | ||
public ActionResult GetUnauthorised() | ||
{ | ||
return Unauthorized(); | ||
} | ||
[HttpGet("unauthorised")] | ||
public ActionResult GetUnauthorised() | ||
{ | ||
return Unauthorized(); | ||
} | ||
} |
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,38 @@ | ||
using System.Text; | ||
using API.Services.Auth; | ||
using Domain.Entities; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Persistence; | ||
|
||
namespace API.Extensions; | ||
|
||
public static class IdentityServiceExtensions | ||
{ | ||
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration config) | ||
{ | ||
services.AddIdentityCore<User>(opt => | ||
{ | ||
opt.Password.RequireNonAlphanumeric = false; | ||
opt.User.RequireUniqueEmail = true; | ||
}) | ||
.AddEntityFrameworkStores<DataContext>(); | ||
|
||
|
||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["TokenKey"])); | ||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => | ||
{ | ||
options.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
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,36 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Domain.Entities; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace API.Services.Auth; | ||
|
||
public class TokenService(IConfiguration config) | ||
{ | ||
public string CreateToken(User user) | ||
{ | ||
var claims = new List<Claim> | ||
{ | ||
new(ClaimTypes.Name, user.UserName), | ||
new(ClaimTypes.NameIdentifier, user.Id), | ||
new(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.UtcNow.AddDays(7), | ||
SigningCredentials = creds | ||
}; | ||
|
||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
|
||
var token = tokenHandler.CreateToken(tokenDescriptor); | ||
|
||
return tokenHandler.WriteToken(token); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
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
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
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,9 @@ | ||
using Microsoft.AspNetCore.Identity; | ||
|
||
namespace Domain.Entities; | ||
|
||
public class User : 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
using Domain.Entities; | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Persistence; | ||
|
||
public class DataContext(DbContextOptions options) : DbContext(options) | ||
public class DataContext(DbContextOptions options) : IdentityDbContext<User>(options) | ||
{ | ||
public DbSet<Activity> Activities { get; set; } | ||
} |
Oops, something went wrong.