diff --git a/.gitignore b/.gitignore index 3f815ea23..bfdd87b0a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ node_modules # misc -.DS_Store \ No newline at end of file +.DS_Store + +# env +.env* \ No newline at end of file diff --git a/apps/cs/src/entities/user/api/user-service.ts b/apps/cs/src/entities/user/api/user-service.ts index a7209969f..5d0f57eed 100644 --- a/apps/cs/src/entities/user/api/user-service.ts +++ b/apps/cs/src/entities/user/api/user-service.ts @@ -1,18 +1,11 @@ import { client } from "@/shared/api"; -import { getToken } from "@/shared/lib/cookie"; -import { AUTH_USER_ID, CLAIM_NAME, ENDPOINT } from "@/shared/config"; -import type { IUser, IUserInfo, UserService } from "../model/user"; +import { ENDPOINT } from "@/shared/config"; +import type { IUser, IUserInfo, UserService } from "../model/user-interface"; export class UsersApiService implements UserService { public async userInfo(payload: IUserInfo): Promise { const response = await client.get( `${ENDPOINT.USER_SERVICE.USERS}/${payload.userUniqId}`, - { - headers: { - [CLAIM_NAME]: `${getToken(CLAIM_NAME)}`, - [AUTH_USER_ID]: `${getToken(AUTH_USER_ID)}`, - }, - }, ); return response.data; diff --git a/apps/cs/src/entities/user/model/user.ts b/apps/cs/src/entities/user/model/user-interface.ts similarity index 100% rename from apps/cs/src/entities/user/model/user.ts rename to apps/cs/src/entities/user/model/user-interface.ts diff --git a/packages/common/network/client.ts b/packages/common/network/client.ts index 5211c793f..c61ce01e9 100644 --- a/packages/common/network/client.ts +++ b/packages/common/network/client.ts @@ -1,5 +1,7 @@ import axios from "axios"; import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"; +import Cookies from "js-cookie"; +import { AUTH_USER_ID, CLAIM_NAME } from "./env"; interface ClientConfig { baseURL: string; @@ -19,6 +21,20 @@ class Client { }, withCredentials: true, }); + + this.axiosInstance.interceptors.request.use((config) => { + const token = Cookies.get(CLAIM_NAME); + const userId = Cookies.get(AUTH_USER_ID); + + if (!token || !userId) { + throw new Error("Missing required authentication tokens"); + } + + config.headers[CLAIM_NAME] = token; + config.headers[AUTH_USER_ID] = userId; + + return config; + }); } public static getInstance(config: ClientConfig): Client { diff --git a/packages/common/network/env.ts b/packages/common/network/env.ts new file mode 100644 index 000000000..0a719e9d4 --- /dev/null +++ b/packages/common/network/env.ts @@ -0,0 +1,4 @@ +export const CLAIM_NAME = process.env.CLAIM_NAME || "Authorization"; +export const ACCESS_TOKEN = process.env.ACCESS_TOKEN || "access-token"; +export const REFRESH_TOKEN = process.env.REFRESH_TOKEN || "refresh-token"; +export const AUTH_USER_ID = process.env.AUTH_USER_ID || "token-id"; diff --git a/packages/common/network/package.json b/packages/common/network/package.json index 9a479b6bd..23659bac7 100644 --- a/packages/common/network/package.json +++ b/packages/common/network/package.json @@ -8,7 +8,9 @@ ".": "./index.ts" }, "dependencies": { - "axios": "^1.8.1" + "@types/js-cookie": "^3.0.6", + "axios": "^1.8.1", + "js-cookie": "^3.0.5" }, "devDependencies": { "@types/node": "^20.11.19", diff --git a/packages/common/network/tsconfig.json b/packages/common/network/tsconfig.json index 8d11d8253..7f4dcf889 100644 --- a/packages/common/network/tsconfig.json +++ b/packages/common/network/tsconfig.json @@ -1,6 +1,9 @@ { + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../../../tsconfig.base.json", "compilerOptions": { - "baseUrl": "." + "baseUrl": ".", + "module": "ESNext" }, "include": ["."], "exclude": ["node_modules", "dist"] diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bfb638ec5..e1c113792 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -166,6 +166,9 @@ importers: '@tanstack/react-table': specifier: ^8.21.2 version: 8.21.2(react-dom@18.3.1)(react@18.3.1) + '@types/js-cookie': + specifier: ^3.0.6 + version: 3.0.6 axios: specifier: ^1.8.1 version: 1.8.1 @@ -181,6 +184,9 @@ importers: date-fns: specifier: ^3.6.0 version: 3.6.0 + js-cookie: + specifier: ^3.0.5 + version: 3.0.5 lucide-react: specifier: ^0.474.0 version: 0.474.0(react@18.3.1) @@ -296,9 +302,15 @@ importers: packages/common/network: dependencies: + '@types/js-cookie': + specifier: ^3.0.6 + version: 3.0.6 axios: specifier: ^1.8.1 version: 1.8.1 + js-cookie: + specifier: ^3.0.5 + version: 3.0.5 devDependencies: '@types/node': specifier: ^20.11.19 @@ -2952,7 +2964,6 @@ packages: /@types/js-cookie@3.0.6: resolution: {integrity: sha512-wkw9yd1kEXOPnvEeEV1Go1MmxtBJL0RR79aOTAApecWFVu7w0NNXNqhcWgvw2YgZDYadliXkl14pa3WXw5jlCQ==} - dev: true /@types/json-schema@7.0.15: resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==}