Rest Api. JWT auth. How to make api stateless? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Rest Api. JWT auth. How to make api stateless?

Here are endpoints I have public static void RegisterTodoListEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("api/lists").RequireAuthorization(); /// <summary> /// Returns lists of current user (Id, Title, Ammount of tasks) /// </summary> group.MapGet("", GetAllTodos); /// <summary> /// Create new todo list /// </summary> group.MapPost("", CreateTodoList); /// <summary> /// Return list and its tasks. 403 if user is not owner of list /// </summary> group.MapGet("/{listId}", GetTodoListById); /// <summary> /// Deletes list and its tasks. 403 if user is not owner of list /// </summary> group.MapDelete("{listId}", DeleteTodoList); /// <summary> /// Creates new task. 403 if user is not owner of list /// </summary> group.MapPost("/{listId}/tasks", CreateTodoTask); /// <summary> /// Updates new task. 403 if user is not owner of list /// </summary> group.MapPatch("{listId}/tasks/{taskId}", UpdateTodoTask); } Lets take a look at GetTodoListById. Using ClaimsPrincipal makes it not stateless? private static async Task<IResult> GetTodoListById( ISender sender, ClaimsPrincipal claimsPrincipal, [FromRoute] int listId) { var userId = int.Parse(claimsPrincipal.FindFirstValue(ClaimTypes.NameIdentifier)!); var query = new GetTodoListByIdQuery(listId, userId); var response = await sender.Send(query); if (response.IsFailure) { return response.AsTypedErrorResult(); } return TypedResults.Ok(response.Value); }

18th Apr 2024, 4:51 PM
Artur
Artur - avatar
1 Answer
+ 2
These are the best thing for your help. Our Sololearn already made community tutorials on API! Take a look at these: - https://www.sololearn.com/learn/10736/?ref=app - https://www.sololearn.com/learn/9566/?ref=app - https://www.sololearn.com/learn/10391/?ref=app You can find it by your own. Just go the practice section and at the top your find a search bar where you need to search API. That's it. Already a tutorial on RESTful API with Node.js & Express is available there or check the first link I provided.
18th Apr 2024, 6:16 PM
`нттp⁴⁰⁶
`нттp⁴⁰⁶ - avatar