From f54a348e034b3f1fed66f7a1d00dcf81075aa293 Mon Sep 17 00:00:00 2001 From: Florian Hartwich Date: Sat, 23 Sep 2017 11:53:10 +0200 Subject: [PATCH] Replace localStorage use by Cookie --- static/package.json | 1 + static/src/app/app.module.ts | 2 + static/src/app/login/login.guard.ts | 48 +++++++------------ .../app/request/award/req-award.component.ts | 6 ++- .../confirm-award/confirm-award.component.ts | 9 ++-- .../confirm-promotion.component.ts | 8 ++-- .../promotion/req-promotion.component.ts | 8 ++-- static/src/app/services/http-client.ts | 7 ++- .../services/login-service/login-service.ts | 23 +++++---- 9 files changed, 55 insertions(+), 57 deletions(-) diff --git a/static/package.json b/static/package.json index 7d7b6cc..bdbb911 100644 --- a/static/package.json +++ b/static/package.json @@ -32,6 +32,7 @@ "jquery-ui-bundle": "^1.11.4", "ngx-bootstrap": "^1.8.1", "ngx-clipboard": "^8.0.2", + "ngx-cookie-service": "^1.0.9", "rxjs": "^5.2.0", "ts-helpers": "^1.1.1", "typescript": "^2.3.2", diff --git a/static/src/app/app.module.ts b/static/src/app/app.module.ts index f8f1d68..bd133ba 100644 --- a/static/src/app/app.module.ts +++ b/static/src/app/app.module.ts @@ -21,6 +21,7 @@ import {SharedModule} from "./shared.module"; import {BrowserAnimationsModule} from "@angular/platform-browser/animations"; import {UserService} from "./services/user-service/user.service"; import {UserStore} from "./services/stores/user.store"; +import {CookieService} from "ngx-cookie-service"; @NgModule({ imports: [SharedModule, BrowserModule, BrowserAnimationsModule, appRouting, HttpModule, ClipboardModule], @@ -43,6 +44,7 @@ import {UserStore} from "./services/stores/user.store"; PromotionService, AppConfig, routingProviders, + CookieService ], declarations: [ AppComponent, diff --git a/static/src/app/login/login.guard.ts b/static/src/app/login/login.guard.ts index 9311fb7..5614fb0 100644 --- a/static/src/app/login/login.guard.ts +++ b/static/src/app/login/login.guard.ts @@ -1,21 +1,19 @@ import {Injectable} from '@angular/core'; import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router'; +import {CookieService} from "ngx-cookie-service"; +import {LoginService} from "../services/login-service/login-service"; @Injectable() export class LoginGuardSQL implements CanActivate { - constructor(private router: Router) { + constructor(private router: Router, + private loginService: LoginService) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - if (localStorage.getItem('currentUser')) { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); - if (currentUser.permission === 1) { - // logged and correct permission so return true + if(this.loginService.hasPermission(1)) { return true; - } } - // not logged in so redirect to login page with the return url this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}}); return false; @@ -25,18 +23,14 @@ export class LoginGuardSQL implements CanActivate { @Injectable() export class LoginGuardHL implements CanActivate { - constructor(private router: Router) { + constructor(private router: Router, + private loginService: LoginService) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - if (localStorage.getItem('currentUser')) { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); - if (currentUser.permission >= 2) { - // logged and correct permission so return true - return true; - } + if(this.loginService.hasPermission(2)) { + return true; } - // not logged in so redirect to login page with the return url this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}}); return false; @@ -46,18 +40,14 @@ export class LoginGuardHL implements CanActivate { @Injectable() export class LoginGuardMT implements CanActivate { - constructor(private router: Router) { + constructor(private router: Router, + private loginService: LoginService) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - if (localStorage.getItem('currentUser')) { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); - if (currentUser.permission >= 3) { - // logged and correct permission so return true - return true; - } + if(this.loginService.hasPermission(3)) { + return true; } - // not logged in so redirect to login page with the return url this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}}); return false; @@ -67,18 +57,14 @@ export class LoginGuardMT implements CanActivate { @Injectable() export class LoginGuardAdmin implements CanActivate { - constructor(private router: Router) { + constructor(private router: Router, + private loginService: LoginService) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - if (localStorage.getItem('currentUser')) { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); - if (currentUser.permission === 4) { - // logged and correct permission so return true - return true; - } + if(this.loginService.hasPermission(4)) { + return true; } - // not logged in so redirect to login page with the return url this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}}); return false; diff --git a/static/src/app/request/award/req-award.component.ts b/static/src/app/request/award/req-award.component.ts index f61d691..9efc89a 100644 --- a/static/src/app/request/award/req-award.component.ts +++ b/static/src/app/request/award/req-award.component.ts @@ -5,6 +5,7 @@ import {NgForm} from "@angular/forms"; import {AwardingService} from "../../services/awarding-service/awarding.service"; import {DecorationService} from "../../services/decoration-service/decoration.service"; import {UserService} from "../../services/user-service/user.service"; +import {LoginService} from "../../services/login-service/login-service"; @Component({ @@ -37,11 +38,12 @@ export class RequestAwardComponent { private route: ActivatedRoute, private userService: UserService, private awardingService: AwardingService, - private decorationService: DecorationService) { + private decorationService: DecorationService, + private loginService: LoginService) { } ngOnInit() { - const currentUser = JSON.parse(localStorage.getItem('currentUser')); + const currentUser = this.loginService.getCurrentUser(); this.userService.findUsers('', undefined, currentUser.squad._id).subscribe(users => { this.users = users; }); diff --git a/static/src/app/request/confirm-award/confirm-award.component.ts b/static/src/app/request/confirm-award/confirm-award.component.ts index f213abe..a6ee3e6 100644 --- a/static/src/app/request/confirm-award/confirm-award.component.ts +++ b/static/src/app/request/confirm-award/confirm-award.component.ts @@ -1,6 +1,7 @@ import {Component} from "@angular/core"; import {Award} from "../../models/model-interfaces"; import {AwardingService} from "../../services/awarding-service/awarding.service"; +import {LoginService} from "../../services/login-service/login-service"; @Component({ @@ -13,12 +14,12 @@ export class ConfirmAwardComponent { showSuccessLabel = false; - constructor(private awardingService: AwardingService) { + constructor(private awardingService: AwardingService, + private loginService: LoginService) { } ngOnInit() { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); - + let currentUser = this.loginService.getCurrentUser(); this.awardingService.getUnconfirmedAwards(currentUser.squad.fraction).subscribe(awards => { this.awards = awards; }); @@ -31,7 +32,7 @@ export class ConfirmAwardComponent { }; this.awardingService.updateAward(updateObject).subscribe(res => { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); + let currentUser = this.loginService.getCurrentUser(); this.awardingService.getUnconfirmedAwards(currentUser.squad.fraction).subscribe(awards => { this.awards = awards; if (awards.length < 1) { diff --git a/static/src/app/request/confirm-promotion/confirm-promotion.component.ts b/static/src/app/request/confirm-promotion/confirm-promotion.component.ts index 91d5d87..05f9c15 100644 --- a/static/src/app/request/confirm-promotion/confirm-promotion.component.ts +++ b/static/src/app/request/confirm-promotion/confirm-promotion.component.ts @@ -2,6 +2,7 @@ import {Component} from "@angular/core"; import {Promotion, Rank} from "../../models/model-interfaces"; import {RankService} from "../../services/rank-service/rank.service"; import {PromotionService} from "../../services/promotion-service/promotion.service"; +import {LoginService} from "../../services/login-service/login-service"; @Component({ @@ -17,11 +18,12 @@ export class ConfirmPromotionComponent { promotions: Promotion[]; constructor(private rankService: RankService, - private promotionService: PromotionService) { + private promotionService: PromotionService, + private loginService: LoginService) { } ngOnInit() { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); + let currentUser = this.loginService.getCurrentUser(); // show only current users fraction promotions this.rankService.findRanks('', currentUser.squad.fraction).subscribe(ranks => { this.ranks = ranks; @@ -39,7 +41,7 @@ export class ConfirmPromotionComponent { }; this.promotionService.updatePromotion(updateObject).subscribe(res => { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); + let currentUser = this.loginService.getCurrentUser(); this.promotionService.getUnconfirmedPromotions(currentUser.squad.fraction).subscribe(promotions => { this.promotions = promotions; if (promotions.length < 1) { diff --git a/static/src/app/request/promotion/req-promotion.component.ts b/static/src/app/request/promotion/req-promotion.component.ts index 7be13fc..cf29d11 100644 --- a/static/src/app/request/promotion/req-promotion.component.ts +++ b/static/src/app/request/promotion/req-promotion.component.ts @@ -5,6 +5,7 @@ import {NgForm} from "@angular/forms"; import {UserService} from "../../services/user-service/user.service"; import {RankService} from "../../services/rank-service/rank.service"; import {PromotionService} from "../../services/promotion-service/promotion.service"; +import {LoginService} from "../../services/login-service/login-service"; @Component({ @@ -33,11 +34,12 @@ export class RequestPromotionComponent { private route: ActivatedRoute, private userService: UserService, private rankService: RankService, - private promotionService: PromotionService) { + private promotionService: PromotionService, + private loginService: LoginService) { } ngOnInit() { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); + let currentUser = this.loginService.getCurrentUser() // show only current users squad members this.userService.findUsers('', undefined, currentUser.squad._id).subscribe(users => { this.users = users; @@ -75,7 +77,7 @@ export class RequestPromotionComponent { }, 2000); this.showForm = false; this.user = {}; - let currentUser = JSON.parse(localStorage.getItem('currentUser')); + let currentUser = this.loginService.getCurrentUser(); this.promotionService.getSquadPromotions(currentUser.squad._id).subscribe(promotions => { this.uncheckedPromotions = promotions; }) diff --git a/static/src/app/services/http-client.ts b/static/src/app/services/http-client.ts index 87c28a7..12010ad 100644 --- a/static/src/app/services/http-client.ts +++ b/static/src/app/services/http-client.ts @@ -1,16 +1,19 @@ import {Injectable} from "@angular/core"; import {Headers, Http, RequestMethod} from "@angular/http"; import {Router} from "@angular/router"; +import {LoginService} from "./login-service/login-service"; +import {CookieService} from "ngx-cookie-service"; @Injectable() export class HttpClient { constructor(private router: Router, - private http: Http) { + private http: Http, + private cookieService: CookieService) { } createAuthorizationHeader() { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); + let currentUser = JSON.parse(this.cookieService.get('currentUser')); if (currentUser) { if (new Date().getTime() <= Date.parse(currentUser.tokenExpireDate)) { let headers = new Headers(); diff --git a/static/src/app/services/login-service/login-service.ts b/static/src/app/services/login-service/login-service.ts index f04f1f9..2dea9d9 100644 --- a/static/src/app/services/login-service/login-service.ts +++ b/static/src/app/services/login-service/login-service.ts @@ -3,14 +3,15 @@ import {Http, Response} from "@angular/http"; import "rxjs/add/operator/map"; import {AppConfig} from "../../app.config"; -import {AppUser} from "../../models/model-interfaces"; import {AwardingService} from "../awarding-service/awarding.service"; import {PromotionService} from "../promotion-service/promotion.service"; +import {CookieService} from "ngx-cookie-service"; @Injectable() export class LoginService { constructor(private http: Http, private config: AppConfig, + private cookieService: CookieService, private awardingService: AwardingService, private promotionService: PromotionService) { } @@ -20,9 +21,10 @@ export class LoginService { .map((response: Response) => { // login successful if there's a jwt token in the response let user = response.json(); + console.log(user); if (user && user.token) { - // store user details and jwt token in local storage to keep user logged in between page refreshes - localStorage.setItem('currentUser', JSON.stringify(user)); + // store user details and jwt token in cookie + this.cookieService.set('currentUser', JSON.stringify(user)); if (user.permission >= 2) { const fraction = user.squad.fraction; this.awardingService.checkUnprocessedAwards(fraction); @@ -39,26 +41,23 @@ export class LoginService { } logout() { - // remove user from local storage - localStorage.removeItem('currentUser'); + this.cookieService.delete('currentUser'); } isLoggedIn() { - return !!localStorage.getItem('currentUser'); + return !!this.cookieService.get('currentUser'); } hasPermission(level: number) { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); - return this.isLoggedIn() && currentUser.permission >= level; + return this.isLoggedIn() && this.getCurrentUser().permission >= level; } - getCurrentUser(): AppUser { - return JSON.parse(localStorage.getItem('currentUser')); + getCurrentUser() { + return JSON.parse(this.cookieService.get('currentUser')); } hasSquad() { - let currentUser = JSON.parse(localStorage.getItem('currentUser')); - return currentUser.squad != null; + return this.getCurrentUser().squad != null; } }