From 4dfc638177148600135c2d6575b08ef208e88c1e Mon Sep 17 00:00:00 2001 From: Ivan Sentemon Date: Tue, 7 Jan 2025 19:31:14 +0100 Subject: [PATCH] Refactor cookie management and remove ngx-cookie-service dependency --- .../AuthService.Api/GraphQL/Mutation.cs | 14 ++++----- frontend/package-lock.json | 14 --------- frontend/package.json | 1 - frontend/src/app/apollo.config.ts | 29 +++++++++++++++++++ frontend/src/app/app.config.ts | 28 ++---------------- .../src/app/core/services/token.service.ts | 23 --------------- .../features/auth/services/auth.service.ts | 12 +------- 7 files changed, 39 insertions(+), 82 deletions(-) create mode 100644 frontend/src/app/apollo.config.ts delete mode 100644 frontend/src/app/core/services/token.service.ts diff --git a/backend/src/AuthService/AuthService.Api/GraphQL/Mutation.cs b/backend/src/AuthService/AuthService.Api/GraphQL/Mutation.cs index 2ac1f74..74e65df 100644 --- a/backend/src/AuthService/AuthService.Api/GraphQL/Mutation.cs +++ b/backend/src/AuthService/AuthService.Api/GraphQL/Mutation.cs @@ -32,10 +32,10 @@ public async Task Register(RegisterDto input, [Service] R _httpContextAccessor.HttpContext?.Response.Cookies.Append("token", result.Response.AccessToken, new CookieOptions { - HttpOnly = true, - Secure = true, + Path = "/", + HttpOnly = false, + Secure = false, SameSite = SameSiteMode.Strict, - Expires = DateTimeOffset.FromUnixTimeSeconds(result.Response.ExpiresIn) }); return result.Response; @@ -53,10 +53,10 @@ public async Task Login(LoginDto input, [Service] LoginCo _httpContextAccessor.HttpContext?.Response.Cookies.Append("token", result.Response.AccessToken, new CookieOptions { - HttpOnly = true, - Secure = true, - SameSite = SameSiteMode.Strict, - Expires = DateTimeOffset.FromUnixTimeSeconds(result.Response.ExpiresIn) + Path = "/", + HttpOnly = false, + Secure = false, + SameSite = SameSiteMode.Strict }); return result.Response; diff --git a/frontend/package-lock.json b/frontend/package-lock.json index b276639..4e05609 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -19,7 +19,6 @@ "@apollo/client": "^3.11.10", "apollo-angular": "^8.0.0", "graphql": "^16.9.0", - "ngx-cookie-service": "^18.0.0", "rxjs": "~7.8.0", "tslib": "^2.3.0", "zone.js": "~0.14.10" @@ -10033,19 +10032,6 @@ "dev": true, "license": "MIT" }, - "node_modules/ngx-cookie-service": { - "version": "18.0.0", - "resolved": "https://registry.npmjs.org/ngx-cookie-service/-/ngx-cookie-service-18.0.0.tgz", - "integrity": "sha512-hkkUckzZTXXWtFgvVkT2hg6mwYMLXioXDZWBsVCOy9gYkADjsj0N5VViO7eo2izQ0VcMPd/Etog1trf/T4oZMQ==", - "license": "MIT", - "dependencies": { - "tslib": "^2.6.2" - }, - "peerDependencies": { - "@angular/common": "^18.0.0-rc.0", - "@angular/core": "^18.0.0-rc.0" - } - }, "node_modules/nice-napi": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/nice-napi/-/nice-napi-1.0.2.tgz", diff --git a/frontend/package.json b/frontend/package.json index 9f7efa2..acd2e95 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -21,7 +21,6 @@ "@apollo/client": "^3.11.10", "apollo-angular": "^8.0.0", "graphql": "^16.9.0", - "ngx-cookie-service": "^18.0.0", "rxjs": "~7.8.0", "tslib": "^2.3.0", "zone.js": "~0.14.10" diff --git a/frontend/src/app/apollo.config.ts b/frontend/src/app/apollo.config.ts new file mode 100644 index 0000000..64eb97d --- /dev/null +++ b/frontend/src/app/apollo.config.ts @@ -0,0 +1,29 @@ +import { ApolloClientOptions, InMemoryCache, ApolloLink, HttpLink } from '@apollo/client/core'; +import { setContext } from '@apollo/client/link/context'; +import { environment } from '../environments/environment'; + +export function createApolloClientOptions(): ApolloClientOptions { + 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]; + return { + headers: { + Authorization: token ? `Bearer ${token}` : '', + } + }; + }); + + const link = ApolloLink.from([authLink, httpLink]); + + return { + link: link, + cache: new InMemoryCache() + }; +} diff --git a/frontend/src/app/app.config.ts b/frontend/src/app/app.config.ts index 0f0784a..480a3b7 100644 --- a/frontend/src/app/app.config.ts +++ b/frontend/src/app/app.config.ts @@ -4,11 +4,9 @@ import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; import {provideHttpClient, withFetch} from '@angular/common/http'; import { provideApollo } from 'apollo-angular'; -import { ApolloLink, InMemoryCache} from '@apollo/client/core'; -import {setContext} from "@apollo/client/link/context"; import {environment} from "../environments/environment"; -import {HttpLink} from "apollo-angular/http"; import {loadDevMessages, loadErrorMessages} from "@apollo/client/dev"; +import { createApolloClientOptions } from "./apollo.config"; export const appConfig: ApplicationConfig = { providers: [ @@ -20,30 +18,8 @@ export const appConfig: ApplicationConfig = { loadDevMessages(); loadErrorMessages(); } - const httpLink = inject(HttpLink); - const authLink = setContext(() => { - const token = document.cookie - .split('; ') - .find(row => row.startsWith('token=')) - ?.split('=')[1]; - return { - - headers: { - Authorization: token ? `Bearer ${token}` : '', - } - }; - }); - - const link = ApolloLink.from([ - authLink, - httpLink.create({ uri: environment.auth_service }), - ]); - - return { - link: link, - cache: new InMemoryCache(), - }; + return createApolloClientOptions(); }) ] }; diff --git a/frontend/src/app/core/services/token.service.ts b/frontend/src/app/core/services/token.service.ts deleted file mode 100644 index 99338bd..0000000 --- a/frontend/src/app/core/services/token.service.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Injectable } from '@angular/core'; -import {CookieService} from "ngx-cookie-service"; -import {Token} from "../models/token.model"; - -@Injectable({ - providedIn: 'root' -}) -export class TokenService { - - constructor(private cookieService: CookieService) { } - - get(): any { - this.cookieService.get("token"); - } - - set(token: Token): void { - this.cookieService.set("token", token.accessToken, token.expiresIn); - } - - delete(): void { - this.cookieService.delete("token"); - } -} diff --git a/frontend/src/app/features/auth/services/auth.service.ts b/frontend/src/app/features/auth/services/auth.service.ts index 9d176a0..13a384e 100644 --- a/frontend/src/app/features/auth/services/auth.service.ts +++ b/frontend/src/app/features/auth/services/auth.service.ts @@ -2,24 +2,16 @@ import {Injectable} from '@angular/core'; import {Apollo} from "apollo-angular"; import {map, Observable} from "rxjs"; import {LOGIN, REGISTER} from "../requests/mutations"; -import {TokenService} from "../../../core/services/token.service"; import {MutationResponse} from "../responses/mutation.response"; import {QueryResponses} from "../responses/query.responses"; import {IS_AUTHENTICATED} from "../requests/queries"; -import {environment} from "../../../../environments/environment"; -import {InMemoryCache} from "@apollo/client/core"; @Injectable({ providedIn: 'root' }) export class AuthService { - constructor(private apollo: Apollo, private tokenService: TokenService) { - // apollo.create({ - // uri: environment.auth_service, - // cache: new InMemoryCache() - // }); - } + constructor(private apollo: Apollo) { } public login(username: string, password: string): Observable { return this.apollo.mutate({ @@ -30,7 +22,6 @@ export class AuthService { const token = response.data?.login; if (token) { - this.tokenService.set(token); return true; } else { console.error("Login failed: no token received."); @@ -55,7 +46,6 @@ export class AuthService { const token = response.data?.register; if (token) { - this.tokenService.set(token); return true; } else { console.error("Registration failed: no token received.");