mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2026-04-29 00:30:11 +00:00
BETTERZON-146: Changing session handling from cookies to localStorage
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
|
||||
import {ApiService} from "../../../services/api.service";
|
||||
import {Router} from "@angular/router";
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
||||
import {ApiService} from '../../../services/api.service';
|
||||
import {Router} from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-signin',
|
||||
@@ -23,33 +23,34 @@ export class SigninComponent implements OnInit {
|
||||
private formBuilder: FormBuilder,
|
||||
private api: ApiService,
|
||||
private router: Router
|
||||
) { }
|
||||
) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loginForm = this.formBuilder.group({
|
||||
username: ['', Validators.required],
|
||||
password: ['', [Validators.required, Validators.minLength(8)]]
|
||||
});
|
||||
}
|
||||
this.loginForm = this.formBuilder.group({
|
||||
username: ['', Validators.required],
|
||||
password: ['', [Validators.required, Validators.minLength(8)]]
|
||||
});
|
||||
}
|
||||
|
||||
onSubmit() {
|
||||
onSubmit(): void {
|
||||
this.submitted = true;
|
||||
|
||||
this.submitted = true;
|
||||
if (this.loginForm.invalid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.loginForm.invalid) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.api.loginUser(this.loginForm.value.username, this.loginForm.value.password)
|
||||
.subscribe(
|
||||
data => {
|
||||
this.router.navigate(['']);
|
||||
this.isSuccessful = true;
|
||||
this.isSignUpFailed = false;
|
||||
},
|
||||
err => {
|
||||
this.errorMessage = err.error.message;
|
||||
this.isSignUpFailed = true;
|
||||
})
|
||||
}
|
||||
this.api.loginUser(this.loginForm.value.username, this.loginForm.value.password)
|
||||
.subscribe(
|
||||
data => {
|
||||
this.router.navigate(['']);
|
||||
this.isSuccessful = true;
|
||||
this.isSignUpFailed = false;
|
||||
this.api.saveSessionInfoToLocalStorage(data);
|
||||
},
|
||||
err => {
|
||||
this.errorMessage = err.error.message;
|
||||
this.isSignUpFailed = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,7 +243,11 @@ export class ApiService {
|
||||
*/
|
||||
addNewPrice(vendorId: number, productId: number, price: number): Observable<any> {
|
||||
try {
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.post((this.apiUrl + '/prices'), JSON.stringify({
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
vendor_id: vendorId,
|
||||
product_id: productId,
|
||||
price_in_cents: price
|
||||
@@ -278,7 +282,13 @@ export class ApiService {
|
||||
*/
|
||||
getManagedVendors(): Observable<Vendor[]> {
|
||||
try {
|
||||
return this.http.get<Vendor[]>((this.apiUrl + '/vendors/managed'));
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
let params = new HttpParams();
|
||||
params = params.append('session_id', sessionInfo.session_id);
|
||||
params = params.append('session_key', sessionInfo.session_key);
|
||||
|
||||
return this.http.get<Vendor[]>((this.apiUrl + '/vendors/managed'), {params});
|
||||
} catch (exception) {
|
||||
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
||||
}
|
||||
@@ -318,7 +328,11 @@ export class ApiService {
|
||||
*/
|
||||
deactivateSingleVendorListing(vendorId: number, productId: number): Observable<any> {
|
||||
try {
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.put((this.apiUrl + '/vendors/manage/deactivatelisting'), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
vendor_id: vendorId,
|
||||
product_id: productId
|
||||
});
|
||||
@@ -334,7 +348,12 @@ export class ApiService {
|
||||
*/
|
||||
deactivateVendor(vendorId: number): Observable<any> {
|
||||
try {
|
||||
return this.http.put((this.apiUrl + '/vendors/manage/shop/deactivate/' + vendorId), {});
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.put((this.apiUrl + '/vendors/manage/shop/deactivate/' + vendorId), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
});
|
||||
} catch (exception) {
|
||||
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
||||
}
|
||||
@@ -347,7 +366,12 @@ export class ApiService {
|
||||
*/
|
||||
activateVendor(vendorId: number): Observable<any> {
|
||||
try {
|
||||
return this.http.put((this.apiUrl + '/vendors/manage/shop/activate/' + vendorId), {});
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.put((this.apiUrl + '/vendors/manage/shop/activate/' + vendorId), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
});
|
||||
} catch (exception) {
|
||||
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
||||
}
|
||||
@@ -367,7 +391,13 @@ export class ApiService {
|
||||
*/
|
||||
getPriceAlarms(): Observable<PriceAlarm[]> {
|
||||
try {
|
||||
return this.http.get<PriceAlarm[]>((this.apiUrl + '/pricealarms'));
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
let params = new HttpParams();
|
||||
params = params.append('session_id', sessionInfo.session_id);
|
||||
params = params.append('session_key', sessionInfo.session_key);
|
||||
|
||||
return this.http.get<PriceAlarm[]>((this.apiUrl + '/pricealarms'), {params});
|
||||
} catch (exception) {
|
||||
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
||||
}
|
||||
@@ -381,7 +411,11 @@ export class ApiService {
|
||||
*/
|
||||
createPriceAlarms(productId: number, definedPrice: number): Observable<any> {
|
||||
try {
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.post((this.apiUrl + '/pricealarms'), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
product_id: productId,
|
||||
defined_price: definedPrice
|
||||
});
|
||||
@@ -398,7 +432,11 @@ export class ApiService {
|
||||
*/
|
||||
updatePriceAlarms(alarmId: number, definedPrice: number): Observable<any> {
|
||||
try {
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.put((this.apiUrl + '/pricealarms'), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
alarm_id: alarmId,
|
||||
defined_price: definedPrice
|
||||
});
|
||||
@@ -458,12 +496,36 @@ export class ApiService {
|
||||
*/
|
||||
getUserInfo(): Observable<any> {
|
||||
try {
|
||||
return this.http.post((this.apiUrl + '/users/checkSessionValid'), {});
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
return this.http.post((this.apiUrl + '/users/checkSessionValid'), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key
|
||||
});
|
||||
} catch (exception) {
|
||||
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets session id and session key from local storage
|
||||
* @return any {session_id: '', session_key: ''}
|
||||
*/
|
||||
getSessionInfoFromLocalStorage(): any {
|
||||
const session_id = localStorage.getItem('session_id') ?? '';
|
||||
const session_key = localStorage.getItem('session_key') ?? '';
|
||||
return {session_id, session_key};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts and saves the session data from an api response
|
||||
* @param data The api response
|
||||
*/
|
||||
saveSessionInfoToLocalStorage(data: any): boolean {
|
||||
localStorage.setItem('session_id', data.session_id);
|
||||
localStorage.setItem('session_key', data.session_key);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ______ _ __ __
|
||||
/ ____/___ __ ______ _____(_) /____ _____/ /_ ____ ____ _____
|
||||
/ /_ / __ `/ | / / __ \/ ___/ / __/ _ \ / ___/ __ \/ __ \/ __ \/ ___/
|
||||
@@ -478,7 +540,13 @@ export class ApiService {
|
||||
*/
|
||||
getFavoriteShops(): Observable<FavoriteShop[]> {
|
||||
try {
|
||||
return this.http.get<FavoriteShop[]>((this.apiUrl + '/favoriteshops'));
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
let params = new HttpParams();
|
||||
params = params.append('session_id', sessionInfo.session_id);
|
||||
params = params.append('session_key', sessionInfo.session_key);
|
||||
|
||||
return this.http.get<FavoriteShop[]>((this.apiUrl + '/favoriteshops'), {params});
|
||||
} catch (exception) {
|
||||
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
||||
}
|
||||
@@ -491,7 +559,11 @@ export class ApiService {
|
||||
*/
|
||||
addFavoriteShop(vendorId: number): Observable<any> {
|
||||
try {
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.post((this.apiUrl + '/favoriteshops'), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
vendor_id: vendorId
|
||||
});
|
||||
} catch (exception) {
|
||||
@@ -506,7 +578,13 @@ export class ApiService {
|
||||
*/
|
||||
deleteFavoriteShop(vendorId: number): Observable<any> {
|
||||
try {
|
||||
return this.http.delete((this.apiUrl + '/favoriteshops/' + vendorId));
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
let params = new HttpParams();
|
||||
params = params.append('session_id', sessionInfo.session_id);
|
||||
params = params.append('session_key', sessionInfo.session_key);
|
||||
|
||||
return this.http.delete((this.apiUrl + '/favoriteshops/' + vendorId), {params});
|
||||
} catch (exception) {
|
||||
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
||||
}
|
||||
@@ -570,7 +648,11 @@ export class ApiService {
|
||||
*/
|
||||
addContactPerson(vendorId: number, firstName: string, lastName: string, gender: string, email: string, phone: string): Observable<any> {
|
||||
try {
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.post((this.apiUrl + '/contactpersons'), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
vendor_id: vendorId,
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
@@ -596,7 +678,11 @@ export class ApiService {
|
||||
*/
|
||||
updateContactPerson(contactId: number, vendorId: number, firstName: string, lastName: string, gender: string, email: string, phone: string): Observable<any> {
|
||||
try {
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
return this.http.put((this.apiUrl + '/contactpersons/' + contactId), {
|
||||
session_id: sessionInfo.session_id,
|
||||
session_key: sessionInfo.session_key,
|
||||
vendor_id: vendorId,
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
@@ -718,7 +804,13 @@ export class ApiService {
|
||||
*/
|
||||
getCurrentCrawlingStatus(): Observable<CrawlingStatus> {
|
||||
try {
|
||||
return this.http.get<CrawlingStatus>((this.apiUrl + '/crawlingstatus'));
|
||||
const sessionInfo = this.getSessionInfoFromLocalStorage();
|
||||
|
||||
let params = new HttpParams();
|
||||
params = params.append('session_id', sessionInfo.session_id);
|
||||
params = params.append('session_key', sessionInfo.session_key);
|
||||
|
||||
return this.http.get<CrawlingStatus>((this.apiUrl + '/crawlingstatus'), {params});
|
||||
} catch (exception) {
|
||||
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user