11#if ( UseAuth )
22using MCA . Application . Commands ;
33using MCA . Application . Handlers ;
4- using MCA . Domain . Entities ;
5- using Microsoft . AspNetCore . Identity ;
64using Microsoft . AspNetCore . Mvc ;
75using MinimalCleanArch . Domain . Common ;
86using System . Security . Claims ;
@@ -161,32 +159,32 @@ public static void MapAuthEndpoints(this IEndpointRouteBuilder app, bool isDevel
161159
162160 // SSR login — signs in via cookie for the authorization code flow
163161 auth . MapPost ( "/login" , async (
164- HttpContext httpContext ,
165162 [ FromBody ] LoginRequest request ,
166- [ FromServices ] SignInManager < ApplicationUser > signInManager ,
167- [ FromServices ] UserManager < ApplicationUser > userManager ) =>
163+ #if ( UseMessaging )
164+ IMessageBus bus ,
165+ CancellationToken cancellationToken ) =>
166+ #else
167+ [ FromServices ] AuthLoginHandler handler,
168+ CancellationToken cancellationToken) =>
169+ #endif
168170 {
169- var user = await userManager . FindByEmailAsync ( request . Email )
170- ?? await userManager . FindByNameAsync ( request . Email ) ;
171-
172- if ( user == null )
173- return Results . Unauthorized ( ) ;
174-
175- var result = await signInManager . CheckPasswordSignInAsync ( user , request . Password , lockoutOnFailure : true ) ;
171+ var command = new AuthLoginCommand ( request . Email , request . Password , request . ReturnUrl ) ;
172+ #if ( UseMessaging )
173+ var result = await bus . InvokeAsync < Result < AuthLoginResult > > ( command , cancellationToken ) ;
174+ #else
175+ var result = await handler . Handle ( command , cancellationToken ) ;
176+ #endif
177+ if ( result . IsSuccess )
178+ {
179+ return string . IsNullOrWhiteSpace ( result . Value . RedirectUrl )
180+ ? Results . Ok ( new { message = "Signed in" } )
181+ : Results . Ok ( new { message = "Signed in" , redirectUrl = result . Value . RedirectUrl } ) ;
182+ }
176183
177- if ( result . IsLockedOut )
184+ if ( result . Error . Code == "LOCKED_OUT" )
178185 return Results . Problem ( "Account is locked out." , statusCode : 423 ) ;
179186
180- if ( ! result . Succeeded )
181- return Results . Unauthorized ( ) ;
182-
183- await signInManager . SignInAsync ( user , isPersistent : false ) ;
184-
185- var returnUrl = request . ReturnUrl ;
186- if ( ! string . IsNullOrEmpty ( returnUrl ) && Uri . IsWellFormedUriString ( returnUrl , UriKind . Relative ) )
187- return Results . Ok ( new { message = "Signed in" , redirectUrl = returnUrl } ) ;
188-
189- return Results . Ok ( new { message = "Signed in" } ) ;
187+ return Results . Unauthorized ( ) ;
190188 } )
191189 . AllowAnonymous ( )
192190#if ( UseRateLimiting )
@@ -196,10 +194,19 @@ public static void MapAuthEndpoints(this IEndpointRouteBuilder app, bool isDevel
196194 . WithSummary ( "Sign in via cookie (for SSR/authorization code flow)" ) ;
197195
198196 auth . MapPost ( "/logout" , async (
199- HttpContext httpContext ,
200- [ FromServices ] SignInManager < ApplicationUser > signInManager ) =>
197+ #if ( UseMessaging )
198+ IMessageBus bus ,
199+ CancellationToken cancellationToken ) =>
200+ #else
201+ [ FromServices ] AuthLogoutHandler handler,
202+ CancellationToken cancellationToken) =>
203+ #endif
201204 {
202- await signInManager . SignOutAsync ( ) ;
205+ #if ( UseMessaging )
206+ await bus . InvokeAsync < Result > ( new AuthLogoutCommand ( ) , cancellationToken ) ;
207+ #else
208+ await handler . Handle ( new AuthLogoutCommand ( ) , cancellationToken ) ;
209+ #endif
203210 return Results . Ok ( new { message = "Signed out" } ) ;
204211 } )
205212 . AllowAnonymous ( )
@@ -238,36 +245,35 @@ public static void MapAuthEndpoints(this IEndpointRouteBuilder app, bool isDevel
238245 // SSR login form handler — POST /auth/login (development only)
239246 app . MapPost ( "/auth/login" , async (
240247 HttpContext context ,
241- [ FromServices ] SignInManager < ApplicationUser > signInManager ,
242- [ FromServices ] UserManager < ApplicationUser > userManager ) =>
248+ #if ( UseMessaging )
249+ IMessageBus bus ,
250+ CancellationToken cancellationToken ) =>
251+ #else
252+ [ FromServices ] AuthLoginHandler handler,
253+ CancellationToken cancellationToken) =>
254+ #endif
243255 {
244256 var form = await context . Request . ReadFormAsync ( ) ;
245257 var email = form [ "email" ] . ToString ( ) ;
246258 var password = form [ "password" ] . ToString ( ) ;
247259 var returnUrl = form [ "returnUrl" ] . ToString ( ) ;
248260
249- var user = await userManager . FindByEmailAsync ( email )
250- ?? await userManager . FindByNameAsync ( email ) ;
251-
252- if ( user == null )
253- return Results . Redirect (
254- $ "/auth/login?error=Invalid+credentials&returnUrl={ Uri . EscapeDataString ( returnUrl ) } ") ;
255-
256- var result = await signInManager . CheckPasswordSignInAsync ( user , password , lockoutOnFailure : true ) ;
261+ var command = new AuthLoginCommand ( email , password , returnUrl ) ;
262+ #if ( UseMessaging )
263+ var result = await bus . InvokeAsync < Result < AuthLoginResult > > ( command , cancellationToken ) ;
264+ #else
265+ var result = await handler . Handle ( command , cancellationToken ) ;
266+ #endif
267+ if ( result . IsSuccess )
268+ {
269+ return Results . Redirect ( result . Value . RedirectUrl ?? "/" ) ;
270+ }
257271
258- if ( result . IsLockedOut )
272+ if ( result . Error . Code == "LOCKED_OUT" )
259273 return Results . Redirect ( "/auth/login?error=Account+is+locked+out" ) ;
260274
261- if ( ! result . Succeeded )
262- return Results . Redirect (
263- $ "/auth/login?error=Invalid+credentials&returnUrl={ Uri . EscapeDataString ( returnUrl ) } ") ;
264-
265- await signInManager . SignInAsync ( user , isPersistent : false ) ;
266-
267- if ( ! string . IsNullOrEmpty ( returnUrl ) && Uri . IsWellFormedUriString ( returnUrl , UriKind . Relative ) )
268- return Results . Redirect ( returnUrl ) ;
269-
270- return Results . Redirect ( "/" ) ;
275+ return Results . Redirect (
276+ $ "/auth/login?error=Invalid+credentials&returnUrl={ Uri . EscapeDataString ( returnUrl ) } ") ;
271277 } )
272278 . AllowAnonymous ( )
273279#if ( UseRateLimiting )
0 commit comments