From abc8b2283f65cc17d292f8caff12fe413d3b54b3 Mon Sep 17 00:00:00 2001 From: "Miles J. Litteral" Date: Tue, 23 Jul 2024 10:50:27 -0400 Subject: [PATCH 1/3] Update Api.cs --- Gotrue/Api.cs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/Gotrue/Api.cs b/Gotrue/Api.cs index 597d10b..8d6d85e 100644 --- a/Gotrue/Api.cs +++ b/Gotrue/Api.cs @@ -55,6 +55,7 @@ public Api(string url, Dictionary? headers = null) _headers = headers; } + /// /// Signs a user up using an email address and password. /// @@ -99,7 +100,61 @@ public Api(string url, Dictionary? headers = null) } return null; } - + + + /// + /// Logs in an existing user using their email address. + /// + /// + /// + /// + public Task SignInWithEmail(UserAttributes attributes, SignUpOptions options = null) + { + string endpoint = $"{Url}/token?grant_type=password"; + + if (options != null) + { + if (!string.IsNullOrEmpty(options.RedirectTo)) + { + endpoint = Helpers.AddQueryParams(endpoint, new Dictionary { { "redirect_to", options.RedirectTo } }).ToString(); + } + + if (options.Data != null) + { + attributes.Data.Add("data", options.Data); + } + } + return Helpers.MakeRequest(HttpMethod.Post, endpoint, attributes, Headers); + } + + /// + /// Signs up a new user using their phone number and a password.The phone number of the user. + /// + /// The phone number of the user. + /// The password of the user. + /// Optional Signup data. + /// + public Task SignUpWithPhone(UserAttributes attributes, SignUpOptions options = null) + { + + string endpoint = $"{Url}/signup"; + + if (options != null) + { + if (!string.IsNullOrEmpty(options.RedirectTo)) + { + endpoint = Helpers.AddQueryParams(endpoint, new Dictionary { { "redirect_to", options.RedirectTo } }).ToString(); + } + + if (options.Data != null) + { + attributes.Data.Add("data", options.Data); + } + } + + return Helpers.MakeRequest(HttpMethod.Post, endpoint, attributes, Headers); + } + /// /// Logs in an existing user using their email address. /// From 68ff35b33a5b2ff21d91fc3a42b8b661b1afc9e9 Mon Sep 17 00:00:00 2001 From: "Miles J. Litteral" Date: Tue, 23 Jul 2024 10:54:39 -0400 Subject: [PATCH 2/3] Update Client.cs --- Gotrue/Client.cs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/Gotrue/Client.cs b/Gotrue/Client.cs index 2a8946f..1d243bc 100644 --- a/Gotrue/Client.cs +++ b/Gotrue/Client.cs @@ -181,6 +181,72 @@ public void ClearStateChangedListeners() return session; } + + /// + /// Signs up a user by email address + /// + /// + /// + /// Object containing redirectTo and optional user metadata (data) + /// + public Task SignUp(string email, string password, SignUpOptions options = null) => SignUp(SignUpType.Email, email, password, options); + + public async Task SignUp(SignUpType type, UserAttributes attributes, SignUpOptions options = null) + { + await DestroySession(); + + try + { + Session session = null; + switch (type) + { + case SignUpType.Email: + session = await api.SignUpWithEmail(attributes, options); + break; + case SignUpType.Phone: + UnityEngine.Debug.LogError("Phone Sign in with User Attributes currently Unsupported"); + session = await api.SignUpWithPhone(attributes, options); + break; + } + + if (session?.User?.ConfirmedAt != null || (session.User != null && Options.AllowUnconfirmedUserSessions)) + { + await PersistSession(session); + + StateChanged?.Invoke(this, new ClientStateChanged(AuthState.SignedIn)); + + return CurrentSession; + } + + return session; + } + catch (RequestException ex) + { + Session session = null; + if (ex.Response.StatusCode == System.Net.HttpStatusCode.UnprocessableEntity) + { + UnityEngine.Debug.LogWarning($"User already exists, login user instead."); + switch (type) + { + case SignUpType.Email: + session = await api.SignInWithEmail(attributes); + break; + case SignUpType.Phone: + session = await api.SignUpWithPhone(attributes, options); + break; + } + + if (session?.User?.ConfirmedAt != null || (session.User != null && Options.AllowUnconfirmedUserSessions)) + { + await PersistSession(session); + StateChanged?.Invoke(this, new ClientStateChanged(AuthState.SignedIn)); + return CurrentSession; + } + } + return session; + } + } + /// public async Task SignIn(string email, SignInOptions? options = null) { From 31fbfe782254308d03a9e382d3f2e58f3eb8a407 Mon Sep 17 00:00:00 2001 From: "Miles J. Litteral" Date: Tue, 23 Jul 2024 11:02:26 -0400 Subject: [PATCH 3/3] Update Client.cs --- Gotrue/Client.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Gotrue/Client.cs b/Gotrue/Client.cs index 1d243bc..443f71e 100644 --- a/Gotrue/Client.cs +++ b/Gotrue/Client.cs @@ -204,7 +204,6 @@ public async Task SignUp(SignUpType type, UserAttributes attributes, Si session = await api.SignUpWithEmail(attributes, options); break; case SignUpType.Phone: - UnityEngine.Debug.LogError("Phone Sign in with User Attributes currently Unsupported"); session = await api.SignUpWithPhone(attributes, options); break; } @@ -225,7 +224,6 @@ public async Task SignUp(SignUpType type, UserAttributes attributes, Si Session session = null; if (ex.Response.StatusCode == System.Net.HttpStatusCode.UnprocessableEntity) { - UnityEngine.Debug.LogWarning($"User already exists, login user instead."); switch (type) { case SignUpType.Email: