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