Skip to content

Commit

Permalink
Add a more convenient overload of AddPasswordlessSdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Nov 23, 2023
1 parent 2d623b1 commit d71a96f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Add Passwordless to your service container:
// In Program.cs or Startup.cs
services.AddPasswordlessSdk(options =>
{
options.ApiKey = "your_api_key";
options.ApiSecret = "your_api_secret";
options.ApiKey = "your_api_key";
});
```

Expand Down
3 changes: 2 additions & 1 deletion examples/Passwordless.AspNetIdentity.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Passwordless.AspNetCore;
Expand All @@ -12,7 +13,7 @@
builder.Services.AddDataContext();
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<PasswordlessContext>()
.AddPasswordless(builder.Configuration.GetSection("Passwordless"));
.AddPasswordless(builder.Configuration.GetRequiredSection("Passwordless"));

builder.Services.AddRazorPages(options =>
{
Expand Down
15 changes: 6 additions & 9 deletions examples/Passwordless.Example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,24 @@ public Startup(IConfiguration configuration)
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// add support for routing to controllers
// Add support for routing to controllers
services.AddControllers();

// Inject the Passwordless SDK
services.AddPasswordlessSdk(options =>
{
Configuration.GetRequiredSection("Passwordless").Bind(options);
});
services.AddPasswordlessSdk(Configuration.GetRequiredSection("Passwordless"));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();

// add support for serving index.html
// Add support for serving index.html
app.UseDefaultFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); });
app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
}
}
2 changes: 1 addition & 1 deletion src/Passwordless.AspNetCore/IdentityBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ private static IServiceCollection AddPasswordlessCore(this IServiceCollection se
.Configure<IOptions<PasswordlessAspNetCoreOptions>>((options, aspNetCoreOptionsAccessor) =>
{
var aspNetCoreOptions = aspNetCoreOptionsAccessor.Value;
options.ApiSecret = aspNetCoreOptions.ApiSecret;
options.ApiUrl = aspNetCoreOptions.ApiUrl;
options.ApiSecret = aspNetCoreOptions.ApiSecret;
options.ApiKey = aspNetCoreOptions.ApiKey;
});

Expand Down
1 change: 1 addition & 0 deletions src/Passwordless/Passwordless.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
Expand Down
20 changes: 20 additions & 0 deletions src/Passwordless/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Passwordless;

Expand Down Expand Up @@ -33,4 +34,23 @@ public static IServiceCollection AddPasswordlessSdk(

return services;
}

/// <summary>
/// Adds and configures Passwordless-related services.
/// </summary>
public static IServiceCollection AddPasswordlessSdk(
this IServiceCollection services,
IConfiguration configuration) =>
services.AddPasswordlessSdk(o =>
{
o.ApiUrl =
configuration["ApiUrl"] ??
throw new InvalidOperationException("Midding 'ApiUrl' configuration value.");

o.ApiSecret =
configuration["ApiSecret"] ??
throw new InvalidOperationException("Midding 'ApiSecret' configuration value.");

o.ApiKey = configuration["ApiKey"];
});
}

0 comments on commit d71a96f

Please sign in to comment.