1- using Asp . Versioning ;
1+ using System . Security . Claims ;
2+ using Asp . Versioning ;
3+ using Microsoft . AspNetCore . Mvc ;
24using whateverAPI . Helpers ;
35using whateverAPI . Models ;
46using whateverAPI . Services ;
@@ -9,39 +11,105 @@ public class AuthEndpoints : IEndpoints
911{
1012 public static void MapEndpoints ( IEndpointRouteBuilder app )
1113 {
12- var apiGroup = app . MapGroup ( "/api" ) ;
13- var googleAuthGroup2 = app
14- . MapGroup ( "/api/auth/google" )
15- . WithTags ( "Authentication" )
16- . RequireRateLimiting ( Helper . AuthPolicy ) ;
17- var microsoftAuthGroup2 = app
18- . MapGroup ( "/api/auth/microsoft" )
19- . WithTags ( "Authentication" )
20- . RequireRateLimiting ( Helper . AuthPolicy ) ;
21- var facebookAuthGroup2 = app
22- . MapGroup ( "/api/auth/facebook" )
23- . WithTags ( "Authentication" )
24- . RequireRateLimiting ( Helper . AuthPolicy ) ;
25-
26-
2714 var microsoftAuthGroup = app . NewVersionedApi ( )
2815 . MapGroup ( "/api/v{version:apiVersion}/auth/microsoft" )
2916 . WithTags ( "Authentication" )
3017 . HasApiVersion ( new ApiVersion ( 1 , 0 ) )
3118 . RequireRateLimiting ( Helper . AuthPolicy ) ;
32-
19+
3320 var googleAuthGroup = app . NewVersionedApi ( )
3421 . MapGroup ( "/api/v{version:apiVersion}/auth/google" )
3522 . WithTags ( "Authentication" )
3623 . HasApiVersion ( new ApiVersion ( 1 , 0 ) )
3724 . RequireRateLimiting ( Helper . AuthPolicy ) ;
38-
25+
3926 var facebookAuthGroup = app . NewVersionedApi ( )
4027 . MapGroup ( "/api/v{version:apiVersion}/auth/facebook" )
4128 . WithTags ( "Authentication" )
4229 . HasApiVersion ( new ApiVersion ( 1 , 0 ) )
4330 . RequireRateLimiting ( Helper . AuthPolicy ) ;
4431
32+ var authGroup = app . NewVersionedApi ( )
33+ . MapGroup ( "/api/v{version:apiVersion}/auth" )
34+ . WithTags ( "Authentication" )
35+ . HasApiVersion ( new ApiVersion ( 1 , 0 ) ) ;
36+
37+
38+ authGroup . MapGet ( "/status" , async Task < IResult > (
39+ HttpContext context ,
40+ IJwtTokenService jwtTokenService ) =>
41+ {
42+ var token = jwtTokenService . GetToken ( ) ;
43+ if ( string . IsNullOrEmpty ( token ) )
44+ {
45+ return TypedResults . Ok ( new { isAuthenticated = false } ) ;
46+ }
47+
48+ var userId = context . User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
49+ var email = context . User . FindFirst ( ClaimTypes . Email ) ? . Value ;
50+ var name = context . User . FindFirst ( ClaimTypes . Name ) ? . Value ;
51+ var role = context . User . FindFirst ( ClaimTypes . Role ) ? . Value ;
52+
53+ return TypedResults . Ok ( new
54+ {
55+ isAuthenticated = true ,
56+ userId ,
57+ email ,
58+ name ,
59+ role
60+ } ) ;
61+ } )
62+ . WithName ( "AuthStatus" )
63+ . WithDescription ( "Checks if the user is currently authenticated and returns their basic information" )
64+ . WithSummary ( "Get authentication status" )
65+ . WithOpenApi ( )
66+ . Produces < object > ( StatusCodes . Status200OK ) ;
67+ // .RequireAuthorization(Helper.RequireAuthenticatedUser);
68+
69+
70+ authGroup . MapPost ( "/login" , async Task < IResult > (
71+ [ FromBody ] UserLoginRequest request ,
72+ IJwtTokenService jwtTokenService ,
73+ HttpContext context ) =>
74+ {
75+ var jwtToken =
76+ await jwtTokenService . GenerateToken ( Guid . CreateVersion7 ( ) . ToString ( ) , request . Email , request . Name , "local" ) ;
77+ return ! string . IsNullOrEmpty ( jwtToken )
78+ ? TypedResults . Ok ( new { request . Email , Token = jwtToken } )
79+ : context . CreateUnauthorizedProblem ( "Invalid credentials provided" ) ;
80+ } )
81+ . WithName ( "UserLogin" )
82+ . WithDescription ( "Authenticates a user and returns a JWT token for subsequent requests" )
83+ . WithSummary ( "Login user" )
84+ . WithOpenApi ( )
85+ . Accepts < UserLoginRequest > ( "application/json" )
86+ . Produces < object > ( StatusCodes . Status200OK )
87+ . ProducesProblem ( StatusCodes . Status401Unauthorized )
88+ . ProducesValidationProblem ( StatusCodes . Status400BadRequest )
89+ . AddEndpointFilter < ValidationFilter < UserLoginRequest > > ( ) ;
90+
91+
92+ // User Logout
93+ authGroup . MapPost ( "/logout" , async Task < IResult > (
94+ [ FromServices ] IJwtTokenService jwtTokenService ,
95+ HttpContext context ) =>
96+ {
97+ var token = jwtTokenService . GetToken ( ) ;
98+ if ( string . IsNullOrEmpty ( token ) )
99+ {
100+ return context . CreateUnauthorizedProblem ( "No valid authentication token found" ) ;
101+ }
102+
103+ jwtTokenService . InvalidateToken ( token ) ;
104+ return TypedResults . Ok ( ) ;
105+ } )
106+ . WithName ( "UserLogout" )
107+ . WithDescription ( "Invalidates the current user's JWT token" )
108+ . WithSummary ( "Logout user" )
109+ . WithOpenApi ( )
110+ . Produces ( StatusCodes . Status200OK )
111+ . ProducesProblem ( StatusCodes . Status401Unauthorized ) ;
112+
45113// Endpoint to start the OAuth flow
46114 googleAuthGroup . MapGet ( "/login" , async Task < IResult > (
47115 IGoogleAuthService authService ,
0 commit comments