Skip to content

Add interfaces, bug fixes, additional error reason detection #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 11, 2023
2 changes: 1 addition & 1 deletion Gotrue/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public Task<BaseResponse> DeleteUser(string uid, string jwt)

public Task<Settings?> Settings()
{
return Helpers.MakeRequest<Settings>(HttpMethod.Get, $"{Url}/settings");
return Helpers.MakeRequest<Settings>(HttpMethod.Get, $"{Url}/settings", null, Headers);
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion Gotrue/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ public Session SetAuth(string accessToken)
public Func<Dictionary<string, string>>? GetHeaders
{
get => _api.GetHeaders;
set => throw new ArgumentException();
set => _api.GetHeaders = value;
}

/// <summary>
Expand Down Expand Up @@ -871,5 +871,10 @@ public void LoadSession()
if (_sessionPersistence != null)
UpdateSession(_sessionPersistence.Persistence.LoadSession());
}

public Task<Settings?> Settings()
{
return _api.Settings();
}
}
}
22 changes: 19 additions & 3 deletions Gotrue/Exceptions/FailureReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ public static class FailureHint
public enum Reason
{
Unknown,
UserEmailNotConfirmed,
UserBadMultiple,
UserBadPassword,
UserBadLogin,
UserBadEmailAddress,
UserBadPhoneNumber,
UserMissingInformation,
UserAlreadyRegistered,
UserTooManyRequests,
InvalidRefreshToken,
AdminTokenRequired
}
Expand All @@ -25,12 +30,23 @@ public static Reason DetectReason(GotrueException gte)

return gte.StatusCode switch
{
400 when gte.Content.Contains("Invalid login") => UserBadLogin,
400 when gte.Content.Contains("Email not confirmed") => UserEmailNotConfirmed,
400 when gte.Content.Contains("User already registered") => UserAlreadyRegistered,
400 when gte.Content.Contains("Invalid Refresh Token") => InvalidRefreshToken,
401 when gte.Content.Contains("This endpoint requires a Bearer token") => AdminTokenRequired,
422 when gte.Content.Contains("Password should be at least") => UserBadPassword,
422 when gte.Content.Contains("Unable to validate email address") => UserBadEmailAddress,
422 when gte.Content.Contains("provide your email or phone number") => UserMissingInformation,
422 when gte.Content.Contains("Phone") && gte.Content.Contains("Email") => UserBadMultiple,
422 when gte.Content.Contains("email") && gte.Content.Contains("password") => UserBadMultiple,
422 when gte.Content.Contains("Phone") => UserBadPhoneNumber,
422 when gte.Content.Contains("phone") => UserBadPhoneNumber,
422 when gte.Content.Contains("Phone") => UserBadPhoneNumber,
422 when gte.Content.Contains("phone") => UserBadPhoneNumber,
422 when gte.Content.Contains("Email") => UserBadEmailAddress,
422 when gte.Content.Contains("email") => UserBadEmailAddress,
422 when gte.Content.Contains("Password") => UserBadPassword,
422 when gte.Content.Contains("password") => UserBadPassword,
422 when gte.Content.Contains("provide") => UserMissingInformation,
429 => UserTooManyRequests,
_ => Unknown
};

Expand Down
3 changes: 3 additions & 0 deletions Gotrue/Interfaces/IGotrueClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ public interface IGotrueClient<TUser, TSession> : IGettableHeaders
Task<TUser?> UpdateUserById(string jwt, string userId, AdminUserAttributes userData);
Task<TSession?> VerifyOTP(string phone, string token, MobileOtpType type = MobileOtpType.SMS);
Task<TSession?> VerifyOTP(string email, string token, EmailOtpType type = EmailOtpType.MagicLink);
void AddDebugListener(Action<string, Exception?> logDebug);
void LoadSession();
Task<Settings?> Settings();
}
}
2 changes: 1 addition & 1 deletion Gotrue/Interfaces/IGotrueSessionPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public interface IGotrueSessionPersistence<TSession> where TSession : Session

public void DestroySession();

public TSession LoadSession();
public TSession? LoadSession();
}
}
1 change: 1 addition & 0 deletions Gotrue/Interfaces/IGotrueStatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ public interface IGotrueStatelessClient<TUser, TSession>
Task<TUser?> UpdateUserById(string jwt, StatelessClientOptions options, string userId, AdminUserAttributes userData);
Task<TSession?> VerifyOTP(string phone, string token, StatelessClientOptions options, MobileOtpType type = MobileOtpType.SMS);
Task<TSession?> VerifyOTP(string email, string token, StatelessClientOptions options, EmailOtpType type = EmailOtpType.MagicLink);
Task<Settings?> Settings(StatelessClientOptions options);
}
}
11 changes: 4 additions & 7 deletions GotrueTests/AnonKeyClientFailureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace GotrueTests
[TestClass]
public class AnonKeyClientFailureTests
{
private readonly List<Constants.AuthState> _stateChanges = new List<Constants.AuthState>();

private IGotrueClient<User, Session> _client;
private TestSessionPersistence _persistence;

private void AuthStateListener(IGotrueClient<User, Session> sender, Constants.AuthState newState)
Expand All @@ -43,11 +46,6 @@ public void TestInitializer()
_client.AddStateChangedListener(AuthStateListener);
}


private Client _client;

private readonly List<Constants.AuthState> _stateChanges = new List<Constants.AuthState>();

[TestMethod("Client: Sign Up With Bad Password")]
public async Task SignUpUserEmailBadPassword()
{
Expand Down Expand Up @@ -85,7 +83,7 @@ public async Task SignUpUserPhone()
{
await _client.SignUp(Constants.SignUpType.Phone, phone1, PASSWORD, new SignUpOptions { Data = new Dictionary<string, object> { { "firstName", "Testing" } } });
});
AreEqual(UserMissingInformation, x.Reason);
AreEqual(UserBadPhoneNumber, x.Reason);
IsNull(_persistence.SavedSession);
Contains(_stateChanges, SignedOut);
AreEqual(1, _stateChanges.Count);
Expand Down Expand Up @@ -152,7 +150,6 @@ await ThrowsExceptionAsync<GotrueException>(async () =>
IsNotNull(result);
});
}

}

}
16 changes: 11 additions & 5 deletions GotrueTests/AnonKeyClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace GotrueTests
public class AnonKeyClientTests
{

private readonly List<Constants.AuthState> _stateChanges = new List<Constants.AuthState>();

private IGotrueClient<User, Session> _client;

private TestSessionPersistence _persistence;

private void AuthStateListener(IGotrueClient<User, Session> sender, Constants.AuthState newState)
Expand All @@ -43,10 +47,6 @@ public void TestInitializer()
_client.AddStateChangedListener(AuthStateListener);
}

private Client _client;

private readonly List<Constants.AuthState> _stateChanges = new List<Constants.AuthState>();

private void VerifyGoodSession(Session session)
{
Contains(_stateChanges, SignedIn);
Expand Down Expand Up @@ -89,7 +89,7 @@ public async Task SaveAndLoadUser()

var newPersistence = new TestSessionPersistence();
newPersistence.SaveSession(session);
var newClient = new Client(new ClientOptions { AllowUnconfirmedUserSessions = true });
IGotrueClient<User, Session> newClient = new Client(new ClientOptions { AllowUnconfirmedUserSessions = true });
newClient.SetPersistence(newPersistence);
newClient.AddDebugListener(LogDebug);
newClient.AddStateChangedListener(AuthStateListener);
Expand Down Expand Up @@ -331,5 +331,11 @@ public async Task ClientSendsResetPasswordForEmail()
var result = await _client.ResetPasswordForEmail(email);
IsTrue(result);
}

[TestMethod("Client: Get Settings")]
public async Task Settings()
{
await _client.Settings();
}
}
}
3 changes: 2 additions & 1 deletion GotrueTests/ConfigurationFailureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Supabase.Gotrue;
using Supabase.Gotrue.Exceptions;
using Supabase.Gotrue.Interfaces;
using static GotrueTests.TestUtils;
using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
using static Supabase.Gotrue.Exceptions.FailureHint.Reason;
Expand All @@ -29,7 +30,7 @@ await ThrowsExceptionAsync<HttpRequestException>(async () =>
[TestMethod("Bad service key message")]
public async Task BadServiceApiKeyTest()
{
var client = new Client(new ClientOptions { AllowUnconfirmedUserSessions = true });
IGotrueClient<User, Session> client = new Client(new ClientOptions { AllowUnconfirmedUserSessions = true });
client.AddDebugListener(LogDebug);

var x = await ThrowsExceptionAsync<GotrueException>(async () =>
Expand Down
3 changes: 2 additions & 1 deletion GotrueTests/ServiceRoleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Supabase.Gotrue;
using Supabase.Gotrue.Interfaces;
using static Supabase.Gotrue.Constants;
using static GotrueTests.TestUtils;
using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
Expand All @@ -14,7 +15,7 @@ namespace GotrueTests
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
public class ServiceRoleTests
{
private Client _client;
private IGotrueClient<User, Session> _client;

private readonly string _serviceKey = GenerateServiceRoleToken();

Expand Down
9 changes: 5 additions & 4 deletions GotrueTests/StatelessClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Supabase.Gotrue;
using Supabase.Gotrue.Exceptions;
using Supabase.Gotrue.Interfaces;
using static Supabase.Gotrue.StatelessClient;
using static Supabase.Gotrue.Constants;

Expand All @@ -23,6 +24,10 @@ public class StatelessClientTests

private static readonly Random Random = new Random();

private IGotrueStatelessClient<User, Session> _client;

private static StatelessClientOptions Options { get => new StatelessClientOptions() { AllowUnconfirmedUserSessions = true }; }

private static string RandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
Expand Down Expand Up @@ -59,17 +64,13 @@ private string GenerateServiceRoleToken()
return tokenHandler.WriteToken(securityToken);
}

private StatelessClient _client;


[TestInitialize]
public void TestInitializer()
{
_client = new StatelessClient();
}

private static StatelessClientOptions Options { get => new StatelessClientOptions() { AllowUnconfirmedUserSessions = true }; }

[TestMethod("StatelessClient: Settings")]
public async Task Settings()
{
Expand Down