Replace localStorage use by Cookie
parent
a59d493a7e
commit
f54a348e03
|
@ -32,6 +32,7 @@
|
||||||
"jquery-ui-bundle": "^1.11.4",
|
"jquery-ui-bundle": "^1.11.4",
|
||||||
"ngx-bootstrap": "^1.8.1",
|
"ngx-bootstrap": "^1.8.1",
|
||||||
"ngx-clipboard": "^8.0.2",
|
"ngx-clipboard": "^8.0.2",
|
||||||
|
"ngx-cookie-service": "^1.0.9",
|
||||||
"rxjs": "^5.2.0",
|
"rxjs": "^5.2.0",
|
||||||
"ts-helpers": "^1.1.1",
|
"ts-helpers": "^1.1.1",
|
||||||
"typescript": "^2.3.2",
|
"typescript": "^2.3.2",
|
||||||
|
|
|
@ -21,6 +21,7 @@ import {SharedModule} from "./shared.module";
|
||||||
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
|
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
|
||||||
import {UserService} from "./services/user-service/user.service";
|
import {UserService} from "./services/user-service/user.service";
|
||||||
import {UserStore} from "./services/stores/user.store";
|
import {UserStore} from "./services/stores/user.store";
|
||||||
|
import {CookieService} from "ngx-cookie-service";
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
imports: [SharedModule, BrowserModule, BrowserAnimationsModule, appRouting, HttpModule, ClipboardModule],
|
imports: [SharedModule, BrowserModule, BrowserAnimationsModule, appRouting, HttpModule, ClipboardModule],
|
||||||
|
@ -43,6 +44,7 @@ import {UserStore} from "./services/stores/user.store";
|
||||||
PromotionService,
|
PromotionService,
|
||||||
AppConfig,
|
AppConfig,
|
||||||
routingProviders,
|
routingProviders,
|
||||||
|
CookieService
|
||||||
],
|
],
|
||||||
declarations: [
|
declarations: [
|
||||||
AppComponent,
|
AppComponent,
|
||||||
|
|
|
@ -1,21 +1,19 @@
|
||||||
import {Injectable} from '@angular/core';
|
import {Injectable} from '@angular/core';
|
||||||
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
||||||
|
import {CookieService} from "ngx-cookie-service";
|
||||||
|
import {LoginService} from "../services/login-service/login-service";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LoginGuardSQL implements CanActivate {
|
export class LoginGuardSQL implements CanActivate {
|
||||||
|
|
||||||
constructor(private router: Router) {
|
constructor(private router: Router,
|
||||||
|
private loginService: LoginService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||||
if (localStorage.getItem('currentUser')) {
|
if(this.loginService.hasPermission(1)) {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
|
||||||
if (currentUser.permission === 1) {
|
|
||||||
// logged and correct permission so return true
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// not logged in so redirect to login page with the return url
|
// not logged in so redirect to login page with the return url
|
||||||
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
||||||
return false;
|
return false;
|
||||||
|
@ -25,18 +23,14 @@ export class LoginGuardSQL implements CanActivate {
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LoginGuardHL implements CanActivate {
|
export class LoginGuardHL implements CanActivate {
|
||||||
|
|
||||||
constructor(private router: Router) {
|
constructor(private router: Router,
|
||||||
|
private loginService: LoginService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||||
if (localStorage.getItem('currentUser')) {
|
if(this.loginService.hasPermission(2)) {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
return true;
|
||||||
if (currentUser.permission >= 2) {
|
|
||||||
// logged and correct permission so return true
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// not logged in so redirect to login page with the return url
|
// not logged in so redirect to login page with the return url
|
||||||
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
||||||
return false;
|
return false;
|
||||||
|
@ -46,18 +40,14 @@ export class LoginGuardHL implements CanActivate {
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LoginGuardMT implements CanActivate {
|
export class LoginGuardMT implements CanActivate {
|
||||||
|
|
||||||
constructor(private router: Router) {
|
constructor(private router: Router,
|
||||||
|
private loginService: LoginService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||||
if (localStorage.getItem('currentUser')) {
|
if(this.loginService.hasPermission(3)) {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
return true;
|
||||||
if (currentUser.permission >= 3) {
|
|
||||||
// logged and correct permission so return true
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// not logged in so redirect to login page with the return url
|
// not logged in so redirect to login page with the return url
|
||||||
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
||||||
return false;
|
return false;
|
||||||
|
@ -67,18 +57,14 @@ export class LoginGuardMT implements CanActivate {
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LoginGuardAdmin implements CanActivate {
|
export class LoginGuardAdmin implements CanActivate {
|
||||||
|
|
||||||
constructor(private router: Router) {
|
constructor(private router: Router,
|
||||||
|
private loginService: LoginService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||||
if (localStorage.getItem('currentUser')) {
|
if(this.loginService.hasPermission(4)) {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
return true;
|
||||||
if (currentUser.permission === 4) {
|
|
||||||
// logged and correct permission so return true
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// not logged in so redirect to login page with the return url
|
// not logged in so redirect to login page with the return url
|
||||||
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {NgForm} from "@angular/forms";
|
||||||
import {AwardingService} from "../../services/awarding-service/awarding.service";
|
import {AwardingService} from "../../services/awarding-service/awarding.service";
|
||||||
import {DecorationService} from "../../services/decoration-service/decoration.service";
|
import {DecorationService} from "../../services/decoration-service/decoration.service";
|
||||||
import {UserService} from "../../services/user-service/user.service";
|
import {UserService} from "../../services/user-service/user.service";
|
||||||
|
import {LoginService} from "../../services/login-service/login-service";
|
||||||
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
|
@ -37,11 +38,12 @@ export class RequestAwardComponent {
|
||||||
private route: ActivatedRoute,
|
private route: ActivatedRoute,
|
||||||
private userService: UserService,
|
private userService: UserService,
|
||||||
private awardingService: AwardingService,
|
private awardingService: AwardingService,
|
||||||
private decorationService: DecorationService) {
|
private decorationService: DecorationService,
|
||||||
|
private loginService: LoginService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
const currentUser = this.loginService.getCurrentUser();
|
||||||
this.userService.findUsers('', undefined, currentUser.squad._id).subscribe(users => {
|
this.userService.findUsers('', undefined, currentUser.squad._id).subscribe(users => {
|
||||||
this.users = users;
|
this.users = users;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import {Component} from "@angular/core";
|
import {Component} from "@angular/core";
|
||||||
import {Award} from "../../models/model-interfaces";
|
import {Award} from "../../models/model-interfaces";
|
||||||
import {AwardingService} from "../../services/awarding-service/awarding.service";
|
import {AwardingService} from "../../services/awarding-service/awarding.service";
|
||||||
|
import {LoginService} from "../../services/login-service/login-service";
|
||||||
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
|
@ -13,12 +14,12 @@ export class ConfirmAwardComponent {
|
||||||
|
|
||||||
showSuccessLabel = false;
|
showSuccessLabel = false;
|
||||||
|
|
||||||
constructor(private awardingService: AwardingService) {
|
constructor(private awardingService: AwardingService,
|
||||||
|
private loginService: LoginService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
let currentUser = this.loginService.getCurrentUser();
|
||||||
|
|
||||||
this.awardingService.getUnconfirmedAwards(currentUser.squad.fraction).subscribe(awards => {
|
this.awardingService.getUnconfirmedAwards(currentUser.squad.fraction).subscribe(awards => {
|
||||||
this.awards = awards;
|
this.awards = awards;
|
||||||
});
|
});
|
||||||
|
@ -31,7 +32,7 @@ export class ConfirmAwardComponent {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.awardingService.updateAward(updateObject).subscribe(res => {
|
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.awardingService.getUnconfirmedAwards(currentUser.squad.fraction).subscribe(awards => {
|
||||||
this.awards = awards;
|
this.awards = awards;
|
||||||
if (awards.length < 1) {
|
if (awards.length < 1) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {Component} from "@angular/core";
|
||||||
import {Promotion, Rank} from "../../models/model-interfaces";
|
import {Promotion, Rank} from "../../models/model-interfaces";
|
||||||
import {RankService} from "../../services/rank-service/rank.service";
|
import {RankService} from "../../services/rank-service/rank.service";
|
||||||
import {PromotionService} from "../../services/promotion-service/promotion.service";
|
import {PromotionService} from "../../services/promotion-service/promotion.service";
|
||||||
|
import {LoginService} from "../../services/login-service/login-service";
|
||||||
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
|
@ -17,11 +18,12 @@ export class ConfirmPromotionComponent {
|
||||||
promotions: Promotion[];
|
promotions: Promotion[];
|
||||||
|
|
||||||
constructor(private rankService: RankService,
|
constructor(private rankService: RankService,
|
||||||
private promotionService: PromotionService) {
|
private promotionService: PromotionService,
|
||||||
|
private loginService: LoginService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
let currentUser = this.loginService.getCurrentUser();
|
||||||
// show only current users fraction promotions
|
// show only current users fraction promotions
|
||||||
this.rankService.findRanks('', currentUser.squad.fraction).subscribe(ranks => {
|
this.rankService.findRanks('', currentUser.squad.fraction).subscribe(ranks => {
|
||||||
this.ranks = ranks;
|
this.ranks = ranks;
|
||||||
|
@ -39,7 +41,7 @@ export class ConfirmPromotionComponent {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.promotionService.updatePromotion(updateObject).subscribe(res => {
|
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.promotionService.getUnconfirmedPromotions(currentUser.squad.fraction).subscribe(promotions => {
|
||||||
this.promotions = promotions;
|
this.promotions = promotions;
|
||||||
if (promotions.length < 1) {
|
if (promotions.length < 1) {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {NgForm} from "@angular/forms";
|
||||||
import {UserService} from "../../services/user-service/user.service";
|
import {UserService} from "../../services/user-service/user.service";
|
||||||
import {RankService} from "../../services/rank-service/rank.service";
|
import {RankService} from "../../services/rank-service/rank.service";
|
||||||
import {PromotionService} from "../../services/promotion-service/promotion.service";
|
import {PromotionService} from "../../services/promotion-service/promotion.service";
|
||||||
|
import {LoginService} from "../../services/login-service/login-service";
|
||||||
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
|
@ -33,11 +34,12 @@ export class RequestPromotionComponent {
|
||||||
private route: ActivatedRoute,
|
private route: ActivatedRoute,
|
||||||
private userService: UserService,
|
private userService: UserService,
|
||||||
private rankService: RankService,
|
private rankService: RankService,
|
||||||
private promotionService: PromotionService) {
|
private promotionService: PromotionService,
|
||||||
|
private loginService: LoginService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
let currentUser = this.loginService.getCurrentUser()
|
||||||
// show only current users squad members
|
// show only current users squad members
|
||||||
this.userService.findUsers('', undefined, currentUser.squad._id).subscribe(users => {
|
this.userService.findUsers('', undefined, currentUser.squad._id).subscribe(users => {
|
||||||
this.users = users;
|
this.users = users;
|
||||||
|
@ -75,7 +77,7 @@ export class RequestPromotionComponent {
|
||||||
}, 2000);
|
}, 2000);
|
||||||
this.showForm = false;
|
this.showForm = false;
|
||||||
this.user = {};
|
this.user = {};
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
let currentUser = this.loginService.getCurrentUser();
|
||||||
this.promotionService.getSquadPromotions(currentUser.squad._id).subscribe(promotions => {
|
this.promotionService.getSquadPromotions(currentUser.squad._id).subscribe(promotions => {
|
||||||
this.uncheckedPromotions = promotions;
|
this.uncheckedPromotions = promotions;
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
import {Injectable} from "@angular/core";
|
import {Injectable} from "@angular/core";
|
||||||
import {Headers, Http, RequestMethod} from "@angular/http";
|
import {Headers, Http, RequestMethod} from "@angular/http";
|
||||||
import {Router} from "@angular/router";
|
import {Router} from "@angular/router";
|
||||||
|
import {LoginService} from "./login-service/login-service";
|
||||||
|
import {CookieService} from "ngx-cookie-service";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HttpClient {
|
export class HttpClient {
|
||||||
|
|
||||||
constructor(private router: Router,
|
constructor(private router: Router,
|
||||||
private http: Http) {
|
private http: Http,
|
||||||
|
private cookieService: CookieService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
createAuthorizationHeader() {
|
createAuthorizationHeader() {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
let currentUser = JSON.parse(this.cookieService.get('currentUser'));
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
if (new Date().getTime() <= Date.parse(currentUser.tokenExpireDate)) {
|
if (new Date().getTime() <= Date.parse(currentUser.tokenExpireDate)) {
|
||||||
let headers = new Headers();
|
let headers = new Headers();
|
||||||
|
|
|
@ -3,14 +3,15 @@ import {Http, Response} from "@angular/http";
|
||||||
import "rxjs/add/operator/map";
|
import "rxjs/add/operator/map";
|
||||||
|
|
||||||
import {AppConfig} from "../../app.config";
|
import {AppConfig} from "../../app.config";
|
||||||
import {AppUser} from "../../models/model-interfaces";
|
|
||||||
import {AwardingService} from "../awarding-service/awarding.service";
|
import {AwardingService} from "../awarding-service/awarding.service";
|
||||||
import {PromotionService} from "../promotion-service/promotion.service";
|
import {PromotionService} from "../promotion-service/promotion.service";
|
||||||
|
import {CookieService} from "ngx-cookie-service";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LoginService {
|
export class LoginService {
|
||||||
constructor(private http: Http,
|
constructor(private http: Http,
|
||||||
private config: AppConfig,
|
private config: AppConfig,
|
||||||
|
private cookieService: CookieService,
|
||||||
private awardingService: AwardingService,
|
private awardingService: AwardingService,
|
||||||
private promotionService: PromotionService) {
|
private promotionService: PromotionService) {
|
||||||
}
|
}
|
||||||
|
@ -20,9 +21,10 @@ export class LoginService {
|
||||||
.map((response: Response) => {
|
.map((response: Response) => {
|
||||||
// login successful if there's a jwt token in the response
|
// login successful if there's a jwt token in the response
|
||||||
let user = response.json();
|
let user = response.json();
|
||||||
|
console.log(user);
|
||||||
if (user && user.token) {
|
if (user && user.token) {
|
||||||
// store user details and jwt token in local storage to keep user logged in between page refreshes
|
// store user details and jwt token in cookie
|
||||||
localStorage.setItem('currentUser', JSON.stringify(user));
|
this.cookieService.set('currentUser', JSON.stringify(user));
|
||||||
if (user.permission >= 2) {
|
if (user.permission >= 2) {
|
||||||
const fraction = user.squad.fraction;
|
const fraction = user.squad.fraction;
|
||||||
this.awardingService.checkUnprocessedAwards(fraction);
|
this.awardingService.checkUnprocessedAwards(fraction);
|
||||||
|
@ -39,26 +41,23 @@ export class LoginService {
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
// remove user from local storage
|
this.cookieService.delete('currentUser');
|
||||||
localStorage.removeItem('currentUser');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isLoggedIn() {
|
isLoggedIn() {
|
||||||
return !!localStorage.getItem('currentUser');
|
return !!this.cookieService.get('currentUser');
|
||||||
}
|
}
|
||||||
|
|
||||||
hasPermission(level: number) {
|
hasPermission(level: number) {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
return this.isLoggedIn() && this.getCurrentUser().permission >= level;
|
||||||
return this.isLoggedIn() && currentUser.permission >= level;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentUser(): AppUser {
|
getCurrentUser() {
|
||||||
return JSON.parse(localStorage.getItem('currentUser'));
|
return JSON.parse(this.cookieService.get('currentUser'));
|
||||||
}
|
}
|
||||||
|
|
||||||
hasSquad() {
|
hasSquad() {
|
||||||
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
return this.getCurrentUser().squad != null;
|
||||||
return currentUser.squad != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue