Skip to content

Commit

Permalink
Fixes #16: CreateUser signature exchanges object userdata with `A…
Browse files Browse the repository at this point in the history
…dminUserAttributes attributes`.
  • Loading branch information
acupofjose committed Dec 24, 2021
1 parent 7b45b8d commit e76c3aa
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 18 deletions.
11 changes: 2 additions & 9 deletions Gotrue/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,9 @@ internal Dictionary<string, string> TransformListUsersParams(string filter = nul
/// <param name="password"></param>
/// <param name="userData"></param>
/// <returns></returns>
public Task<User> CreateUser(string jwt, string email, string password, object userData = null)
public Task<User> CreateUser(string jwt, AdminUserAttributes attributes = null)
{
var data = new Dictionary<string, object> { { "email", email }, { "password", password } };

if (userData != null)
{
data.Add("data", userData);
}

return Helpers.MakeRequest<User>(HttpMethod.Post, $"{Url}/admin/users", data, CreateAuthedRequestHeaders(jwt));
return Helpers.MakeRequest<User>(HttpMethod.Post, $"{Url}/admin/users", attributes, CreateAuthedRequestHeaders(jwt));
}

/// <summary>
Expand Down
26 changes: 22 additions & 4 deletions Gotrue/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,18 +521,36 @@ public async Task<User> GetUserById(string jwt, string userId)
}

/// <summary>
/// Create a user
/// Create a user (as a service_role)
/// </summary>
/// <param name="jwt">A valid JWT. Must be a full-access API key (e.g. service_role key).</param>
/// <param name="email"></param>
/// <param name="password"></param>
/// <param name="userData"></param>
/// <param name="attributes"></param>
/// <returns></returns>
public Task<User> CreateUser(string jwt, string email, string password, AdminUserAttributes attributes = null)
{
if (attributes == null)
{
attributes = new AdminUserAttributes();
}
attributes.Email = email;
attributes.Password = password;

return CreateUser(jwt, attributes);
}

/// <summary>
/// Create a user (as a service_role)
/// </summary>
/// <param name="jwt">A valid JWT. Must be a full-access API key (e.g. service_role key).</param>
/// <param name="attributes"></param>
/// <returns></returns>
public async Task<User> CreateUser(string jwt, string email, string password, object userData = null)
public async Task<User> CreateUser(string jwt, AdminUserAttributes attributes)
{
try
{
return await api.CreateUser(jwt, email, password, userData);
return await api.CreateUser(jwt, attributes);
}
catch (RequestException ex)
{
Expand Down
24 changes: 21 additions & 3 deletions Gotrue/StatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,31 @@ public static async Task<User> GetUserById(string jwt, StatelessClientOptions op
/// <param name="jwt">A valid JWT. Must be a full-access API key (e.g. service_role key).</param>
/// <param name="email"></param>
/// <param name="password"></param>
/// <param name="userData"></param>
/// <param name="attributes"></param>
/// <returns></returns>
public static Task<User> CreateUser(string jwt, StatelessClientOptions options, string email, string password, AdminUserAttributes attributes = null)
{
if (attributes == null)
{
attributes = new AdminUserAttributes();
}
attributes.Email = email;
attributes.Password = password;

return CreateUser(jwt, options, attributes);
}

/// <summary>
/// Create a user
/// </summary>
/// <param name="jwt">A valid JWT. Must be a full-access API key (e.g. service_role key).</param>
/// <param name="attributes"></param>
/// <returns></returns>
public static async Task<User> CreateUser(string jwt, StatelessClientOptions options, string email, string password, object userData = null)
public static async Task<User> CreateUser(string jwt, StatelessClientOptions options, AdminUserAttributes attributes)
{
try
{
return await GetApi(options).CreateUser(jwt, email, password, userData);
return await GetApi(options).CreateUser(jwt, attributes);
}
catch (RequestException ex)
{
Expand Down
34 changes: 34 additions & 0 deletions Gotrue/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,40 @@ public class User
public string UpdatedAt { get; set; }
}

public class AdminUserAttributes: UserAttributes
{
/// <summary>
/// A custom data object for user_metadata. Can be any JSON serializable data.
/// Only a service role can modify.
/// </summary>
[JsonProperty("user_metadata")]
public Dictionary<string, object> UserMetadata { get; set; } = new Dictionary<string, object>();

/// <summary>
/// A custom data object for app_metadata that. Can be any JSON serializable data.
/// Only a service role can modify
///
/// Note: GoTrue does not yest support creating a user with app metadata
/// (see: https://github.com/supabase/gotrue-js/blob/d7b334a4283027c65814aa81715ffead262f0bfa/test/GoTrueApi.test.ts#L45)
/// </summary>
[JsonProperty("app_metadata")]
public Dictionary<string, object> AppMetadata { get; set; } = new Dictionary<string, object>();

/// <summary>
/// Sets if a user has confirmed their email address.
/// Only a service role can modify
/// </summary>
[JsonProperty("email_confirm")]
public bool EmailConfirm { get; set; }

/// <summary>
/// Sets if a user has confirmed their phone number.
/// Only a service role can modify
/// </summary>
[JsonProperty("phone_confirm")]
public bool PhoneConfirm { get; set; }
}

public class UserAttributes
{
[JsonProperty("email")]
Expand Down
15 changes: 14 additions & 1 deletion GotrueTests/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,20 @@ public async Task ClientCreateUser()
var service_role_key = GenerateServiceRoleToken();
var result = await client.CreateUser(service_role_key, $"{RandomString(12)}@supabase.io", password);

Assert.IsNotNull(result);
Assert.IsNotNull(result);


var attributes = new AdminUserAttributes
{
UserMetadata = new Dictionary<string, object> { { "firstName", "123" } },
AppMetadata = new Dictionary<string, object> { { "roles", new List<string> { "editor", "publisher" } } }
};

var result2 = await client.CreateUser(service_role_key, $"{RandomString(12)}@supabase.io", password, attributes);
Assert.AreEqual("123", result2.UserMetadata["firstName"]);

var result3 = await client.CreateUser(service_role_key, new AdminUserAttributes { Email = $"{RandomString(12)}@supabase.io", Password = password });
Assert.IsNotNull(result3);
}


Expand Down
13 changes: 12 additions & 1 deletion GotrueTests/StatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,18 @@ public async Task ClientCreateUser()
var service_role_key = GenerateServiceRoleToken();
var result = await CreateUser(service_role_key, options, $"{RandomString(12)}@supabase.io", password);

Assert.IsNotNull(result);
Assert.IsNotNull(result);

var attributes = new AdminUserAttributes
{
UserMetadata = new Dictionary<string, object> { { "firstName", "123" } },
};

var result2 = await CreateUser(service_role_key, options, $"{RandomString(12)}@supabase.io", password, attributes);
Assert.AreEqual("123", result2.UserMetadata["firstName"]);

var result3 = await CreateUser(service_role_key, options, new AdminUserAttributes { Email = $"{RandomString(12)}@supabase.io", Password = password });
Assert.IsNotNull(result3);
}

[TestMethod("Client: Update User by Id")]
Expand Down

0 comments on commit e76c3aa

Please sign in to comment.