Firebase_Implementation #135

Merged
ashutosh.nehete merged 62 commits from Firebase_Implementation into main 2025-09-20 12:11:54 +00:00
2 changed files with 17 additions and 10 deletions
Showing only changes of commit 6f1a9cd892 - Show all commits

View File

@ -2,9 +2,8 @@
{ {
public class LoginDto public class LoginDto
{ {
public string? Username { get; set; } public required string Username { get; set; }
public string? Password { get; set; } public required string Password { get; set; }
public string? DeviceToken { get; set; } public required string DeviceToken { get; set; }
} }
} }

View File

@ -69,7 +69,7 @@ namespace MarcoBMS.Services.Controllers
} }
catch (FirebaseMessagingException ex) catch (FirebaseMessagingException ex)
{ {
_logger.LogError("Error sending push notification. : {Error}", ex.Message); _logger.LogError(ex, "Error sending push notification.");
// Check for the specific error codes that indicate an invalid token // Check for the specific error codes that indicate an invalid token
if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered || if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered ||
@ -145,13 +145,14 @@ namespace MarcoBMS.Services.Controllers
} }
} }
[HttpPost("login-mobile")]
/// <summary> /// <summary>
/// Handles mobile user login, validates credentials, sends a test push notification, /// Handles mobile user login, validates credentials, sends a test push notification,
/// and generates JWT, Refresh, and MPIN tokens upon successful authentication. /// and generates JWT, Refresh, and MPIN tokens upon successful authentication.
/// </summary> /// </summary>
/// <param name="loginDto">Data Transfer Object containing the user's login credentials and device token.</param> /// <param name="loginDto">Data Transfer Object containing the user's login credentials and device token.</param>
/// <returns>An IActionResult containing the authentication tokens or an error response.</returns> /// <returns>An IActionResult containing the authentication tokens or an error response.</returns>
[HttpPost("login-mobile")]
public async Task<IActionResult> LoginMobile([FromBody] LoginDto loginDto) public async Task<IActionResult> LoginMobile([FromBody] LoginDto loginDto)
{ {
// Log the start of the login attempt for traceability. // Log the start of the login attempt for traceability.
@ -179,7 +180,7 @@ namespace MarcoBMS.Services.Controllers
catch (FirebaseMessagingException ex) catch (FirebaseMessagingException ex)
{ {
// Log the specific Firebase error. // Log the specific Firebase error.
_logger.LogError("Error sending push notification: {Error}", ex.Message); _logger.LogError(ex, "Error sending push notification");
// Check for specific error codes that indicate an invalid or unregistered token. // Check for specific error codes that indicate an invalid or unregistered token.
if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered || if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered ||
@ -245,7 +246,7 @@ namespace MarcoBMS.Services.Controllers
if (string.IsNullOrWhiteSpace(user.UserName)) if (string.IsNullOrWhiteSpace(user.UserName))
{ {
// This is an unlikely edge case, but good to handle. // This is an unlikely edge case, but good to handle.
_logger.LogError("Login failed: User object for ID {UserId} is missing a UserName.", user.Id); _logger.LogWarning("Login failed: User object for ID {UserId} is missing a UserName.", user.Id);
return NotFound(ApiResponse<object>.ErrorResponse("UserName not found", "Username is missing", 404)); return NotFound(ApiResponse<object>.ErrorResponse("UserName not found", "Username is missing", 404));
} }
@ -255,7 +256,7 @@ namespace MarcoBMS.Services.Controllers
var emp = await _employeeHelper.GetEmployeeByApplicationUserID(user.Id); var emp = await _employeeHelper.GetEmployeeByApplicationUserID(user.Id);
if (emp == null) if (emp == null)
{ {
_logger.LogError("Login failed: Could not find associated employee record for user ID {UserId}", user.Id); _logger.LogWarning("Login failed: Could not find associated employee record for user ID {UserId}", user.Id);
return NotFound(ApiResponse<object>.ErrorResponse("Employee not found", "Employee details missing", 404)); return NotFound(ApiResponse<object>.ErrorResponse("Employee not found", "Employee details missing", 404));
} }
_logger.LogInfo("Successfully found employee details for tenant ID: {TenantId}", emp.TenantId); _logger.LogInfo("Successfully found employee details for tenant ID: {TenantId}", emp.TenantId);
@ -290,7 +291,7 @@ namespace MarcoBMS.Services.Controllers
{ {
// --- Global Exception Handling --- // --- Global Exception Handling ---
// Catch any unexpected exceptions during the login process. // Catch any unexpected exceptions during the login process.
_logger.LogError("An unexpected error occurred during the LoginMobile process for user: {Username} : {Error}", loginDto?.Username ?? "N/A", ex.Message); _logger.LogError(ex, "An unexpected error occurred during the LoginMobile process for user: {Username}", loginDto?.Username ?? "N/A");
// Return a generic 500 Internal Server Error to avoid leaking implementation details. // Return a generic 500 Internal Server Error to avoid leaking implementation details.
return StatusCode(500, ApiResponse<object>.ErrorResponse("An internal server error occurred.", "Server Error", 500)); return StatusCode(500, ApiResponse<object>.ErrorResponse("An internal server error occurred.", "Server Error", 500));
@ -917,6 +918,13 @@ namespace MarcoBMS.Services.Controllers
return Ok(ApiResponse<object>.SuccessResponse(mpinToken, "MPIN updated successfully", 200)); return Ok(ApiResponse<object>.SuccessResponse(mpinToken, "MPIN updated successfully", 200));
} }
} }
//[Authorize]
//[HttpPost("set/device-token")]
//public async Task<IActionResult> StoreDeviceToken([FromBody] DeviceTokenDto model)
//{
//}
private static string ComputeSha256Hash(string rawData) private static string ComputeSha256Hash(string rawData)
{ {
using (SHA256 sha256 = SHA256.Create()) using (SHA256 sha256 = SHA256.Create())