Update authservice to kiota exceptions.

This commit is contained in:
2025-11-16 23:09:36 +00:00
parent db7aee369b
commit 80ba603265

View File

@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Refit;
using Microsoft.Kiota.Abstractions;
using Uno.Extensions;
using Uno.Extensions.Authentication;
@@ -30,13 +29,20 @@ public sealed class AuthService
credentials.FirstOrDefault(x => x.Key.Equals("password", StringComparison.OrdinalIgnoreCase)).Value)
?.Trim();
if(email is null)
if(string.IsNullOrWhiteSpace(email))
{
credentials["error"] = stringLocalizer["Auth.EmailIsRequired"];
return false;
}
if(string.IsNullOrWhiteSpace(password))
{
credentials["error"] = stringLocalizer["Auth.PasswordIsRequired"];
return false;
}
var loginModel = new AuthRequest
{
Email = email,
@@ -50,44 +56,25 @@ public sealed class AuthService
tokenService.RemoveToken();
authResponse = await client.Auth.Login.PostAsync(loginModel);
}
catch(ValidationApiException ex)
catch(ProblemDetails ex)
{
switch(ex.StatusCode)
{
case HttpStatusCode.BadRequest:
if(ex.Content is {} problemDetails)
{
if(problemDetails.Errors.Count > 0)
{
credentials["error"] = problemDetails.Errors.FirstOrDefault().Value?.FirstOrDefault() ??
stringLocalizer["Http.BadRequest"];
return false;
}
credentials["error"] = stringLocalizer["Http.BadRequest"];
return false;
}
break;
}
credentials["error"] = stringLocalizer["Http.BadRequest"];
if(ex.Status == 400)
credentials["error"] = ex.Detail ?? ex.Title ?? stringLocalizer["Http.BadRequest"];
else if(ex.Status == 401)
credentials["error"] = stringLocalizer["Auth.InvalidCredentials"];
else
credentials["error"] = ex.Detail ?? ex.Title ?? stringLocalizer["Http.BadRequest"];
return false;
}
catch(ApiException ex)
{
switch(ex.StatusCode)
{
case HttpStatusCode.Unauthorized:
credentials["error"] = stringLocalizer["Auth.InvalidCredentials"];
return false;
}
credentials["error"] = stringLocalizer["Http.BadRequest"];
if(ex.ResponseStatusCode == 401)
credentials["error"] = stringLocalizer["Auth.InvalidCredentials"];
else if(ex.ResponseStatusCode == 400)
credentials["error"] = stringLocalizer["Http.BadRequest"];
else
credentials["error"] = ex.Message ?? stringLocalizer["Http.BadRequest"];
return false;
}