Replace localStorage use by Cookie

pull/10/head
Florian Hartwich 2017-09-23 11:53:10 +02:00
parent a59d493a7e
commit f54a348e03
9 changed files with 55 additions and 57 deletions

View File

@ -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",

View File

@ -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,

View File

@ -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;

View File

@ -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;
});

View File

@ -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) {

View File

@ -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) {

View File

@ -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;
})

View File

@ -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();

View File

@ -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;
}
}