diff --git a/.gitignore b/.gitignore index 6786d1dc..049d5f46 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,9 @@ *.userosscache *.sln.docstates +*.db-shm +*.db-wal + # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/Directory.Build.props b/Directory.Build.props index 7b23949a..96fa6cdf 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 0.1.0-preview.2 + 0.1.0-rc.1 $(NoWarn);1591 CodeBeam diff --git a/docs/content/getting-started/quickstart.md b/docs/content/getting-started/quickstart.md index 8d77e763..77853bb1 100644 --- a/docs/content/getting-started/quickstart.md +++ b/docs/content/getting-started/quickstart.md @@ -12,7 +12,7 @@ In this guide, you will set up UltimateAuth in a few minutes and perform your ** ## 1. Create a Project -Create a new Blazor Server web app: +Start by creating a new Blazor app: ```bash dotnet new blazorserver -n UltimateAuthDemo @@ -21,7 +21,7 @@ cd UltimateAuthDemo ## 2. Install Packages -Add UltimateAuth packages: +Install the required UltimateAuth packages: ```csharp dotnet add package CodeBeam.UltimateAuth.Server @@ -69,7 +69,46 @@ Replace `Routes.razor` with this code: ``` -## 8. Perform Your First Login +## 8. Recommended Setup (Optional) +Add these for better experience: + +For login page (Use this only once in your application) +```csharp +@attribute [UAuthLoginPage] +``` + +For protected pages +```csharp +@attribute [UAuthAuthorize] +``` + +For any page that you use UltimateAuth features like AuthState etc. +```csharp +@inherits UAuthFlowPageBase +``` + +## 9. Seed Data For QuickStart (Optional) +This code creates admin and user users with same password and admin role. + +For in memory +```csharp +builder.Services.AddUltimateAuthSampleSeed(); +``` + +For entity framework core: +```csharp +builder.Services.AddScopedUltimateAuthSampleSeed(); +``` + +In pipeline configuration +```csharp +if (app.Environment.IsDevelopment()) +{ + await app.SeedUltimateAuthAsync(); +} +``` + +## 10. Perform Your First Login Example using IUAuthClient: ```csharp [Inject] IUAuthClient UAuthClient { get; set; } = null!; @@ -78,8 +117,8 @@ private async Task Login() { await UAuthClient.Flows.LoginAsync(new LoginRequest { - Identifier = "demo", - Secret = "password" + Identifier = "admin", + Secret = "admin" }); } ``` diff --git a/docs/content/getting-started/real-world-setup.md b/docs/content/getting-started/real-world-setup.md index 574cf928..92afaeef 100644 --- a/docs/content/getting-started/real-world-setup.md +++ b/docs/content/getting-started/real-world-setup.md @@ -65,7 +65,7 @@ builder.Services.AddUltimateAuthServer(o => { }); ``` -## Blazor WASM Setup +## Blazor Standalone WASM Setup Blazor WASM applications run entirely on the client and cannot securely handle credentials. For this reason, UltimateAuth uses a dedicated Auth server called **UAuthHub**. @@ -91,6 +91,42 @@ app.MapUltimateAuthEndpoints(); app.MapUAuthHub(); ``` +## Blazor Web App Setup +A blazor web app contains two projects that includes host and client. You need to arrange them both. + +In the host project: +```csharp +builder.Services.AddUltimateAuthClientBlazor(o => +{ + o.Endpoints.BasePath = "https://localhost:6112/auth"; // UAuthHub URL + o.Pkce.ReturnUrl = "https://localhost:6132/home"; // Current application domain + path +}); + +// In pipeline configuration +app.MapRazorComponents() + .AddInteractiveWebAssemblyRenderMode() + .AddAdditionalAssemblies(UAuthAssemblies.BlazorClient().First()); +``` + +In the client project: +```csharp +builder.Services.AddUltimateAuthClientBlazor(o => +{ + o.Endpoints.BasePath = "https://localhost:6112/auth"; // UAuthHub URL + o.Pkce.ReturnUrl = "https://localhost:6132/home"; // Current application domain + path +}); + +builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); + +// Optional if you use external API calls in your client project. +builder.Services.AddHttpClient("resourceApi", client => +{ + client.BaseAddress = new Uri("https://localhost:6122"); +}); +``` + +> If you want to use embedded UAuthHub in host project, you can register server services as shown in quickstart. + > ℹ️ UltimateAuth automatically selects the appropriate authentication mode (PureOpaque, Hybrid, etc.) based on the client type. ## ResourceApi Setup diff --git a/samples/CodeBeam.UltimateAuth.Sample.Seed/CodeBeam.UltimateAuth.Sample.Seed.csproj b/samples/CodeBeam.UltimateAuth.Sample.Seed/CodeBeam.UltimateAuth.Sample.Seed.csproj index 92487581..cc80e9e8 100644 --- a/samples/CodeBeam.UltimateAuth.Sample.Seed/CodeBeam.UltimateAuth.Sample.Seed.csproj +++ b/samples/CodeBeam.UltimateAuth.Sample.Seed/CodeBeam.UltimateAuth.Sample.Seed.csproj @@ -1,13 +1,22 @@  - net10.0 - enable - enable + net8.0;net9.0;net10.0 + $(NoWarn);1591 + CodeBeam.UltimateAuth.Sample.Seed + + Minimal seeded data for UltimateAuth samples and quickstart projects. + + authentication;session;identity;auth-framework;seed;data + uauthlogo.png + + + + diff --git a/samples/CodeBeam.UltimateAuth.Sample.Seed/Extensions/UAuthSeedExtensions.cs b/samples/CodeBeam.UltimateAuth.Sample.Seed/Extensions/UAuthSeedExtensions.cs new file mode 100644 index 00000000..aeea00f2 --- /dev/null +++ b/samples/CodeBeam.UltimateAuth.Sample.Seed/Extensions/UAuthSeedExtensions.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using CodeBeam.UltimateAuth.Core.Infrastructure; +using CodeBeam.UltimateAuth.Core.MultiTenancy; + +namespace CodeBeam.UltimateAuth.Sample.Seed.Extensions; + +public static class UAuthSeedExtensions +{ + public static async Task SeedUltimateAuthAsync(this WebApplication app, TenantKey? tenant = null, CancellationToken ct = default) + { + using var scope = app.Services.CreateScope(); + var runner = scope.ServiceProvider.GetRequiredService(); + await runner.RunAsync(tenant, ct); + } +} diff --git a/samples/CodeBeam.UltimateAuth.Sample.Seed/uauthlogo.png b/samples/CodeBeam.UltimateAuth.Sample.Seed/uauthlogo.png new file mode 100644 index 00000000..911f2530 Binary files /dev/null and b/samples/CodeBeam.UltimateAuth.Sample.Seed/uauthlogo.png differ diff --git a/samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub.EFCore/Program.cs b/samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub.EFCore/Program.cs index 125730ca..0de4d2c1 100644 --- a/samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub.EFCore/Program.cs +++ b/samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub.EFCore/Program.cs @@ -71,10 +71,9 @@ using (var scope = app.Services.CreateScope()) { await UAuthDbInitializer.InitializeAsync(app.Services, reset: true); - - var seedRunner = scope.ServiceProvider.GetRequiredService(); - await seedRunner.RunAsync(null); } + + await app.SeedUltimateAuthAsync(); } app.UseHttpsRedirection(); diff --git a/samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub/Program.cs b/samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub/Program.cs index 9784acb8..1182dcf7 100644 --- a/samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub/Program.cs +++ b/samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub/Program.cs @@ -63,9 +63,7 @@ app.MapOpenApi(); app.MapScalarApiReference(); - using var scope = app.Services.CreateScope(); - var seedRunner = scope.ServiceProvider.GetRequiredService(); - await seedRunner.RunAsync(null); + await app.SeedUltimateAuthAsync(); } app.UseHttpsRedirection(); diff --git a/samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer.EFCore/Program.cs b/samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer.EFCore/Program.cs index 377eb8b1..99fe67c7 100644 --- a/samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer.EFCore/Program.cs +++ b/samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer.EFCore/Program.cs @@ -89,12 +89,9 @@ using (var scope = app.Services.CreateScope()) { await UAuthDbInitializer.InitializeAsync(app.Services, reset: true); - - var seedRunner = scope.ServiceProvider.GetRequiredService(); - await seedRunner.RunAsync(null); } + await app.SeedUltimateAuthAsync(); } - app.UseForwardedHeaders(); app.UseHttpsRedirection(); diff --git a/samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Program.cs b/samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Program.cs index 7b54009b..074e07a3 100644 --- a/samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Program.cs +++ b/samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Program.cs @@ -82,9 +82,7 @@ app.MapOpenApi(); app.MapScalarApiReference(); - using var scope = app.Services.CreateScope(); - var seedRunner = scope.ServiceProvider.GetRequiredService(); - await seedRunner.RunAsync(null); + await app.SeedUltimateAuthAsync(); } app.UseForwardedHeaders();