Skip to content
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 @@ public Api(string url, Dictionary<string, string>? headers = null)
_headers = headers;
}


/// <summary>
/// Signs a user up using an email address and password.
/// </summary>
Expand Down Expand Up @@ -99,7 +100,61 @@ public Api(string url, Dictionary<string, string>? headers = null)
}
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)
{
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)
{

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 @@ public void ClearStateChangedListeners()
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);

public async Task<Session> 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:
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