From 68e9d75e2dcceec1cfd12a5d38003e59bb2046a1 Mon Sep 17 00:00:00 2001 From: Jegor Date: Sun, 13 Jun 2021 12:49:15 +0200 Subject: [PATCH 1/5] wip: register api --- .../registration/registration.component.html | 17 +++++++------ .../registration/registration.component.ts | 25 ++++++++++++++++++- .../auth/signin/signin.component.html | 8 +++--- .../auth/signin/signin.component.ts | 22 ++++++++++++---- .../hot-deals-widget.component.html | 4 +-- Frontend/src/app/models/product.ts | 1 + .../product-detail-page.component.html | 2 +- Frontend/src/app/services/api.service.ts | 1 + 8 files changed, 60 insertions(+), 20 deletions(-) diff --git a/Frontend/src/app/components/auth/registration/registration.component.html b/Frontend/src/app/components/auth/registration/registration.component.html index b752579..c18e6e0 100644 --- a/Frontend/src/app/components/auth/registration/registration.component.html +++ b/Frontend/src/app/components/auth/registration/registration.component.html @@ -9,27 +9,30 @@

Konto erstellen

-
+
- + +
+
Username is required
+
- +
- +
+ -
+ -->
-

Haben Sie bereits ein Konto?Sich anmelden

+ Sich anmelden
diff --git a/Frontend/src/app/components/auth/registration/registration.component.ts b/Frontend/src/app/components/auth/registration/registration.component.ts index 2c5f5a5..3f54551 100644 --- a/Frontend/src/app/components/auth/registration/registration.component.ts +++ b/Frontend/src/app/components/auth/registration/registration.component.ts @@ -1,4 +1,7 @@ import { Component, OnInit } from '@angular/core'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import {ApiService} from "../../../services/api.service"; + @Component({ selector: 'app-registration', @@ -6,10 +9,30 @@ import { Component, OnInit } from '@angular/core'; styleUrls: ['./registration.component.css'] }) export class RegistrationComponent implements OnInit { + form: any; + loading = false; + submitted = false; - constructor() { } + constructor( + private formBuilder: FormBuilder, + private api : ApiService + ) { } ngOnInit(): void { + this.form = this.formBuilder.group({ + username: ['', Validators.required], + email: ['', Validators.required], + password: ['', [ + Validators.required, + Validators.minLength(8)] + ], + }); } + get me() { return this.form.controls; } + + onSubmit() { + console.log(this.form.value); + this.api.registerUser(this.form.value.username, this.form.value.password, this.form.value.email).subscribe(res=>console.log(res)); + } } diff --git a/Frontend/src/app/components/auth/signin/signin.component.html b/Frontend/src/app/components/auth/signin/signin.component.html index 0a415c6..dcfb76a 100644 --- a/Frontend/src/app/components/auth/signin/signin.component.html +++ b/Frontend/src/app/components/auth/signin/signin.component.html @@ -9,13 +9,13 @@

Anmelden

-
+
- +
- +
@@ -23,7 +23,7 @@
-

Noch kein Konto?Konto erstellen

+ Konto erstellen
diff --git a/Frontend/src/app/components/auth/signin/signin.component.ts b/Frontend/src/app/components/auth/signin/signin.component.ts index e497f15..8bf594f 100644 --- a/Frontend/src/app/components/auth/signin/signin.component.ts +++ b/Frontend/src/app/components/auth/signin/signin.component.ts @@ -1,4 +1,6 @@ import { Component, OnInit } from '@angular/core'; +import {FormBuilder, Validators} from "@angular/forms"; +import {ApiService} from "../../../services/api.service"; @Component({ selector: 'app-signin', @@ -8,12 +10,22 @@ import { Component, OnInit } from '@angular/core'; export class SigninComponent implements OnInit { - constructor() { } + form: any; - ngOnInit(): void { - } + constructor( + private formBuilder: FormBuilder, + private api : ApiService + ) { } - onSubmit() { + ngOnInit(): void { + this.form = this.formBuilder.group({ + email: ['', Validators.required], + password: ['', Validators.required] + }); + } - } + onSubmit() { + console.log(this.form.value); + this.api.loginUser(this.form.value.username, this.form.value.password); + } } diff --git a/Frontend/src/app/components/hot-deals-widget/hot-deals-widget.component.html b/Frontend/src/app/components/hot-deals-widget/hot-deals-widget.component.html index 36ea156..a77fc6f 100644 --- a/Frontend/src/app/components/hot-deals-widget/hot-deals-widget.component.html +++ b/Frontend/src/app/components/hot-deals-widget/hot-deals-widget.component.html @@ -9,7 +9,7 @@
-
+
@@ -17,7 +17,7 @@
{{product.name}}
-
Amazon: 699.00$
+
Amazon: {{product.price}}$
Plantshub: 599,00$
diff --git a/Frontend/src/app/models/product.ts b/Frontend/src/app/models/product.ts index 8c51860..a73850e 100644 --- a/Frontend/src/app/models/product.ts +++ b/Frontend/src/app/models/product.ts @@ -11,4 +11,5 @@ export interface Product { manufacturer_id: number; selling_rank: string; category_id: number; + price: number; } diff --git a/Frontend/src/app/pages/product-detail-page/product-detail-page.component.html b/Frontend/src/app/pages/product-detail-page/product-detail-page.component.html index bf2d7cc..a75e4d4 100644 --- a/Frontend/src/app/pages/product-detail-page/product-detail-page.component.html +++ b/Frontend/src/app/pages/product-detail-page/product-detail-page.component.html @@ -1,7 +1,7 @@ -
+ diff --git a/Frontend/src/app/services/api.service.ts b/Frontend/src/app/services/api.service.ts index 8d06a81..20e9e27 100644 --- a/Frontend/src/app/services/api.service.ts +++ b/Frontend/src/app/services/api.service.ts @@ -11,6 +11,7 @@ import {ContactPerson} from '../models/contactperson'; import {Category} from '../models/category'; import {Manufacturer} from '../models/manufacturer'; import {CrawlingStatus} from '../models/crawlingstatus'; +import {log} from "util"; @Injectable({ providedIn: 'root' From f2adb1e37571fd874b825614c37722ebb8ebb818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Sun, 13 Jun 2021 13:09:51 +0200 Subject: [PATCH 2/5] BETTERZON-141: Fixing service class - Because Patrick was stupid --- Frontend/src/app/services/api.service.ts | 38 ++++++++++++------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Frontend/src/app/services/api.service.ts b/Frontend/src/app/services/api.service.ts index 20e9e27..dbaddb0 100644 --- a/Frontend/src/app/services/api.service.ts +++ b/Frontend/src/app/services/api.service.ts @@ -11,7 +11,7 @@ import {ContactPerson} from '../models/contactperson'; import {Category} from '../models/category'; import {Manufacturer} from '../models/manufacturer'; import {CrawlingStatus} from '../models/crawlingstatus'; -import {log} from "util"; +import {log} from 'util'; @Injectable({ providedIn: 'root' @@ -206,10 +206,10 @@ export class ApiService { */ deactivateSingleVendorListing(vendorId: number, productId: number): Observable { try { - return this.http.put((this.apiUrl + '/vendors/manage/deactivatelisting'), JSON.stringify({ + return this.http.put((this.apiUrl + '/vendors/manage/deactivatelisting'), { vendor_id: vendorId, product_id: productId - })); + }); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -222,7 +222,7 @@ export class ApiService { */ deactivateVendor(vendorId: number): Observable { try { - return this.http.put((this.apiUrl + '/vendors/manage/shop/deactivate/' + vendorId), JSON.stringify({})); + return this.http.put((this.apiUrl + '/vendors/manage/shop/deactivate/' + vendorId), {}); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -235,7 +235,7 @@ export class ApiService { */ activateVendor(vendorId: number): Observable { try { - return this.http.put((this.apiUrl + '/vendors/manage/shop/activate/' + vendorId), JSON.stringify({})); + return this.http.put((this.apiUrl + '/vendors/manage/shop/activate/' + vendorId), {}); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -269,10 +269,10 @@ export class ApiService { */ createPriceAlarms(productId: number, definedPrice: number): Observable { try { - return this.http.post((this.apiUrl + '/pricealarms'), JSON.stringify({ + return this.http.post((this.apiUrl + '/pricealarms'), { product_id: productId, defined_price: definedPrice - })); + }); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -286,10 +286,10 @@ export class ApiService { */ updatePriceAlarms(alarmId: number, definedPrice: number): Observable { try { - return this.http.put((this.apiUrl + '/pricealarms'), JSON.stringify({ + return this.http.put((this.apiUrl + '/pricealarms'), { alarm_id: alarmId, defined_price: definedPrice - })); + }); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -312,11 +312,11 @@ export class ApiService { */ registerUser(username: string, password: string, email: string): Observable { try { - return this.http.post((this.apiUrl + '/users/register'), JSON.stringify({ + return this.http.post((this.apiUrl + '/users/register'), { username, password, email - })); + }); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -330,10 +330,10 @@ export class ApiService { */ loginUser(username: string, password: string): Observable { try { - return this.http.post((this.apiUrl + '/users/login'), JSON.stringify({ + return this.http.post((this.apiUrl + '/users/login'), { username, password - })); + }); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -366,9 +366,9 @@ export class ApiService { */ addFavoriteShop(vendorId: number): Observable { try { - return this.http.post((this.apiUrl + '/favoriteshops'), JSON.stringify({ + return this.http.post((this.apiUrl + '/favoriteshops'), { vendor_id: vendorId - })); + }); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -445,14 +445,14 @@ export class ApiService { */ addContactPerson(vendorId: number, firstName: string, lastName: string, gender: string, email: string, phone: string): Observable { try { - return this.http.post((this.apiUrl + '/contactpersons'), JSON.stringify({ + return this.http.post((this.apiUrl + '/contactpersons'), { vendor_id: vendorId, first_name: firstName, last_name: lastName, gender, email, phone - })); + }); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } @@ -471,14 +471,14 @@ export class ApiService { */ updateContactPerson(contactId: number, vendorId: number, firstName: string, lastName: string, gender: string, email: string, phone: string): Observable { try { - return this.http.put((this.apiUrl + '/contactpersons/' + contactId), JSON.stringify({ + return this.http.put((this.apiUrl + '/contactpersons/' + contactId), { vendor_id: vendorId, first_name: firstName, last_name: lastName, gender, email, phone - })); + }); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } From daef6ec208703a69ac9b7e08a9156c4f09cb42b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Sun, 13 Jun 2021 14:16:09 +0200 Subject: [PATCH 3/5] BETTERZON-141: Fixing user updating query --- Backend/src/models/users/users.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Backend/src/models/users/users.service.ts b/Backend/src/models/users/users.service.ts index 639ca5b..77a9785 100644 --- a/Backend/src/models/users/users.service.ts +++ b/Backend/src/models/users/users.service.ts @@ -115,8 +115,8 @@ export const login = async (username: string, password: string, ip: string): Pro const sessionKeyHash = bcrypt.hashSync(sessionKey, 10); // Update user entry in SQL - const userQuery = 'UPDATE users SET last_login_date = NOW()'; - const userIdRes = await conn.query(userQuery); + const userQuery = 'UPDATE users SET last_login_date = NOW() WHERE user_id = ?'; + const userIdRes = await conn.query(userQuery, userId); await conn.commit(); // Create session From b062e14c9a1d6bce5698c5c206098fcc1274cdf6 Mon Sep 17 00:00:00 2001 From: Jegor Date: Tue, 15 Jun 2021 10:38:54 +0200 Subject: [PATCH 4/5] auth with cookies. --- .../registration/registration.component.css | 2 +- .../registration/registration.component.ts | 1 - .../auth/signin/signin.component.css | 2 +- .../auth/signin/signin.component.html | 2 +- .../auth/signin/signin.component.ts | 40 +++++++++++++++---- .../components/top-bar/top-bar.component.ts | 9 ++++- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/Frontend/src/app/components/auth/registration/registration.component.css b/Frontend/src/app/components/auth/registration/registration.component.css index 08f1877..6de2e1b 100644 --- a/Frontend/src/app/components/auth/registration/registration.component.css +++ b/Frontend/src/app/components/auth/registration/registration.component.css @@ -61,7 +61,7 @@ form{ } .btn_signin{ transition: all .5s ease; - width: 70%; + width: 100%; border-radius: 30px; color:#008080; font-weight: 600; diff --git a/Frontend/src/app/components/auth/registration/registration.component.ts b/Frontend/src/app/components/auth/registration/registration.component.ts index 3f54551..ff8c904 100644 --- a/Frontend/src/app/components/auth/registration/registration.component.ts +++ b/Frontend/src/app/components/auth/registration/registration.component.ts @@ -32,7 +32,6 @@ export class RegistrationComponent implements OnInit { get me() { return this.form.controls; } onSubmit() { - console.log(this.form.value); this.api.registerUser(this.form.value.username, this.form.value.password, this.form.value.email).subscribe(res=>console.log(res)); } } diff --git a/Frontend/src/app/components/auth/signin/signin.component.css b/Frontend/src/app/components/auth/signin/signin.component.css index 08f1877..6de2e1b 100644 --- a/Frontend/src/app/components/auth/signin/signin.component.css +++ b/Frontend/src/app/components/auth/signin/signin.component.css @@ -61,7 +61,7 @@ form{ } .btn_signin{ transition: all .5s ease; - width: 70%; + width: 100%; border-radius: 30px; color:#008080; font-weight: 600; diff --git a/Frontend/src/app/components/auth/signin/signin.component.html b/Frontend/src/app/components/auth/signin/signin.component.html index dcfb76a..358f183 100644 --- a/Frontend/src/app/components/auth/signin/signin.component.html +++ b/Frontend/src/app/components/auth/signin/signin.component.html @@ -9,7 +9,7 @@

Anmelden

-
+
diff --git a/Frontend/src/app/components/auth/signin/signin.component.ts b/Frontend/src/app/components/auth/signin/signin.component.ts index 8bf594f..cf4142e 100644 --- a/Frontend/src/app/components/auth/signin/signin.component.ts +++ b/Frontend/src/app/components/auth/signin/signin.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; -import {FormBuilder, Validators} from "@angular/forms"; +import {FormBuilder, FormGroup, Validators} from "@angular/forms"; import {ApiService} from "../../../services/api.service"; +import {Router} from "@angular/router"; @Component({ selector: 'app-signin', @@ -10,22 +11,45 @@ import {ApiService} from "../../../services/api.service"; export class SigninComponent implements OnInit { - form: any; + loginForm: FormGroup; + loading = false; + submitted = false; + private isSuccessful: boolean; + private isSignUpFailed: boolean; + private errorMessage: ''; + constructor( private formBuilder: FormBuilder, - private api : ApiService + private api: ApiService, + private router: Router ) { } ngOnInit(): void { - this.form = this.formBuilder.group({ - email: ['', Validators.required], - password: ['', Validators.required] + this.loginForm = this.formBuilder.group({ + username: ['', Validators.required], + password: ['', [Validators.required, Validators.minLength(8)]] }); } onSubmit() { - console.log(this.form.value); - this.api.loginUser(this.form.value.username, this.form.value.password); + + this.submitted = true; + + 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; + }) } } diff --git a/Frontend/src/app/components/top-bar/top-bar.component.ts b/Frontend/src/app/components/top-bar/top-bar.component.ts index 15aa721..f2e667b 100644 --- a/Frontend/src/app/components/top-bar/top-bar.component.ts +++ b/Frontend/src/app/components/top-bar/top-bar.component.ts @@ -1,4 +1,6 @@ import { Component, OnInit } from '@angular/core'; +import {ApiService} from "../../services/api.service"; + @Component({ selector: 'app-top-bar', @@ -9,9 +11,12 @@ export class TopBarComponent implements OnInit { sidenav: any; - constructor() { } + constructor( + private api: ApiService + ) { } ngOnInit() { - } + this.api.getUserInfo().subscribe(data=>{console.log(data)}); + } } From bf56d2b50944fe63c3547bb84e1a7596e51dcaea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Tue, 15 Jun 2021 11:32:48 +0200 Subject: [PATCH 5/5] BETTERZON-146: Changing session handling from cookies to localStorage --- .../contact_persons/contact_persons.router.ts | 8 +- .../crawling_status/crawling_status.router.ts | 4 +- .../favorite_shops/favoriteshops.router.ts | 12 +- .../models/pricealarms/pricealarms.router.ts | 12 +- Backend/src/models/prices/prices.router.ts | 4 +- Backend/src/models/users/users.router.ts | 22 ++-- Backend/src/models/vendors/vendors.router.ts | 16 ++- .../auth/signin/signin.component.ts | 57 ++++----- Frontend/src/app/services/api.service.ts | 108 ++++++++++++++++-- 9 files changed, 183 insertions(+), 60 deletions(-) diff --git a/Backend/src/models/contact_persons/contact_persons.router.ts b/Backend/src/models/contact_persons/contact_persons.router.ts index b06f62d..071482b 100644 --- a/Backend/src/models/contact_persons/contact_persons.router.ts +++ b/Backend/src/models/contact_persons/contact_persons.router.ts @@ -76,7 +76,9 @@ contactpersonsRouter.post('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get required parameters const vendor_id = req.body.vendor_id; @@ -104,7 +106,9 @@ contactpersonsRouter.put('/:id', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get required parameters const contact_person_id = parseInt(req.params.id, 10); diff --git a/Backend/src/models/crawling_status/crawling_status.router.ts b/Backend/src/models/crawling_status/crawling_status.router.ts index 4ca0b89..3d215bd 100644 --- a/Backend/src/models/crawling_status/crawling_status.router.ts +++ b/Backend/src/models/crawling_status/crawling_status.router.ts @@ -25,7 +25,9 @@ crawlingstatusRouter.get('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); if (!user.is_admin) { res.status(403).send({}); diff --git a/Backend/src/models/favorite_shops/favoriteshops.router.ts b/Backend/src/models/favorite_shops/favoriteshops.router.ts index 0c0d033..4cb0266 100644 --- a/Backend/src/models/favorite_shops/favoriteshops.router.ts +++ b/Backend/src/models/favorite_shops/favoriteshops.router.ts @@ -24,7 +24,9 @@ favoriteshopsRouter.get('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.params.session_id; + const session_key = req.params.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); const priceAlarms = await FavoriteShopsService.getFavoriteShops(user.user_id); @@ -40,7 +42,9 @@ favoriteshopsRouter.post('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get info for price alarm creation const vendor_id = req.body.vendor_id; @@ -72,7 +76,9 @@ favoriteshopsRouter.delete('/:id', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.params.session_id; + const session_key = req.params.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get info for price alarm creation const favorite_id = parseInt(req.params.id, 10); diff --git a/Backend/src/models/pricealarms/pricealarms.router.ts b/Backend/src/models/pricealarms/pricealarms.router.ts index fcd4939..a5d01c5 100644 --- a/Backend/src/models/pricealarms/pricealarms.router.ts +++ b/Backend/src/models/pricealarms/pricealarms.router.ts @@ -24,7 +24,9 @@ pricealarmsRouter.get('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.params.session_id; + const session_key = req.params.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); const priceAlarms = await PriceAlarmsService.getPriceAlarms(user.user_id); @@ -40,7 +42,9 @@ pricealarmsRouter.post('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get info for price alarm creation const product_id = req.body.product_id; @@ -73,7 +77,9 @@ pricealarmsRouter.put('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get info for price alarm creation const alarm_id = req.body.alarm_id; diff --git a/Backend/src/models/prices/prices.router.ts b/Backend/src/models/prices/prices.router.ts index 16bc5ba..61ed812 100644 --- a/Backend/src/models/prices/prices.router.ts +++ b/Backend/src/models/prices/prices.router.ts @@ -107,7 +107,9 @@ pricesRouter.post('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get required parameters const vendor_id = req.body.vendor_id; diff --git a/Backend/src/models/users/users.router.ts b/Backend/src/models/users/users.router.ts index d1ca782..7851ef7 100644 --- a/Backend/src/models/users/users.router.ts +++ b/Backend/src/models/users/users.router.ts @@ -47,10 +47,10 @@ usersRouter.post('/register', async (req: Request, res: Response) => { const session: Session = await UserService.createUser(username, password, email, ip); // Send the session details back to the user - res.cookie('betterauth', JSON.stringify({ - id: session.session_id, - key: session.session_key - }), {expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30)}).status(201).send({}); + res.status(201).send({ + session_id: session.session_id, + session_key: session.session_key + }); } catch (e) { console.log('Error handling a request: ' + e.message); res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); @@ -80,10 +80,10 @@ usersRouter.post('/login', async (req: Request, res: Response) => { } // Send the session details back to the user - res.cookie('betterauth', JSON.stringify({ - id: session.session_id, - key: session.session_key - }), {expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30)}).status(200).send({}); + res.status(200).send({ + session_id: session.session_id, + session_key: session.session_key + }); } catch (e) { console.log('Error handling a request: ' + e.message); res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); @@ -94,15 +94,17 @@ usersRouter.post('/login', async (req: Request, res: Response) => { usersRouter.post('/checkSessionValid', async (req: Request, res: Response) => { try { const ip: string = req.connection.remoteAddress ?? ''; + const session_id = req.body.session_id; + const session_key = req.body.session_key; - if(!req.cookies.betterauth) { + if(!session_id || !session_key) { // Error logging in, probably wrong username / password res.status(401).send(JSON.stringify({messages: ['No session detected'], codes: [5]})); return; } // Update the user entry and create a session - const user: User = await UserService.checkSessionWithCookie(req.cookies.betterauth, ip); + const user: User = await UserService.checkSession(session_id, session_key, ip); if (!user.user_id) { // Error logging in, probably wrong username / password diff --git a/Backend/src/models/vendors/vendors.router.ts b/Backend/src/models/vendors/vendors.router.ts index 7888cae..75ccc35 100644 --- a/Backend/src/models/vendors/vendors.router.ts +++ b/Backend/src/models/vendors/vendors.router.ts @@ -37,7 +37,9 @@ vendorsRouter.get('/managed', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.params.session_id; + const session_key = req.params.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); const vendors = await VendorService.getManagedShops(user.user_id); @@ -91,7 +93,9 @@ vendorsRouter.put('/manage/deactivatelisting', async (req: Request, res: Respons try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get required parameters const vendor_id = req.body.vendor_id; @@ -115,7 +119,9 @@ vendorsRouter.put('/manage/shop/deactivate/:id', async (req: Request, res: Respo try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get required parameters const vendor_id = parseInt(req.params.id, 10); @@ -138,7 +144,9 @@ vendorsRouter.put('/manage/shop/activate/:id', async (req: Request, res: Respons try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; - const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user = await UserService.checkSession(session_id, session_key, user_ip); // Get required parameters const vendor_id = parseInt(req.params.id, 10); diff --git a/Frontend/src/app/components/auth/signin/signin.component.ts b/Frontend/src/app/components/auth/signin/signin.component.ts index cf4142e..e39ac56 100644 --- a/Frontend/src/app/components/auth/signin/signin.component.ts +++ b/Frontend/src/app/components/auth/signin/signin.component.ts @@ -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; + }); + } } diff --git a/Frontend/src/app/services/api.service.ts b/Frontend/src/app/services/api.service.ts index 1cefc4b..953c229 100644 --- a/Frontend/src/app/services/api.service.ts +++ b/Frontend/src/app/services/api.service.ts @@ -243,7 +243,11 @@ export class ApiService { */ addNewPrice(vendorId: number, productId: number, price: number): Observable { 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 { try { - return this.http.get((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((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 { 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 { 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 { 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 { try { - return this.http.get((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((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 { 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 { 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 { 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 { try { - return this.http.get((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((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 { 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 { 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 { 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 { 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 { try { - return this.http.get((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((this.apiUrl + '/crawlingstatus'), {params}); } catch (exception) { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); }