From 054d567cef92cae42cd75f56c55e16febdac658c Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Tue, 19 Apr 2022 16:52:03 +0200 Subject: Omoguceno pravljenje privremenog korisnika. --- backend/api/api/Controllers/ModelController.cs | 2 +- backend/api/api/Models/IJwtToken.cs | 2 +- backend/api/api/Models/JwtToken.cs | 5 +++-- backend/api/api/Models/User.cs | 1 + backend/api/api/Services/AuthService.cs | 14 +++++++++++++- backend/api/api/Services/IAuthService.cs | 1 + 6 files changed, 20 insertions(+), 5 deletions(-) (limited to 'backend') diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs index ce1759ca..d60e3236 100644 --- a/backend/api/api/Controllers/ModelController.cs +++ b/backend/api/api/Controllers/ModelController.cs @@ -98,7 +98,7 @@ namespace api.Controllers // GET: api//mymodels [HttpGet("mymodels")] - [Authorize(Roles = "User")] + [Authorize(Roles = "User,Guest")] public ActionResult> Get() { string uploaderId = getUserId(); diff --git a/backend/api/api/Models/IJwtToken.cs b/backend/api/api/Models/IJwtToken.cs index 2afb6683..96b96997 100644 --- a/backend/api/api/Models/IJwtToken.cs +++ b/backend/api/api/Models/IJwtToken.cs @@ -4,7 +4,7 @@ namespace api.Models { public interface IJwtToken { - string GenGuestToken(); + string GenGuestToken(string id); string GenToken(AuthRequest user); string RenewToken(string existingToken); string TokenToUsername(string token); diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs index 06b3a666..3ec75468 100644 --- a/backend/api/api/Models/JwtToken.cs +++ b/backend/api/api/Models/JwtToken.cs @@ -100,15 +100,16 @@ namespace api.Models } - public string GenGuestToken() + public string GenGuestToken(string id) { + var user=_userService.GetUserById(id); var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim("name",""), new Claim("role", "Guest"), - new Claim("id","")}), + new Claim("id",user._id)}), Expires = DateTime.UtcNow.AddMinutes(20), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; diff --git a/backend/api/api/Models/User.cs b/backend/api/api/Models/User.cs index 1ae8e437..ce289af1 100644 --- a/backend/api/api/Models/User.cs +++ b/backend/api/api/Models/User.cs @@ -25,6 +25,7 @@ namespace api.Models public string LastName { get; set; } public string photoId { get; set; } + public bool isPermament { get; set; } } } diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs index c7161dee..b734fa7a 100644 --- a/backend/api/api/Services/AuthService.cs +++ b/backend/api/api/Services/AuthService.cs @@ -37,6 +37,7 @@ namespace api.Services u.FirstName = user.firstName; u.LastName = user.lastName; u.photoId = "1"; + u.isPermament = true; if (_users.Find(user => user.Username == u.Username).FirstOrDefault() != null) return "Username Already Exists"; if (_users.Find(user => user.Email == u.Email).FirstOrDefault() != null) @@ -45,6 +46,13 @@ namespace api.Services _users.InsertOne(u); return "User added"; } + public void RegisterGuest() + { + User u=new User(); + u._id = ""; + _users.InsertOne(u); + _jwt.GenGuestToken(u._id); + } public string RenewToken(string header) { @@ -60,7 +68,11 @@ namespace api.Services public string GuestToken() { - return _jwt.GenGuestToken(); + User u = new User(); + u._id = ""; + _users.InsertOne(u); + return _jwt.GenGuestToken(u._id); + } diff --git a/backend/api/api/Services/IAuthService.cs b/backend/api/api/Services/IAuthService.cs index 9a109208..4ed9a761 100644 --- a/backend/api/api/Services/IAuthService.cs +++ b/backend/api/api/Services/IAuthService.cs @@ -8,5 +8,6 @@ namespace api.Services string Register(RegisterRequest user); string RenewToken(string token); public string GuestToken(); + public void RegisterGuest(); } } \ No newline at end of file -- cgit v1.2.3