Enhance Customer Logout Functionality with Improved Error Handling and Device Token Management
- Updated the Logout method in customer service to retrieve the customer by ID before invalidating the access token, enhancing error logging for failed retrievals. - Added error handling for the access token invalidation process, ensuring a clear error message is returned if it fails. - Implemented logic to clear the device token upon logout, updating the customer record accordingly and logging any errors during the update process. - Improved logging for successful and failed logout operations to enhance traceability.
This commit is contained in:
@@ -779,18 +779,37 @@ func (s *customerService) GetProfileWithCompanies(ctx context.Context, customerI
|
||||
// Logout handles customer logout
|
||||
func (s *customerService) Logout(ctx context.Context, customerID string, accessToken string) error {
|
||||
s.logger.Info("Customer logout", map[string]interface{}{
|
||||
"customer_id": customerID,
|
||||
"access_token": accessToken[:10] + "...", // Log only first 10 chars for security
|
||||
"customer_id": customerID,
|
||||
})
|
||||
|
||||
// get customer by id
|
||||
customer, err := s.repository.GetByID(ctx, customerID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get customer for logout", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"customer_id": customerID,
|
||||
})
|
||||
}
|
||||
|
||||
// Invalidate the access token
|
||||
err := s.authService.InvalidateToken(accessToken)
|
||||
err = s.authService.InvalidateToken(accessToken)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to invalidate access token", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"customer_id": customerID,
|
||||
})
|
||||
// Don't return error here as the customer should still be logged out
|
||||
return errors.New("failed to invalidate access token")
|
||||
}
|
||||
|
||||
// delete device token from customer
|
||||
customer.DeviceToken = make([]string, 0)
|
||||
err = s.repository.Update(ctx, customer)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to update customer device token", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"customer_id": customerID,
|
||||
})
|
||||
}
|
||||
|
||||
s.logger.Info("Customer logout successful", map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user