Skip to content

Getting Started with GrydAuth ​

The maintained host templates are the executable setup for GrydAuth. For a new Auth API, create a gryd-api project with Auth enabled:

bash
dotnet new gryd-api -n MyApi --IncludeAuth true

The exact controller registration, API versioning, Auth service registration, exception handlers, middleware order, and Development migration call are in the template composition root. The template is built by the repository's template validation workflow. Its README documents local PostgreSQL, Redis, RSA key setup, and first-run steps: gryd-api README.

Required composition ​

For a custom host, preserve the same parts shown in the template:

  • Add controllers and include typeof(AuthController).Assembly as an application part.
  • Call AddGrydApiVersioning() before AddGrydSwagger() when Swagger is used.
  • Register AddGrydAuth(builder.Configuration), then the Auth-specific and Core exception handlers.
  • In the pipeline, call UseRouting(), UseAuthentication(), UseGrydAuth(), UseAuthorization(), and MapControllers() in that order.
  • In Development, the template calls ApplyGrydAuthMigrationsAsync() with GrantSeedAdminConfiguredAppId = true, so the seeded admin (admin@local.com) can sign in with the host's JwtSettings:AppId. The migration seed binds only the framework's development AppId; this step adds the configured one at startup, never removes the seeded one and never restores an AppId that was revoked. Production migration rollout is an explicit deployment responsibility; do not run automatic startup migrations there by copying the Development block.

The list above describes the parts to preserve, not a second standalone project recipe. The linked, maintained template is the complete source for namespaces, package references, and host configuration.

Database configuration ​

GrydAuth's identity store uses PostgreSQL through ConnectionStrings:DefaultPostgresConnection. This remains separate from the host's business DbContext and its ConnectionStrings:DefaultConnection; AddGrydAuth does not replace the business context. The gryd-api template supports PostgreSQL or SQL Server for its business context, while Auth still needs its PostgreSQL connection. A SQL Server choice for the business context does not change Auth's provider requirement.

The generated Development settings show the expected connection-string keys. JSON values such as ${POSTGRES_PASSWORD} are placeholders, not automatic .NET configuration interpolation. Set the complete local values using user-secrets or environment variables, and keep credentials and private keys out of source control. The template README gives concrete local setup commands and secret names.

Authentication boundary ​

AddGrydAuth configures the Auth authority and its API. A remote API that only validates tokens uses the separate remote-consumer registration and middleware contract described in Remote consumer; it does not register the Auth identity pipelines.

See Authentication, Authorization, and Multi-tenancy for runtime contracts and frontend behavior.

Database transactions ​

Inject the scoped IGrydAuthUnitOfWork through normal ASP.NET Core DI and use its value-returning or void ExecuteInTransactionAsync overload. Successful completion commits. A failure is rethrown, but rollback can itself fail and a commit error can leave the database outcome unknown. Nested calls on the same context join the outer transaction.

The Auth execution strategy may retry transaction creation only when no transaction was returned. After work starts, a save, callback, or commit failure makes that GrydAuthDbContext unusable for further writes and prevents replaying the operation against its tracked state. Use a new context to verify or continue work. Do not assume an uncertain commit rolled back or blindly repeat a non-idempotent operation. Keep email and other external side effects outside transaction callbacks.

Given an IServiceScopeFactory scopeFactory, Guid userId, and cancellation token, resolve the verification context from a new DI scope so it does not reuse the failed scoped GrydAuthDbContext:

csharp
using GrydAuth.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

await using var verificationScope = scopeFactory.CreateAsyncScope();
var verificationContext = verificationScope.ServiceProvider.GetRequiredService<GrydAuthDbContext>();
var isActive = await verificationContext.Users.AsNoTracking()
    .Where(user => user.Id == userId)
    .Select(user => user.IsActive)
    .SingleAsync(cancellationToken);

Check the expected field value, not just whether the row exists. Use the observed state and the operation's idempotency rules before deciding whether to issue a new command.

The maintained PostgreSQL integration tests exercise both commit overloads, rollback after confirmed SQL writes, and nested joins through the public DI registration. This guarantee is database scoped; it does not make external effects atomic.

Released under the MIT License.