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 extra endpoint to add an alias #19

Merged
merged 8 commits into from
Sep 25, 2023
Merged
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
3 changes: 1 addition & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PropertyGroup>
<PasswordlessMajorVersion>1</PasswordlessMajorVersion>
<PasswordlessMinorVersion>0</PasswordlessMinorVersion>
<PasswordlessPatchVersion>3</PasswordlessPatchVersion>
<PasswordlessPatchVersion>4</PasswordlessPatchVersion>
<PasswordlessMajorMinorVersion>$(PasswordlessMajorVersion).$(PasswordlessMinorVersion)</PasswordlessMajorMinorVersion>
<VersionPrefix>$(PasswordlessMajorMinorVersion).$(PasswordlessPatchVersion)</VersionPrefix>
</PropertyGroup>
Expand All @@ -31,5 +31,4 @@
-->
<Using Remove="System.Net.Http" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Passwordless.Helpers;
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(AddAliasRequest))]
[JsonSerializable(typeof(SetAliasRequest))]
[JsonSerializable(typeof(RegisterTokenResponse))]
[JsonSerializable(typeof(RegisterOptions))]
[JsonSerializable(typeof(VerifyTokenRequest))] // TODO: Use this with JsonContent.Create
Expand Down
6 changes: 3 additions & 3 deletions src/Passwordless/IPasswordlessClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Passwordless.Models;
using Passwordless.Models;

namespace Passwordless;

Expand All @@ -8,12 +8,12 @@ namespace Passwordless;
public interface IPasswordlessClient
{
/// <summary>
/// Adds one or more aliases to an existing user.
/// Sets one or more aliases to an existing user and removes existing aliases that are not included in the request.
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task AddAliasAsync(AddAliasRequest request, CancellationToken cancellationToken = default);
Task SetAliasAsync(SetAliasRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Creates a <see cref="RegisterTokenResponse" /> which will be used by your frontend to negotiate
Expand Down
37 changes: 0 additions & 37 deletions src/Passwordless/Models/AddAliasRequest.cs

This file was deleted.

51 changes: 51 additions & 0 deletions src/Passwordless/Models/SetAliasRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Passwordless.Models;

public class SetAliasRequest
{

/// <summary>
/// Sets a single alias for a given user, and removes any other aliases that may exist.
/// </summary>
/// <param name="userId"></param>
/// <param name="alias"></param>
/// <param name="hashing"></param>
public SetAliasRequest(string userId, string alias, bool hashing = true)
: this(userId, new HashSet<string> { alias }, hashing)
{
}

/// <summary>
/// Sets one or more aliases for a given user, and removes any other aliases that may exist.
/// </summary>
/// <param name="userId"></param>
/// <param name="aliases"></param>
/// <param name="hashing"></param>
/// <exception cref="ArgumentNullException"></exception>
public SetAliasRequest(string userId, HashSet<string> aliases, bool hashing = true)
{
UserId = userId ?? throw new ArgumentNullException(nameof(userId));
Hashing = hashing;

Aliases = aliases == null
? new HashSet<string>()
: new HashSet<string>(aliases.Where(x => !string.IsNullOrWhiteSpace(x)));
}

public string UserId { get; }

public IReadOnlyCollection<string> Aliases { get; }

/// <summary>
/// If you want your aliases to be available in plain text, set the <see cref="bool"/> false.
/// </summary>
public bool Hashing { get; }

/// <summary>
/// Removes all aliases from a user.
/// </summary>
/// <param name="userId"></param>
public static SetAliasRequest Empty(string userId)
{
return new SetAliasRequest(userId, new HashSet<string>(), true);
}
}
4 changes: 2 additions & 2 deletions src/Passwordless/PasswordlessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public PasswordlessClient(HttpClient client)
}

/// <inheritdoc/>
public async Task AddAliasAsync(AddAliasRequest request, CancellationToken cancellationToken)
public async Task SetAliasAsync(SetAliasRequest request, CancellationToken cancellationToken)
{
var res = await _client.PostAsJsonAsync("alias",
request,
PasswordlessSerializerContext.Default.AddAliasRequest,
PasswordlessSerializerContext.Default.SetAliasRequest,
cancellationToken);
res.EnsureSuccessStatusCode();
}
Expand Down
Loading