Skip to content
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

Add SignUp using UserAttributes class #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion Gotrue/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
_headers = headers;
}


/// <summary>
/// Signs a user up using an email address and password.
/// </summary>
Expand Down Expand Up @@ -99,7 +100,61 @@
}
return null;
}



/// <summary>
/// Logs in an existing user using their email address.
/// </summary>
/// <param name="email"></param>
/// <param name="password"></param>
/// <returns></returns>
public Task<Session> SignInWithEmail(UserAttributes attributes, SignUpOptions options = null)

Check warning on line 111 in Gotrue/Api.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.

Check warning on line 111 in Gotrue/Api.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.
{
string endpoint = $"{Url}/token?grant_type=password";

if (options != null)
{
if (!string.IsNullOrEmpty(options.RedirectTo))
{
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo } }).ToString();
}

if (options.Data != null)
{
attributes.Data.Add("data", options.Data);
}
}
return Helpers.MakeRequest<Session>(HttpMethod.Post, endpoint, attributes, Headers);
}

/// <summary>
/// Signs up a new user using their phone number and a password.The phone number of the user.
/// </summary>
/// <param name="phone">The phone number of the user.</param>
/// <param name="password">The password of the user.</param>
/// <param name="options">Optional Signup data.</param>
/// <returns></returns>
public Task<Session> SignUpWithPhone(UserAttributes attributes, SignUpOptions options = null)

Check warning on line 137 in Gotrue/Api.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.

Check warning on line 137 in Gotrue/Api.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.
{

string endpoint = $"{Url}/signup";

if (options != null)
{
if (!string.IsNullOrEmpty(options.RedirectTo))
{
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo } }).ToString();
}

if (options.Data != null)
{
attributes.Data.Add("data", options.Data);
}
}

return Helpers.MakeRequest<Session>(HttpMethod.Post, endpoint, attributes, Headers);
}

/// <summary>
/// Logs in an existing user using their email address.
/// </summary>
Expand Down
64 changes: 64 additions & 0 deletions Gotrue/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,70 @@
return session;
}


/// <summary>
/// Signs up a user by email address
/// </summary>
/// <param name="email"></param>
/// <param name="password"></param>
/// <param name="options">Object containing redirectTo and optional user metadata (data)</param>
/// <returns></returns>
public Task<Session> SignUp(string email, string password, SignUpOptions options = null) => SignUp(SignUpType.Email, email, password, options);

Check failure on line 192 in Gotrue/Client.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Type 'Client' already defines a member called 'SignUp' with the same parameter types

Check warning on line 192 in Gotrue/Client.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.

Check warning on line 192 in Gotrue/Client.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.

Check failure on line 192 in Gotrue/Client.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Type 'Client' already defines a member called 'SignUp' with the same parameter types

public async Task<Session> SignUp(SignUpType type, UserAttributes attributes, SignUpOptions options = null)

Check warning on line 194 in Gotrue/Client.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.

Check warning on line 194 in Gotrue/Client.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.
{
await DestroySession();

try
{
Session session = null;
switch (type)
{
case SignUpType.Email:
session = await api.SignUpWithEmail(attributes, options);
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;
}
catch (RequestException ex)
{
Session session = null;
if (ex.Response.StatusCode == System.Net.HttpStatusCode.UnprocessableEntity)
{
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;
}
}

/// <inheritdoc />
public async Task<bool> SignIn(string email, SignInOptions? options = null)
{
Expand Down
Loading