diff --git a/Marco.Pms.Model/Dtos/Authentication/LoginDto.cs b/Marco.Pms.Model/Dtos/Authentication/LoginDto.cs
index 00bef12..536ac5d 100644
--- a/Marco.Pms.Model/Dtos/Authentication/LoginDto.cs
+++ b/Marco.Pms.Model/Dtos/Authentication/LoginDto.cs
@@ -4,5 +4,7 @@
{
public string? Username { get; set; }
public string? Password { get; set; }
+ public string? DeviceToken { get; set; }
+
}
}
diff --git a/Marco.Pms.Services/Controllers/AttendanceController.cs b/Marco.Pms.Services/Controllers/AttendanceController.cs
index 6bd5a66..3675dc6 100644
--- a/Marco.Pms.Services/Controllers/AttendanceController.cs
+++ b/Marco.Pms.Services/Controllers/AttendanceController.cs
@@ -1,5 +1,4 @@
-using System.Globalization;
-using FirebaseAdmin.Messaging;
+using FirebaseAdmin.Messaging;
using Marco.Pms.DataAccess.Data;
using Marco.Pms.Model.AttendanceModule;
using Marco.Pms.Model.Dtos.Attendance;
diff --git a/Marco.Pms.Services/Controllers/AuthController.cs b/Marco.Pms.Services/Controllers/AuthController.cs
index 67dd74a..aa064ee 100644
--- a/Marco.Pms.Services/Controllers/AuthController.cs
+++ b/Marco.Pms.Services/Controllers/AuthController.cs
@@ -1,4 +1,5 @@
-using Marco.Pms.DataAccess.Data;
+using FirebaseAdmin.Messaging;
+using Marco.Pms.DataAccess.Data;
using Marco.Pms.Model.Authentication;
using Marco.Pms.Model.Dtos.Authentication;
using Marco.Pms.Model.Dtos.Util;
@@ -51,7 +52,36 @@ namespace MarcoBMS.Services.Controllers
{
try
{
- // Find user by email or phone number
+ var deviceToken = "ewjsd9zGTh6aS6Vg3Z_uxP:APA91bFZi1KzZdxlHUfBa_dX3PEJnDhX4R2dvFjD9Zf3WPSm957Hb53JPim7jrpjhpeOY61I9rfc11c3wpqWfW_06aSx-Yb8UfWpygV2YgZ8gbHtSku_PSQ";
+ var message = new Message()
+ {
+ Token = deviceToken,
+ Notification = new Notification
+ {
+ Title = "Hello from .NET",
+ Body = "This is a test message"
+ }
+ };
+ try
+ {
+ string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
+ _logger.LogInfo("Successfully sent message: {MessageId}", response);
+ }
+ catch (FirebaseMessagingException ex)
+ {
+ _logger.LogError("Error sending push notification. : {Error}", ex.Message);
+
+ // Check for the specific error codes that indicate an invalid token
+ if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered ||
+ ex.MessagingErrorCode == MessagingErrorCode.InvalidArgument)
+ {
+ _logger.LogWarning("FCM token is invalid and should be deleted: {Token}", deviceToken);
+
+ // Add your logic here to remove the invalid token from your database
+ // await YourTokenService.DeleteTokenAsync(recordAttendanceDot.DeviceToken);
+ }
+ }
+
var user = await _context.ApplicationUsers
.FirstOrDefaultAsync(u => u.Email == loginDto.Username || u.PhoneNumber == loginDto.Username);
@@ -116,77 +146,158 @@ namespace MarcoBMS.Services.Controllers
}
[HttpPost("login-mobile")]
+ ///
+ /// Handles mobile user login, validates credentials, sends a test push notification,
+ /// and generates JWT, Refresh, and MPIN tokens upon successful authentication.
+ ///
+ /// Data Transfer Object containing the user's login credentials and device token.
+ /// An IActionResult containing the authentication tokens or an error response.
public async Task LoginMobile([FromBody] LoginDto loginDto)
{
- // Validate input DTO
- if (loginDto == null || string.IsNullOrWhiteSpace(loginDto.Username) || string.IsNullOrWhiteSpace(loginDto.Password))
+ // Log the start of the login attempt for traceability.
+ _logger.LogInfo("Login attempt initiated for user: {Username}", loginDto.Username ?? "N/A");
+
+ // --- Push Notification Section ---
+ // This section attempts to send a test push notification to the user's device.
+ // It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
+ var message = new Message()
{
- return BadRequest(ApiResponse