From a77c7bff39574428953c162b17d4dee1113e68f7 Mon Sep 17 00:00:00 2001 From: Ivan Ljubisavljevic Date: Mon, 21 Mar 2022 16:31:12 +0100 Subject: api za myprofilepage --- backend/api/api/Controllers/UserController.cs | 31 ++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'backend/api') diff --git a/backend/api/api/Controllers/UserController.cs b/backend/api/api/Controllers/UserController.cs index 58121656..dcab53cc 100644 --- a/backend/api/api/Controllers/UserController.cs +++ b/backend/api/api/Controllers/UserController.cs @@ -2,7 +2,9 @@ using api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Net.Http.Headers; using System.Diagnostics; +using System.Net.Http.Headers; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 //dovrsi kontroler @@ -13,10 +15,12 @@ namespace api.Controllers public class UserController : ControllerBase { private readonly IUserService userService; + private JwtToken jwtToken; - public UserController(IUserService userService) + public UserController(IUserService userService, IConfiguration configuration) { this.userService = userService; + jwtToken = new JwtToken(configuration); } // GET: api/ @@ -38,12 +42,29 @@ namespace api.Controllers return user; } - /* + + + + // GET api//5 //potrebno za profile page - [HttpGet("{id}")] - public ActionResult GetUserUsername(string username) + [HttpGet("myprofile")] + [Authorize(Roles = "User")] + public ActionResult MyProfilePage() { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + var user = userService.GetUserUsername(username); if (user == null) @@ -51,7 +72,7 @@ namespace api.Controllers return user; } - */ + // POST api/ [HttpPost] public ActionResult Post([FromBody] User user) -- cgit v1.2.3