Initial commit - Auth
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
using Auth.Contracts.DTOs;
|
||||
using Auth.Contracts.DTOs.Auth;
|
||||
using Auth.Contracts.DTOs.Users;
|
||||
using Auth.Core.Services.Auth;
|
||||
using Auth.Domain.Entities.HR;
|
||||
using Auth.Core.Services;
|
||||
using Auth.Domain.Entities;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Auth.API.Endpoints.v1.UserEndpoints;
|
||||
|
||||
public static class AuthMap
|
||||
{
|
||||
public static RouteGroupBuilder AuthEndpoints(this RouteGroupBuilder group)
|
||||
{
|
||||
group.MapPost(
|
||||
"auth/phone-number",
|
||||
async (string phoneNumber, AuthService authService, HttpContext httpContext) =>
|
||||
{
|
||||
var userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "uid").Value;
|
||||
|
||||
await authService.SendChangePhoneNumberToken(userId, phoneNumber);
|
||||
}
|
||||
);
|
||||
|
||||
group.MapPut(
|
||||
"auth/phone-number",
|
||||
async (
|
||||
UpdatePhoneNumber updatePhoneNumber,
|
||||
AuthService authService,
|
||||
HttpContext httpContext
|
||||
) =>
|
||||
{
|
||||
var userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "uid").Value;
|
||||
|
||||
var result = await authService.ChangePhoneNumber(userId, updatePhoneNumber);
|
||||
return result;
|
||||
}
|
||||
);
|
||||
|
||||
group.MapPost(
|
||||
"auth/login",
|
||||
async ([FromBody] LoginModel loginModel, AuthService authService) =>
|
||||
{
|
||||
var token = await authService.Login(loginModel);
|
||||
if (token == null)
|
||||
return Results.BadRequest();
|
||||
return Results.Ok(token);
|
||||
}
|
||||
);
|
||||
|
||||
group.MapPost(
|
||||
"auth/refresh",
|
||||
async ([FromBody] TokenModel tokenModel, AuthService authService) =>
|
||||
{
|
||||
var token = await authService.RefreshToken(tokenModel);
|
||||
if (token == null)
|
||||
return Results.BadRequest();
|
||||
|
||||
return Results.Ok(token);
|
||||
}
|
||||
);
|
||||
|
||||
group.MapPost(
|
||||
"auth/register",
|
||||
async ([FromBody] RegisterModel registerModel, AuthService authService) =>
|
||||
{
|
||||
var result = await authService.SignUp(registerModel);
|
||||
if (result == "")
|
||||
return Results.Ok();
|
||||
return Results.Conflict(result);
|
||||
}
|
||||
);
|
||||
|
||||
group
|
||||
.MapGet(
|
||||
"auth/resend-confirmation",
|
||||
async (HttpContext httpContext, AuthService authService) =>
|
||||
{
|
||||
var userId = httpContext
|
||||
.User.Claims.FirstOrDefault(x => x.Type == "uid")
|
||||
.Value;
|
||||
if (userId is null)
|
||||
return Results.BadRequest();
|
||||
await authService.SendEmailConfirmationToken(userId);
|
||||
return Results.Ok();
|
||||
}
|
||||
)
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapGet(
|
||||
"auth/password-reset-token/{email}",
|
||||
async (string email, AuthService authService) =>
|
||||
{
|
||||
await authService.SendPasswordResetToken(email);
|
||||
return Results.Ok();
|
||||
}
|
||||
);
|
||||
|
||||
group
|
||||
.MapPost(
|
||||
"auth/confirm-email",
|
||||
async (
|
||||
[FromBody] string confirmationToken,
|
||||
UserManager<AppUser> userManager,
|
||||
HttpContext httpContext
|
||||
) =>
|
||||
{
|
||||
var userId = httpContext
|
||||
.User.Claims.FirstOrDefault(x => x.Type == "uid")
|
||||
.Value;
|
||||
|
||||
if (userId is null)
|
||||
return Results.BadRequest();
|
||||
|
||||
var user = await userManager.FindByIdAsync(userId);
|
||||
var result = await userManager.ConfirmEmailAsync(user, confirmationToken);
|
||||
return Results.Ok();
|
||||
}
|
||||
)
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapPost(
|
||||
"auth/reset-password",
|
||||
async (
|
||||
[FromBody] PasswordResetModel passwordResetModel,
|
||||
UserManager<AppUser> userManager,
|
||||
HttpContext httpContext
|
||||
) =>
|
||||
{
|
||||
var user = await userManager.FindByEmailAsync(passwordResetModel.Email);
|
||||
if (user is null)
|
||||
return Results.BadRequest("Email not registered.");
|
||||
await userManager.ResetPasswordAsync(
|
||||
user,
|
||||
passwordResetModel.Token,
|
||||
passwordResetModel.Password
|
||||
);
|
||||
return Results.Ok();
|
||||
}
|
||||
);
|
||||
|
||||
group.MapPost(
|
||||
"auth/change-email",
|
||||
async (
|
||||
RequestChangeEmail requestChangeEmail,
|
||||
AuthService authService,
|
||||
HttpContext httpContext
|
||||
) =>
|
||||
{
|
||||
var userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "uid").Value;
|
||||
await authService.SendChangeEmailToken(requestChangeEmail, userId);
|
||||
}
|
||||
);
|
||||
|
||||
group.MapPut(
|
||||
"auth/email",
|
||||
async (
|
||||
ChangeEmailModel changeEmailModel,
|
||||
AuthService authService,
|
||||
HttpContext httpContext
|
||||
) =>
|
||||
{
|
||||
var userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "uid").Value;
|
||||
await authService.ChangeEmail(userId, changeEmailModel);
|
||||
}
|
||||
);
|
||||
|
||||
group.MapPost(
|
||||
"auth/logout",
|
||||
async (AuthService authService, HttpContext httpContext) =>
|
||||
{
|
||||
var username = httpContext.User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
|
||||
|
||||
var result = await authService.RevokeRefreshToken(username);
|
||||
return result ? Results.Ok() : Results.BadRequest();
|
||||
}
|
||||
).RequireAuthorization();
|
||||
|
||||
return group;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// using System;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using System.Threading.Tasks;
|
||||
|
||||
// namespace Auth.API.Endpoints.v1.UserEndpoints;
|
||||
|
||||
// public class GroupMap
|
||||
// {
|
||||
|
||||
// }
|
||||
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Auth.Domain.Entities.HR;
|
||||
using Auth.Domain.Entities;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Auth.Contracts.DTOs.Users;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Auth.API.Endpoints.v1.UserEndpoints;
|
||||
|
||||
public static class RoleMap
|
||||
{
|
||||
public static RouteGroupBuilder RoleEndpoints(this RouteGroupBuilder group)
|
||||
{
|
||||
|
||||
|
||||
|
||||
group.MapPost(
|
||||
"role-management/{role}/user/{userId}",
|
||||
async ([FromRoute] string userId, [FromRoute] string role, UserManager<AppUser> userManager) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await userManager.Users.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
if (user is null)
|
||||
return Results.BadRequest();
|
||||
|
||||
var result = await userManager.AddToRoleAsync(user, role);
|
||||
if (!result.Succeeded)
|
||||
return Results.BadRequest();
|
||||
return Results.Ok();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Results.StatusCode(500);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
group.MapDelete(
|
||||
"role-management/{role}/user/{userid}",
|
||||
async (string role, string userId, UserManager<AppUser> userManager) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await userManager.Users.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
if (user is null)
|
||||
return Results.BadRequest();
|
||||
|
||||
var result = await userManager.RemoveFromRoleAsync(user, role);
|
||||
if (!result.Succeeded)
|
||||
return Results.BadRequest();
|
||||
|
||||
return Results.Ok();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Results.StatusCode(500);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
group.MapGet(
|
||||
"role-management/roles",
|
||||
async (RoleManager<IdentityRole> roleManager) =>
|
||||
{
|
||||
var roles = await roleManager.Roles.ToListAsync();
|
||||
return Results.Ok(roles);
|
||||
}
|
||||
).RequireAuthorization();
|
||||
|
||||
group.MapPost(
|
||||
"role-management/roles",
|
||||
async ([FromBody] RoleVM roleVM, RoleManager<IdentityRole> roleManager) =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(roleVM.Name)) return Results.BadRequest("Role Name is required.");
|
||||
|
||||
var roleExists = await roleManager.RoleExistsAsync(roleVM.Name);
|
||||
if (roleExists) return Results.Conflict("Role already exists.");
|
||||
|
||||
var result = await roleManager.CreateAsync(new IdentityRole(roleVM.Name));
|
||||
return result.Succeeded ? Results.Ok() : Results.BadRequest(result.Errors);
|
||||
}
|
||||
).RequireAuthorization();
|
||||
|
||||
group.MapDelete(
|
||||
"role-management/roles/{id}",
|
||||
async (string id, RoleManager<IdentityRole> roleManager) =>
|
||||
{
|
||||
var role = await roleManager.FindByIdAsync(id);
|
||||
if (role == null) return Results.NotFound();
|
||||
|
||||
var result = await roleManager.DeleteAsync(role);
|
||||
return result.Succeeded ? Results.Ok() : Results.BadRequest(result.Errors);
|
||||
}
|
||||
).RequireAuthorization();
|
||||
|
||||
group.MapGet("role-management/roles/{id}/permissions", async (string id, RoleManager<IdentityRole> roleManager) =>
|
||||
{
|
||||
var role = await roleManager.FindByIdAsync(id);
|
||||
if (role == null) return Results.NotFound("Role not found.");
|
||||
|
||||
var claims = await roleManager.GetClaimsAsync(role);
|
||||
return Results.Ok(claims.Select(c => c.Value));
|
||||
}).RequireAuthorization();
|
||||
|
||||
group.MapPost("role-management/roles/{id}/permissions", async (string id, [FromBody] string permission, RoleManager<IdentityRole> roleManager) =>
|
||||
{
|
||||
var role = await roleManager.FindByIdAsync(id);
|
||||
if (role == null) return Results.NotFound("Role not found.");
|
||||
|
||||
var result = await roleManager.AddClaimAsync(role, new Claim("Permission", permission));
|
||||
return result.Succeeded ? Results.Ok() : Results.BadRequest(result.Errors);
|
||||
}).RequireAuthorization();
|
||||
|
||||
group.MapDelete("role-management/roles/{id}/permissions", async (string id, [FromBody] string permission, RoleManager<IdentityRole> roleManager) =>
|
||||
{
|
||||
var role = await roleManager.FindByIdAsync(id);
|
||||
if (role == null) return Results.NotFound("Role not found.");
|
||||
|
||||
var result = await roleManager.RemoveClaimAsync(role, new Claim("Permission", permission));
|
||||
return result.Succeeded ? Results.Ok() : Results.BadRequest(result.Errors);
|
||||
}).RequireAuthorization();
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// using System;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using System.Threading.Tasks;
|
||||
// using Contracts.DTOs.HR;
|
||||
// using Core.Services.HR.Users;
|
||||
// using Domain.Entities.HR;
|
||||
|
||||
// namespace Auth.API.Endpoints.v1.UserEndpoints;
|
||||
|
||||
// public static class SalaryMap
|
||||
// {
|
||||
// public static RouteGroupBuilder SalaryEndpoints(this RouteGroupBuilder group)
|
||||
// {
|
||||
// group.MapPost(
|
||||
// "salary",
|
||||
// async (SalaryVM salaryVM, SalaryService salaryService) =>
|
||||
// {
|
||||
// await salaryService.AddNewSalaryToUser(salaryVM);
|
||||
// }
|
||||
// );
|
||||
|
||||
// return group;
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Auth.Contracts.DTOs.Users;
|
||||
using Auth.Core.Services;
|
||||
using Auth.Core.Services.HR.Users;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Auth.API.Endpoints.v1.UserEndpoints;
|
||||
|
||||
public static class UserMap
|
||||
{
|
||||
public static RouteGroupBuilder UserEndpoints(this RouteGroupBuilder group)
|
||||
{
|
||||
|
||||
|
||||
// group.MapGet(
|
||||
// "location",
|
||||
// async (UserService userService, HttpContext httpContext) =>
|
||||
// {
|
||||
// var userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "uid").Value;
|
||||
// return await userService.GetLocation(userId);
|
||||
// }
|
||||
// );
|
||||
|
||||
// group.MapPut(
|
||||
// "location",
|
||||
// async (Location location, UserService userService, HttpContext httpContext) =>
|
||||
// {
|
||||
// var userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "uid").Value;
|
||||
// return await userService.UpdateLocation(location, userId);
|
||||
// }
|
||||
// );
|
||||
|
||||
// // group.MapGet("user_realestates/{id}", (
|
||||
// // string id,
|
||||
// // UserService userService
|
||||
// // ) =>
|
||||
// // {
|
||||
// // return userService.GetUserView(id);
|
||||
// // });
|
||||
|
||||
|
||||
|
||||
//get your own profile/settings
|
||||
group.MapGet(
|
||||
"user",
|
||||
async (UserService userService, HttpContext httpContext) =>
|
||||
{
|
||||
var userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "uid").Value;
|
||||
return await userService.GetUser(userId);
|
||||
}
|
||||
);
|
||||
|
||||
//get user id (for admins)
|
||||
group.MapGet(
|
||||
"user/{id}",
|
||||
async (string id, UserService userService) =>
|
||||
{
|
||||
return await userService.GetUser(id);
|
||||
}
|
||||
);
|
||||
|
||||
//get all users information
|
||||
group
|
||||
.MapGet(
|
||||
"users",
|
||||
async (UserService userService) =>
|
||||
{
|
||||
return await userService.GetAllUsers();
|
||||
}
|
||||
)
|
||||
;
|
||||
|
||||
//update user info
|
||||
group.MapPut(
|
||||
"user",
|
||||
async (
|
||||
[FromBody] UserInfoUpdate userVM,
|
||||
HttpContext httpContext,
|
||||
UserService userService
|
||||
) =>
|
||||
{
|
||||
var userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "uid").Value;
|
||||
await userService.UpdateUserInfo(userVM, userId);
|
||||
}
|
||||
);
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user