mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 21:06:06 +00:00
Improve local storage
This commit is contained in:
@@ -1,20 +1,22 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import { LocalstoreService } from '../store/localstore.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private router:Router
|
||||
private router:Router,
|
||||
private localstoreService: LocalstoreService
|
||||
){}
|
||||
|
||||
canActivate(
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||||
|
||||
if(window.location.pathname != '' && !localStorage.getItem('UserData')) {
|
||||
if(window.location.pathname != '' && !this.localstoreService.get('UserData', false)) {
|
||||
this.router.navigate(['/']);
|
||||
return false
|
||||
} else {
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import { LocalstoreService } from '../store/localstore.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class LoginGuard implements CanActivate {
|
||||
constructor( private router:Router) {
|
||||
constructor( private router:Router,
|
||||
private localstoreService: LocalstoreService) {
|
||||
|
||||
}
|
||||
canActivate(
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||||
|
||||
if(window.location.pathname == '/' && localStorage.getItem('UserData') != null ) {
|
||||
if(window.location.pathname == '/' && this.localstoreService.get('UserData', null) != null ) {
|
||||
this.router.navigate(['/home/events']);
|
||||
return false
|
||||
} else {
|
||||
|
||||
@@ -7,6 +7,7 @@ import { environment } from 'src/environments/environment';
|
||||
import { AlertController } from '@ionic/angular';
|
||||
import { NotificationsService } from 'src/app/services/notifications.service';
|
||||
import crypto from 'crypto-js'
|
||||
import { LocalstoreService } from 'src/app/store/localstore.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
@@ -31,22 +32,23 @@ export class LoginPage implements OnInit {
|
||||
private router: Router,
|
||||
private authService: AuthService,
|
||||
private toastService: ToastService,
|
||||
public alertController: AlertController
|
||||
public alertController: AlertController,
|
||||
private localstoreService: LocalstoreService
|
||||
) {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
|
||||
// App has session
|
||||
if(!localStorage.getItem('UserData')) {
|
||||
if(!this.localstoreService.get('UserData', false)) {
|
||||
this.hasSession = false
|
||||
} else {
|
||||
this.hasSession = true
|
||||
// this.router.navigate(['/home/events']);
|
||||
}
|
||||
|
||||
let userData = JSON.parse(localStorage.getItem('UserData')) || {}
|
||||
|
||||
let userData = this.localstoreService.get('UserData', {})
|
||||
|
||||
const loginPreference = userData?.loginPreference
|
||||
const pin = userData?.PIN
|
||||
|
||||
@@ -176,13 +178,13 @@ export class LoginPage implements OnInit {
|
||||
|
||||
const code = this.code.join('')
|
||||
const encrypted = crypto.SHA1(code)
|
||||
|
||||
let userData = JSON.parse(localStorage.getItem('UserData')) || {}
|
||||
|
||||
let userData = this.localstoreService.get('UserData', {})
|
||||
const pin = userData?.PIN
|
||||
|
||||
//if( encrypted == pin) {
|
||||
|
||||
if( encrypted == localStorage.getItem('PIN')) {
|
||||
if( encrypted == this.localstoreService.get('UserData', false)) {
|
||||
|
||||
//this.toastService.successMessage()
|
||||
this.router.navigate(['/home/events']);
|
||||
@@ -197,15 +199,16 @@ export class LoginPage implements OnInit {
|
||||
|
||||
const code = this.code.join('')
|
||||
const encrypted = crypto.SHA1(code)
|
||||
let userData: Object = JSON.parse(localStorage.getItem('UserData')) || {}
|
||||
let userData: Object = this.localstoreService.get('UserData', {})
|
||||
|
||||
userData['PIN'] = encrypted
|
||||
userData['loginPreference'] = 'none'
|
||||
|
||||
|
||||
localStorage.setItem('UserData', JSON.stringify(userData) )
|
||||
|
||||
this.localstoreService.set('UserData', userData)
|
||||
|
||||
localStorage.setItem('PIN', encrypted)
|
||||
|
||||
this.localstoreService.set('PIN', encrypted)
|
||||
|
||||
//
|
||||
if(window['cy']) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { HttpService } from './http.service';
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { AuthConnstants } from '../config/auth-constants';
|
||||
import { AlertController } from '@ionic/angular';
|
||||
import { LocalstoreService } from '../store/localstore.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -26,15 +27,15 @@ export class AuthService {
|
||||
private storageService:StorageService,
|
||||
private router:Router,
|
||||
public alertController: AlertController,
|
||||
private localstoreService: LocalstoreService
|
||||
) {
|
||||
|
||||
|
||||
|
||||
this.headers = new HttpHeaders();
|
||||
|
||||
if (localStorage.getItem("user") != null) {
|
||||
this.ValidatedUser = JSON.parse(localStorage.getItem('user'));
|
||||
if (localstoreService.get('user', null) != null) {
|
||||
this.ValidatedUser = localstoreService.get('user',{});
|
||||
}
|
||||
|
||||
if (localStorage.getItem("userChat") != null) {
|
||||
this.ValidatedUserChat = JSON.parse(localStorage.getItem('userChat'));
|
||||
}
|
||||
@@ -71,7 +72,7 @@ export class AuthService {
|
||||
response.BasicAuthKey = user.BasicAuthKey
|
||||
this.ValidatedUser = response;
|
||||
|
||||
localStorage.setItem('user', JSON.stringify(response));
|
||||
this.localstoreService.set('user',response)
|
||||
|
||||
this.storageService.store(AuthConnstants.USER, response);
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { User } from 'src/app/models/user.model';
|
||||
import { AuthService } from 'src/app/services/auth.service';
|
||||
import { FingerprintPage } from 'src/app/shared/fingerprint/fingerprint.page';
|
||||
import { PinPage } from 'src/app/shared/pin/pin.page';
|
||||
import { LocalstoreService } from 'src/app/store/localstore.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-profile',
|
||||
@@ -19,7 +20,9 @@ export class ProfileComponent implements OnInit {
|
||||
constructor(private modalController:ModalController,
|
||||
private authService: AuthService,
|
||||
private animationController: AnimationController,
|
||||
private router: Router) {
|
||||
private router: Router,
|
||||
private localstoreService: LocalstoreService
|
||||
) {
|
||||
|
||||
this.loggeduser = authService.ValidatedUser;
|
||||
|
||||
@@ -117,7 +120,7 @@ export class ProfileComponent implements OnInit {
|
||||
|
||||
LoginPreferenceMethod(type: string) {
|
||||
|
||||
let userData = JSON.parse(localStorage.getItem('UserData')) || {}
|
||||
let userData = this.localstoreService.get('UserData', {})
|
||||
|
||||
if (userData.hasOwnProperty('loginPreference')) {
|
||||
if (userData.loginPreference != type) {
|
||||
@@ -132,13 +135,14 @@ export class ProfileComponent implements OnInit {
|
||||
userData.loginPreference = 'none'
|
||||
}
|
||||
|
||||
localStorage.setItem('UserData', JSON.stringify(userData) )
|
||||
|
||||
this.localstoreService.set('UserData', userData)
|
||||
}
|
||||
|
||||
checkState() {
|
||||
|
||||
let userData = JSON.parse(localStorage.getItem('UserData')) || {}
|
||||
|
||||
let userData = this.localstoreService.get('UserData', {})
|
||||
|
||||
if (userData.hasOwnProperty('loginPreference')) {
|
||||
this.userLoginPreference = userData.loginPreference
|
||||
} else {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { BadRequestPage } from '../popover/bad-request/bad-request.page';
|
||||
import { SuccessMessagePage } from '../popover/success-message/success-message.page';
|
||||
import crypto from 'crypto-js'
|
||||
import { ToastService } from 'src/app/services/toast.service';
|
||||
import { LocalstoreService } from 'src/app/store/localstore.service';
|
||||
@Component({
|
||||
selector: 'app-pin',
|
||||
templateUrl: './pin.page.html',
|
||||
@@ -14,7 +15,8 @@ export class PinPage implements OnInit {
|
||||
code = []
|
||||
constructor( private modalController: ModalController,
|
||||
private animationController: AnimationController,
|
||||
private toastService: ToastService) { }
|
||||
private toastService: ToastService,
|
||||
private localstoreService: LocalstoreService) { }
|
||||
|
||||
ngOnInit() {
|
||||
}
|
||||
@@ -49,6 +51,7 @@ export class PinPage implements OnInit {
|
||||
const encrypted = crypto.SHA1(code)
|
||||
|
||||
localStorage.setItem('PIN', encrypted)
|
||||
this.localstoreService.set('PIN', encrypted)
|
||||
|
||||
} else {
|
||||
this.toastService.badRequest()
|
||||
|
||||
@@ -1,36 +1,48 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { SHA1, SHA256, AES, enc } from 'crypto-js'
|
||||
import { AES, enc, SHA1 } from 'crypto-js'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class LocalstoreService {
|
||||
|
||||
constructor() { }
|
||||
private prefix = 'v0-'
|
||||
|
||||
constructor() { }
|
||||
|
||||
get( keyName, safe) {
|
||||
|
||||
keyName = this.prefix + keyName
|
||||
|
||||
const ciphertext = localStorage.getItem(keyName)
|
||||
|
||||
const hashKey = SHA1(keyName).toString()
|
||||
|
||||
if(ciphertext) {
|
||||
const bytes = AES.decrypt(ciphertext, keyName)
|
||||
var decryptedData = JSON.parse(bytes.toString(enc.Utf8));
|
||||
const bytes = AES.decrypt(ciphertext, hashKey)
|
||||
var decryptedData = bytes.toString(enc.Utf8);
|
||||
if(typeof(decryptedData) != 'string') {
|
||||
decryptedData = JSON.parse(decryptedData)
|
||||
}
|
||||
return decryptedData;
|
||||
} else {
|
||||
return safe;
|
||||
}
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
set(keyName, value) {
|
||||
|
||||
keyName = this.prefix + keyName
|
||||
|
||||
if(typeof(value) != 'string') {
|
||||
value = JSON.stringify(value)
|
||||
}
|
||||
}
|
||||
|
||||
const hashKey = SHA1(keyName).toString()
|
||||
|
||||
const data = value
|
||||
const encoded = AES.encrypt( data, key).toString();
|
||||
localStorage.setItem(key, encoded)
|
||||
const encoded = AES.encrypt( data, hashKey).toString();
|
||||
localStorage.setItem(keyName, encoded)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user