Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add helper auth service
Roland Geidere88bc847bb39
diff --git a/angular/src/app/core/auth.service.ts b/angular/src/app/core/auth.service.tsnew file mode 100644index 000000000..b88981262--- /dev/null+++ b/angular/src/app/core/auth.service.ts@@ -0,0 +1,50 @@+import {HttpHeaders} from '@angular/common/http';+import {Injectable} from '@angular/core';+import {environment} from '../../environments/environment';++/**+ * Helper service that sets the appropriate headers+ *+ * During local development we use the Token defined in the dev environment+ * but on the wger application, we need to pass the CSRF value in the header+ */+@Injectable({+ providedIn: 'root',+})+export class AuthService {++ DJANGO_CSRF_COOKIE = 'csrftoken';++ constructor() {+ }++ // from https://docs.djangoproject.com/en/3.2/ref/csrf/#ajax+ getCookie(name: string) {+ let cookieValue = null;+ if (document.cookie && document.cookie !== '') {+ const cookies = document.cookie.split(';');+ for (let i = 0; i < cookies.length; i++) {+ const cookie = cookies[i].trim();+ // Does this cookie string begin with the name we want?+ if (cookie.substring(0, name.length + 1) === (name + '=')) {+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));+ break;+ }+ }+ }+ return cookieValue;+ }++ get headers(): HttpHeaders {+ const csrf_cookie = this.getCookie(this.DJANGO_CSRF_COOKIE);+ let headers = environment.headers;++ if(csrf_cookie != undefined)+ {+ headers = headers.set('X-CSRFToken', csrf_cookie);+ }+ return headers;+ }+++}diff --git a/angular/src/app/weight/weight.service.ts b/angular/src/app/weight/weight.service.tsindex a77b5a4a9..90697e0dc 100644--- a/angular/src/app/weight/weight.service.ts+++ b/angular/src/app/weight/weight.service.ts@@ -1,6 +1,7 @@import {HttpClient} from '@angular/common/http';import {Injectable} from '@angular/core';import {environment} from '../../environments/environment';+import {AuthService} from '../core/auth.service';import {WeightAdapter, WeightEntry} from './models/weight.model';@@ -14,9 +15,12 @@ export class WeightService {constructor(private http: HttpClient,private weightAdapter: WeightAdapter,+ private authService: AuthService,) {+}+/*** Sorts weight entries by date.*@@ -29,7 +33,9 @@ export class WeightService {async loadWeightEntries(): Promise<WeightEntry[]> {- const data = await this.http.get<any>(this.weightEntryUrl, {params: {limit: 500}, headers: environment.headers}).toPromise();++ const data = await this.http.get<any>(this.weightEntryUrl,+ {params: {limit: 500}, headers: this.authService.headers}).toPromise();for (const weightData of data.results) {this.entries.push(this.weightAdapter.fromJson(weightData));@@ -45,8 +51,9 @@ export class WeightService {* @param weight a [WeightEntry] instance*/updateWeightEntry(weight: WeightEntry) {- this.http.patch<any>(this.weightEntryUrl + weight.id + '/', this.weightAdapter.toJson(weight), {- headers: environment.headers+ this.http.patch<any>(this.weightEntryUrl + weight.id + '/',+ this.weightAdapter.toJson(weight), {+ headers: this.authService.headers}).subscribe(value => {});}@@ -58,7 +65,7 @@ export class WeightService {*/addWeightEntry(data: { weight: number, date: Date }) {this.http.post<any>(this.weightEntryUrl, data, {- headers: environment.headers+ headers: this.authService.headers}).subscribe(value => {this.entries.push(this.weightAdapter.fromJson(value));this.sortEntries();@@ -73,7 +80,7 @@ export class WeightService {deleteWeightEntry(id: number) {this.http.delete<any>(this.weightEntryUrl + id + '/', {- headers: environment.headers+ headers: this.authService.headers}).subscribe();this.entries.forEach((value: WeightEntry, index: number) => {