188 lines
6.1 KiB
C#
188 lines
6.1 KiB
C#
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;
|
|
|
|
}
|
|
}
|