From 906bff805caa45b4b99b99a42101dbd77f61549a Mon Sep 17 00:00:00 2001 From: Ivan Sentemon Date: Tue, 7 Jan 2025 20:07:28 +0100 Subject: [PATCH] Implement CookieService --- frontend/src/app/apollo.config.ts | 9 ++++--- .../src/app/core/services/cookie.service.ts | 18 +++++++++++++ .../features/auth/services/auth.service.ts | 26 ++++++++++++++----- 3 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 frontend/src/app/core/services/cookie.service.ts diff --git a/frontend/src/app/apollo.config.ts b/frontend/src/app/apollo.config.ts index 64eb97d..0e2816f 100644 --- a/frontend/src/app/apollo.config.ts +++ b/frontend/src/app/apollo.config.ts @@ -1,18 +1,19 @@ import { ApolloClientOptions, InMemoryCache, ApolloLink, HttpLink } from '@apollo/client/core'; import { setContext } from '@apollo/client/link/context'; import { environment } from '../environments/environment'; +import {inject} from "@angular/core"; +import {CookieService} from "./core/services/cookie.service"; export function createApolloClientOptions(): ApolloClientOptions { + const cookieService = inject(CookieService); + const httpLink = new HttpLink({ uri: environment.auth_service, credentials: 'include', }); const authLink = setContext(() => { - const token = document.cookie - .split('; ') - .find(row => row.startsWith('token=')) - ?.split('=')[1]; + const token = cookieService.get("token"); return { headers: { Authorization: token ? `Bearer ${token}` : '', diff --git a/frontend/src/app/core/services/cookie.service.ts b/frontend/src/app/core/services/cookie.service.ts new file mode 100644 index 0000000..3a112b3 --- /dev/null +++ b/frontend/src/app/core/services/cookie.service.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class CookieService { + + constructor() { } + + get(key: string): string { + let value = document.cookie + .split('; ') + .find(row => row.startsWith(`${key}=`)) + ?.split('=')[1]; + + return value ?? `There is no cookie with key ${key}.`; + } +} diff --git a/frontend/src/app/features/auth/services/auth.service.ts b/frontend/src/app/features/auth/services/auth.service.ts index 13a384e..103a9ca 100644 --- a/frontend/src/app/features/auth/services/auth.service.ts +++ b/frontend/src/app/features/auth/services/auth.service.ts @@ -1,18 +1,29 @@ -import {Injectable} from '@angular/core'; +import {inject, Injectable} from '@angular/core'; import {Apollo} from "apollo-angular"; -import {map, Observable} from "rxjs"; +import {BehaviorSubject, map, Observable} from "rxjs"; import {LOGIN, REGISTER} from "../requests/mutations"; import {MutationResponse} from "../responses/mutation.response"; import {QueryResponses} from "../responses/query.responses"; import {IS_AUTHENTICATED} from "../requests/queries"; +import {CookieService} from "../../../core/services/cookie.service"; @Injectable({ providedIn: 'root' }) export class AuthService { + private isAuthenticatedSubject = new BehaviorSubject(this.checkAuth()); + public isAuthenticated$ = this.isAuthenticatedSubject.asObservable(); constructor(private apollo: Apollo) { } + private checkAuth(): boolean { + let cookieService = inject(CookieService); + + const token = cookieService.get("token"); + + return token != "There is no cookie with key token."; + } + public login(username: string, password: string): Observable { return this.apollo.mutate({ mutation: LOGIN, @@ -22,8 +33,10 @@ export class AuthService { const token = response.data?.login; if (token) { + this.isAuthenticatedSubject.next(true); return true; } else { + this.isAuthenticatedSubject.next(false); console.error("Login failed: no token received."); return false; } @@ -57,9 +70,10 @@ export class AuthService { public isAuthenticated(): Observable { - return this.apollo.query({ - query: IS_AUTHENTICATED - }).pipe( - map(response => response.data.isAuthenticated)); + // return this.apollo.query({ + // query: IS_AUTHENTICATED + // }).pipe( + // map(response => response.data.isAuthenticated)); + return this.isAuthenticated$; } }