Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions Dentizone.Application/Services/Authentication/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class AuthService(
ITokenService tokenService,
UserManager<ApplicationUser> userManager,
IMailService mailService,
IUserActivityService userActivityService


)
: IAuthService
{
Expand Down Expand Up @@ -71,7 +72,6 @@ public async Task<LoggedInUser> LoginWithEmailAndPassword(string email, string p

if (isLockedOut)
{
await userActivityService.CreateAsync(UserActivities.Lockedout, DateTime.Now, user.Id);
throw new
UserLockedOutException(
"User is locked out due to too many failed login attempts. Please try again later.");
Expand Down Expand Up @@ -107,7 +107,6 @@ public async Task<LoggedInUser> LoginWithEmailAndPassword(string email, string p
// 5. Generate token

await userManager.ResetAccessFailedCountAsync(user);
await userActivityService.CreateAsync(UserActivities.Login, DateTime.Now, user.Id);
return new LoggedInUser()
{
User = user,
Expand Down Expand Up @@ -145,7 +144,6 @@ public async Task<LoggedInUser> RegisterWithEmailAndPassword(RegisterRequestDto
// 4. Send Verification Email

await SendVerificationEmail(user.Email);
await userActivityService.CreateAsync(UserActivities.Registered, DateTime.Now, user.Id);
return new LoggedInUser()
{
User = user,
Expand Down Expand Up @@ -178,7 +176,6 @@ public async Task<string> ConfirmEmail(string token, string userId)

// 4. Assign verified role
await AlternateUserRoleAsync(UserRoles.PartilyVerified, user);
await userActivityService.CreateAsync(UserActivities.EmailConfirmed, DateTime.Now, user.Id);
// 4. Generate token
return GenerateToken(user.Id, user.Email, UserRoles.PartilyVerified.ToString());
}
Expand Down Expand Up @@ -212,7 +209,6 @@ await mailService.Send(
<p>If you did not request this, please ignore this email.</p>
"""
);
await userActivityService.CreateAsync(UserActivities.EmailVerificationSent, DateTime.Now, user.Id);
}

public async Task SendForgetPasswordEmail(string email)
Expand All @@ -230,7 +226,6 @@ public async Task SendForgetPasswordEmail(string email)
// 3. Send Reset Password Email
await mailService.Send(email, "Dentizone: Reset your password",
$"Please click the following link to reset your password: <a href=\"{resetLink}\">Reset Password</a>");
await userActivityService.CreateAsync(UserActivities.PasswordResetRequested, DateTime.Now, user.Id);
}

public async Task<ApplicationUser> GetById(string userId)
Expand Down Expand Up @@ -262,7 +257,6 @@ public async Task<string> ResetPassword(string email, string token, string newPa
throw new NotFoundException("User does not have any roles assigned");
}

await userActivityService.CreateAsync(UserActivities.PasswordReset, DateTime.Now, user.Id);
// 3. Generate token
return GenerateToken(user.Id, user.Email, roles.FirstOrDefault());
}
Expand Down
2 changes: 0 additions & 2 deletions Dentizone.Infrastructure/ApiClient/IAILayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ public interface IAiLayer
{
[Get("/all")]
Task<ApiResponse<ScanAllResponse>> ScanAll([AliasAs("text")] string text);

[Get("/contact-toxic")]
Task<ApiResponse<ScanAllResponse>> ScanContactToxic([AliasAs("text")] string text);
[Get("/sentiment")]

Task<ApiResponse<Sentiment>> GetSetmenetAnalysis([AliasAs("text")] string text);
}

Expand Down
23 changes: 21 additions & 2 deletions Dentizone.Presentaion/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using Dentizone.Application.DTOs.User;
using Dentizone.Application.Interfaces;
using Dentizone.Domain.Enums;
using Dentizone.Domain.Exceptions;
using Dentizone.Domain.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Dentizone.Domain.Exceptions;

namespace Dentizone.Presentaion.Controllers
{
Expand All @@ -15,7 +15,7 @@ namespace Dentizone.Presentaion.Controllers
public class AuthenticationController(
IAuthService authenticationService,
IUserService userService,
ITokenService tokenService) : ControllerBase
ITokenService tokenService, IUserActivityService userActivityService) : ControllerBase
{
[HttpPost("login")]
[AllowAnonymous]
Expand All @@ -31,6 +31,8 @@ public async Task<IActionResult> Login([FromBody] LoginRequestDto loginPayload)
var token = tokenService.GenerateAccessToken(loggedInUser.User.Id, loggedInUser.User.Email,
loggedInUser.Role.ToString());
var refreshToken = tokenService.GenerateRefreshToken(loggedInUser.User.Id);
await userActivityService.CreateAsync(UserActivities.Login, DateTime.UtcNow,
loggedInUser.User.Id);
return Ok(new RefreshTokenResponse()
{
AccessToken = token,
Expand Down Expand Up @@ -60,6 +62,8 @@ public async Task<IActionResult> Register([FromBody] RegisterRequestDto register
var token = tokenService.GenerateAccessToken(loggedInUser.User.Id, registerPayloadDto.Email,
loggedInUser.Role.ToString());
var refreshToken = tokenService.GenerateRefreshToken(loggedInUser.User.Id);
await userActivityService.CreateAsync(UserActivities.Register, DateTime.UtcNow,
loggedInUser.User.Id);
return Ok(new RefreshTokenResponse()
{
AccessToken = token,
Expand All @@ -75,6 +79,10 @@ public async Task<IActionResult> ConfirmEmail([FromQuery] string token)


var result = await authenticationService.ConfirmEmail(token, userId);

await userActivityService.CreateAsync(UserActivities.EmailConfirmed, DateTime.UtcNow, userId);


return Ok(new { Token = result });
}

Expand All @@ -83,8 +91,11 @@ public async Task<IActionResult> ConfirmEmail([FromQuery] string token)
public async Task<IActionResult> SendVerificationEmail()
{
var email = User.Claims.First(c => c.Type == ClaimTypes.Email).Value;
var userId = User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;

await authenticationService.SendVerificationEmail(email);
await userActivityService.CreateAsync(UserActivities.EmailVerificationSent, DateTime.UtcNow, userId);

return Ok();
}

Expand All @@ -94,6 +105,8 @@ public async Task<IActionResult> SendVerificationEmail()
public async Task<IActionResult> SendForgetPasswordEmail([FromQuery] string email)
{
await authenticationService.SendForgetPasswordEmail(email);


return Ok();
}

Expand All @@ -105,6 +118,8 @@ public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordDto reset
resetPasswordDto.NewPassword);




return Ok(new { Message = result });
}

Expand Down Expand Up @@ -186,6 +201,10 @@ public async Task<IActionResult> Logout([FromBody] LogoutRequest request)
await tokenService.BlacklistAccessTokenAsync(token);
await tokenService.BlacklistRefreshTokenAsync(request.RefreshToken);

var userId = User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;

await userActivityService.CreateAsync(UserActivities.Logout, DateTime.UtcNow, userId);


return Ok();
}
Expand Down