diff options
author | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-05-04 21:27:16 +0200 |
---|---|---|
committer | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-05-04 21:27:16 +0200 |
commit | 87aa75b968b2cf7cc322c50e83661b3bf3e463ca (patch) | |
tree | 036f6f9a1e23cc1366153eb5539d95b5c439d7a7 | |
parent | a0679ab22577f37e729b84610918b0fd757b6b17 (diff) |
Omoguceno da se rad gostu sacuva, ako se on registruje pre nego sto izgubi sesiju.
-rw-r--r-- | backend/api/api/Controllers/AuthController.cs | 30 | ||||
-rw-r--r-- | backend/api/api/Interfaces/IAuthService.cs | 2 | ||||
-rw-r--r-- | backend/api/api/Services/AuthService.cs | 5 | ||||
-rw-r--r-- | frontend/src/app/_services/auth.service.ts | 2 |
4 files changed, 32 insertions, 7 deletions
diff --git a/backend/api/api/Controllers/AuthController.cs b/backend/api/api/Controllers/AuthController.cs index df8a514c..f70146ed 100644 --- a/backend/api/api/Controllers/AuthController.cs +++ b/backend/api/api/Controllers/AuthController.cs @@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Mvc; using api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.Net.Http.Headers; +using System.Net.Http.Headers; +using api.Models; namespace api.Controllers { @@ -12,16 +14,20 @@ namespace api.Controllers public class AuthController : ControllerBase { private IAuthService _auth; - public AuthController(IAuthService auth) + private IJwtToken _jwtToken; + public AuthController(IAuthService auth, IJwtToken Token) { _auth = auth; + _jwtToken = Token; } [HttpPost("register")] public async Task<ActionResult<string>> Register(RegisterRequest user) { - - return Ok(_auth.Register(user)); + string id=getUserId(); + if (id == null) + return BadRequest(); + return Ok(_auth.Register(user,id)); } [HttpPost("login")] @@ -57,6 +63,24 @@ namespace api.Controllers } + public string getUserId() + { + string uploaderId; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + uploaderId = _jwtToken.TokenToId(parameter); + if (uploaderId == null) + return null; + } + else + return null; + + return uploaderId; + } + } diff --git a/backend/api/api/Interfaces/IAuthService.cs b/backend/api/api/Interfaces/IAuthService.cs index 9a109208..570ce0a4 100644 --- a/backend/api/api/Interfaces/IAuthService.cs +++ b/backend/api/api/Interfaces/IAuthService.cs @@ -5,7 +5,7 @@ namespace api.Services public interface IAuthService { string Login(AuthRequest user); - string Register(RegisterRequest user); + string Register(RegisterRequest user, string id); string RenewToken(string token); public string GuestToken(); } diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs index 2d7d753d..672511b3 100644 --- a/backend/api/api/Services/AuthService.cs +++ b/backend/api/api/Services/AuthService.cs @@ -28,7 +28,7 @@ namespace api.Services return _jwt.GenToken(u); } - public string Register(RegisterRequest user) + public string Register(RegisterRequest user,string id) { User u = new User(); u.Username = user.username; @@ -38,13 +38,14 @@ namespace api.Services u.LastName = user.lastName; u.photoId = "1"; u.isPermament = true; + u._id = id; u.dateCreated= DateTime.Now.ToUniversalTime(); if (_users.Find(user => user.Username == u.Username).FirstOrDefault() != null) return "Username Already Exists"; if (_users.Find(user => user.Email == u.Email).FirstOrDefault() != null) return "Email Already Exists"; - _users.InsertOne(u); + _users.ReplaceOne(x=>x._id==u._id,u); return "User added"; } diff --git a/frontend/src/app/_services/auth.service.ts b/frontend/src/app/_services/auth.service.ts index 68c29182..cc5ad688 100644 --- a/frontend/src/app/_services/auth.service.ts +++ b/frontend/src/app/_services/auth.service.ts @@ -22,7 +22,7 @@ export class AuthService { } register(user: any) { - return this.http.post(`${Configuration.settings.apiURL}/auth/register`, { ...user }, { responseType: 'text' }); + return this.http.post(`${Configuration.settings.apiURL}/auth/register`, { ...user },{ headers: this.authHeader() , responseType: 'text' }); } getGuestToken() { |