Added logs in login-mobile

This commit is contained in:
ashutosh.nehete 2025-08-12 10:54:40 +05:30
parent 6017d87793
commit c5385c0b06
2 changed files with 43 additions and 7 deletions

View File

@ -4,7 +4,7 @@
{
public string? Username { get; set; }
public string? Password { get; set; }
public required string DeviceToken { get; set; }
public string? DeviceToken { get; set; }
}
}

View File

@ -52,18 +52,36 @@ namespace MarcoBMS.Services.Controllers
{
try
{
var deviceToken = "ewjsd9zGTh6aS6Vg3Z_uxP:APA91bFZi1KzZdxlHUfBa_dX3PEJnDhX4R2dvFjD9Zf3WPSm957Hb53JPim7jrpjhpeOY61I9rfc11c3wpqWfW_06aSx-Yb8UfWpygV2YgZ8gbHtSku_PSQ";
var message = new Message()
{
Token = "ewjsd9zGTh6aS6Vg3Z_uxP:APA91bFZi1KzZdxlHUfBa_dX3PEJnDhX4R2dvFjD9Zf3WPSm957Hb53JPim7jrpjhpeOY61I9rfc11c3wpqWfW_06aSx-Yb8UfWpygV2YgZ8gbHtSku_PSQ",
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("Firebase push notification messageId: {MessageId}", response);
// Find user by email or phone number
_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);
@ -139,11 +157,29 @@ namespace MarcoBMS.Services.Controllers
Body = "This is a test message"
}
};
try
{
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
_logger.LogInfo("Firebase push notification messageId: {MessageId}", response);
_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}", loginDto.DeviceToken);
// Add your logic here to remove the invalid token from your database
// await YourTokenService.DeleteTokenAsync(recordAttendanceDot.DeviceToken);
}
}
// Validate input DTO
if (loginDto == null || string.IsNullOrWhiteSpace(loginDto.Username) || string.IsNullOrWhiteSpace(loginDto.Password))
{
_logger.LogWarning("Login failed: User not found for input {Username}", loginDto.Username ?? string.Empty);
return BadRequest(ApiResponse<object>.ErrorResponse("Username or password is missing.", "Invalid request", 400));
}