From 208394ff08cba0880746d9c7841be08e127f66d6 Mon Sep 17 00:00:00 2001 From: Ivan Ljubisavljevic Date: Mon, 18 Apr 2022 01:14:31 +0200 Subject: Izmena na frontu i ml-u(username -> userId) #71 --- frontend/src/app/_services/datasets.service.ts | 4 ++-- frontend/src/app/_services/predictors.service.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'frontend/src/app/_services') diff --git a/frontend/src/app/_services/datasets.service.ts b/frontend/src/app/_services/datasets.service.ts index c3281be6..b2272f0a 100644 --- a/frontend/src/app/_services/datasets.service.ts +++ b/frontend/src/app/_services/datasets.service.ts @@ -29,10 +29,10 @@ export class DatasetsService { } editDataset(dataset: Dataset): Observable { - return this.http.put(`${Configuration.settings.apiURL}/dataset/`, dataset, { headers: this.authService.authHeader() }); + return this.http.put(`${Configuration.settings.apiURL}/dataset/` + dataset._id, dataset, { headers: this.authService.authHeader() }); } deleteDataset(dataset: Dataset) { - return this.http.delete(`${Configuration.settings.apiURL}/dataset/` + dataset.name, { headers: this.authService.authHeader(), responseType: "text" }); + return this.http.delete(`${Configuration.settings.apiURL}/dataset/` + dataset._id, { headers: this.authService.authHeader(), responseType: "text" }); } } diff --git a/frontend/src/app/_services/predictors.service.ts b/frontend/src/app/_services/predictors.service.ts index 9e8383aa..a24ee708 100644 --- a/frontend/src/app/_services/predictors.service.ts +++ b/frontend/src/app/_services/predictors.service.ts @@ -24,7 +24,7 @@ export class PredictorsService { } deletePredictor(predictor: Predictor) { - return this.http.delete(`${Configuration.settings.apiURL}/predictor/` + predictor.name, { headers: this.authService.authHeader(), responseType: "text" }); + return this.http.delete(`${Configuration.settings.apiURL}/predictor/` + predictor._id, { headers: this.authService.authHeader(), responseType: "text" }); } getMyPredictors(): Observable { -- cgit v1.2.3 From 03177959b3e50b6f4e80045a7e545938f56e84fa Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sat, 30 Apr 2022 18:56:46 +0200 Subject: Omoguceno da Guest ne gubi token na refreshu. --- frontend/src/app/_services/auth.service.ts | 11 +++++++++++ frontend/src/app/app.component.ts | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'frontend/src/app/_services') diff --git a/frontend/src/app/_services/auth.service.ts b/frontend/src/app/_services/auth.service.ts index dd46615d..845cb3f5 100644 --- a/frontend/src/app/_services/auth.service.ts +++ b/frontend/src/app/_services/auth.service.ts @@ -106,4 +106,15 @@ export class AuthService { authHeader() { return new HttpHeaders().set("Authorization", "Bearer " + this.cookie.get('token')); } + alreadyGuest(){ + if(this.cookie.check('token')){ + const token = this.cookie.get('token'); + const decodedToken = jwtHelper.decodeToken(token); + if(decodedToken.role=="Guest") + return true; + } + return false; + } + + } diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index f9bc2726..e301b46f 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -40,7 +40,8 @@ export class AppComponent implements OnInit, AfterViewInit { } }); if (!this.authService.isAuthenticated()) { - this.authService.addGuestToken(); + if(!this.authService.alreadyGuest()) + this.authService.addGuestToken(); } this.signalRService.startConnection(); //this.startHttpRequest(); -- cgit v1.2.3 From 0e5aaf6d3976b9a46b48f554d004f3b90ca8b813 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sat, 30 Apr 2022 19:45:20 +0200 Subject: Omoguceno na frontu da se guestu refresha token. --- backend/api/api/Services/TempRemovalService.cs | 2 +- frontend/src/app/_services/auth.service.ts | 10 +--------- frontend/src/app/app.component.ts | 2 +- 3 files changed, 3 insertions(+), 11 deletions(-) (limited to 'frontend/src/app/_services') diff --git a/backend/api/api/Services/TempRemovalService.cs b/backend/api/api/Services/TempRemovalService.cs index 525ff386..9e6b7f96 100644 --- a/backend/api/api/Services/TempRemovalService.cs +++ b/backend/api/api/Services/TempRemovalService.cs @@ -29,7 +29,7 @@ namespace api.Services List tempUsers=_user.Find(u=>u.isPermament==false).ToList(); foreach (User user in tempUsers) { - if ((DateTime.Now.ToUniversalTime() - user.dateCreated).TotalMinutes < 1) + if ((DateTime.Now.ToUniversalTime() - user.dateCreated).TotalDays < 1) continue; List tempPredictors=_predictor.Find(p=>p.uploaderId==user._id).ToList(); List tempModels=_model.Find(m=>m.uploaderId==user._id).ToList(); diff --git a/frontend/src/app/_services/auth.service.ts b/frontend/src/app/_services/auth.service.ts index 845cb3f5..92920b24 100644 --- a/frontend/src/app/_services/auth.service.ts +++ b/frontend/src/app/_services/auth.service.ts @@ -52,21 +52,13 @@ export class AuthService { } var property = jwtHelper.decodeToken(this.cookie.get('token')); var username = property['name']; - if (username != "") { this.refresher = setTimeout(() => { this.http.post(`${Configuration.settings.apiURL}/auth/renewJwt`, {}, { headers: this.authHeader(), responseType: 'text' }).subscribe((response) => { this.authenticate(response); }); }, exp.getTime() - new Date().getTime() - 60000); - } - else { - this.refresher = setTimeout(() => { - this.getGuestToken().subscribe((response) => { - this.authenticate(response); - }); - }, exp.getTime() - new Date().getTime() - 60000); - } + } addGuestToken() { diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index e301b46f..dfe4febc 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -43,7 +43,7 @@ export class AppComponent implements OnInit, AfterViewInit { if(!this.authService.alreadyGuest()) this.authService.addGuestToken(); } - this.signalRService.startConnection(); + //this.signalRService.startConnection(); //this.startHttpRequest(); } } -- cgit v1.2.3 From 45c519d53fee1124c2882c7b353cd930fd311f9e Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sat, 30 Apr 2022 20:04:42 +0200 Subject: Stranica se refreshuje po dodeli tokena Guesta ili Usera da bi se refreshovala komunikacija preko webSocketa. Potrebno izmeniti u buducnosti. --- frontend/src/app/_modals/login-modal/login-modal.component.ts | 2 ++ frontend/src/app/_services/auth.service.ts | 1 + frontend/src/app/app.component.ts | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) (limited to 'frontend/src/app/_services') diff --git a/frontend/src/app/_modals/login-modal/login-modal.component.ts b/frontend/src/app/_modals/login-modal/login-modal.component.ts index 062a0550..ccd78509 100644 --- a/frontend/src/app/_modals/login-modal/login-modal.component.ts +++ b/frontend/src/app/_modals/login-modal/login-modal.component.ts @@ -50,7 +50,9 @@ export class LoginModalComponent implements OnInit { this.userInfoService.getUserInfo().subscribe((response) => { shared.photoId = response.photoId; }); + location.reload(); } + }); } else { diff --git a/frontend/src/app/_services/auth.service.ts b/frontend/src/app/_services/auth.service.ts index 92920b24..68c29182 100644 --- a/frontend/src/app/_services/auth.service.ts +++ b/frontend/src/app/_services/auth.service.ts @@ -64,6 +64,7 @@ export class AuthService { addGuestToken() { this.getGuestToken().subscribe((token) => { this.authenticate(token); + location.reload(); }); } diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index dfe4febc..e301b46f 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -43,7 +43,7 @@ export class AppComponent implements OnInit, AfterViewInit { if(!this.authService.alreadyGuest()) this.authService.addGuestToken(); } - //this.signalRService.startConnection(); + this.signalRService.startConnection(); //this.startHttpRequest(); } } -- cgit v1.2.3 From 87aa75b968b2cf7cc322c50e83661b3bf3e463ca Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Wed, 4 May 2022 21:27:16 +0200 Subject: Omoguceno da se rad gostu sacuva, ako se on registruje pre nego sto izgubi sesiju. --- backend/api/api/Controllers/AuthController.cs | 30 ++++++++++++++++++++++++--- backend/api/api/Interfaces/IAuthService.cs | 2 +- backend/api/api/Services/AuthService.cs | 5 +++-- frontend/src/app/_services/auth.service.ts | 2 +- 4 files changed, 32 insertions(+), 7 deletions(-) (limited to 'frontend/src/app/_services') 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> 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() { -- cgit v1.2.3