From 2976475ddd72869329666525ec9c4895912dc476 Mon Sep 17 00:00:00 2001 From: "dev.daminik00" Date: Sat, 1 Nov 2025 01:21:28 +0100 Subject: [PATCH] fix redirect --- src/app/[locale]/auth/callback/route.ts | 30 -------------------- src/providers/analytics-provider.tsx | 18 ++++++++++-- src/services/analytics/analyticsService.ts | 9 ++---- src/shared/api/httpClient.ts | 32 +++++++++++----------- 4 files changed, 35 insertions(+), 54 deletions(-) diff --git a/src/app/[locale]/auth/callback/route.ts b/src/app/[locale]/auth/callback/route.ts index f9ed64b..f01bc56 100644 --- a/src/app/[locale]/auth/callback/route.ts +++ b/src/app/[locale]/auth/callback/route.ts @@ -1,33 +1,3 @@ -//import { NextRequest, NextResponse } from "next/server"; - -//import { ROUTES } from "@/shared/constants/client-routes"; - -//export async function GET(req: NextRequest) { -// const { searchParams } = req.nextUrl; -// const token = searchParams.get("jwtToken"); -// const productId = searchParams.get("productId"); -// const placementId = searchParams.get("placementId"); -// const paywallId = searchParams.get("paywallId"); - -// const redirectUrl = new URL(`${ROUTES.payment()}`, process.env.NEXT_PUBLIC_APP_URL || ""); -// if (productId) redirectUrl.searchParams.set("productId", productId); -// if (placementId) redirectUrl.searchParams.set("placementId", placementId); -// if (paywallId) redirectUrl.searchParams.set("paywallId", paywallId); - -// const res = NextResponse.redirect(redirectUrl); - -// res.cookies.set({ -// name: "accessToken", -// value: token || "", -// httpOnly: true, -// secure: true, -// sameSite: "lax", -// path: "/", -// maxAge: 60 * 60 * 24 * 365, -// }); -// return res; -//} - import { NextRequest, NextResponse } from "next/server"; import { ROUTES } from "@/shared/constants/client-routes"; diff --git a/src/providers/analytics-provider.tsx b/src/providers/analytics-provider.tsx index 30ec304..7e39000 100644 --- a/src/providers/analytics-provider.tsx +++ b/src/providers/analytics-provider.tsx @@ -2,6 +2,7 @@ import { type ReactNode,useEffect } from "react"; +import { fetchMe } from "@/entities/user/actions"; import { analyticsService } from "@/services/analytics"; interface AnalyticsProviderProps { @@ -16,8 +17,21 @@ interface AnalyticsProviderProps { */ export function AnalyticsProvider({ children }: AnalyticsProviderProps) { useEffect(() => { - // Initialize analytics service - analyticsService.initialize(); + // Initialize analytics service with server-side data + const initAnalytics = async () => { + try { + const response = await fetchMe(); + if (response.data && !response.error) { + await analyticsService.initialize(response.data.user); + } else { + await analyticsService.initialize(null); + } + } catch { + await analyticsService.initialize(null); + } + }; + + initAnalytics(); }, []); return <>{children}; diff --git a/src/services/analytics/analyticsService.ts b/src/services/analytics/analyticsService.ts index ab2ce00..7fdfcf0 100644 --- a/src/services/analytics/analyticsService.ts +++ b/src/services/analytics/analyticsService.ts @@ -1,6 +1,4 @@ -import type { IMeResponse, IUser } from "@/entities/user/types"; -import { http } from "@/shared/api/httpClient"; -import { API_ROUTES } from "@/shared/constants/api-routes"; +import type { IUser } from "@/entities/user/types"; /** * Analytics Service @@ -23,14 +21,13 @@ export class AnalyticsService { /** * Initialize analytics service with user data */ - async initialize(): Promise { + async initialize(userData: IUser | null): Promise { if (this.isInitialized) { return; } try { - const response = await http.get(API_ROUTES.usersMe()); - this.user = response.user; + this.user = userData; // Set user parameters in analytics await this.setUserParameters(); diff --git a/src/shared/api/httpClient.ts b/src/shared/api/httpClient.ts index 38dc0fd..0cb1b89 100644 --- a/src/shared/api/httpClient.ts +++ b/src/shared/api/httpClient.ts @@ -58,6 +58,22 @@ class HttpClient { const fullUrl = this.buildUrl(rootUrl, path, query); const startTime = Date.now(); + const headers = new Headers(); + let accessToken: string | undefined; + if (typeof window === "undefined") { + const { getServerAccessToken } = await import("../auth/token"); + accessToken = await getServerAccessToken(); + } else { + try { + const { getClientAccessToken } = await import("../auth/token"); + accessToken = getClientAccessToken(); + } catch { + // ignore + } + } + if (accessToken) headers.set("Authorization", `Bearer ${accessToken}`); + headers.set("Content-Type", "application/json"); + // Log API request (both client and server with ENV control) if (typeof window !== "undefined") { // Client-side logging @@ -78,22 +94,6 @@ class HttpClient { } } - const headers = new Headers(); - let accessToken: string | undefined; - if (typeof window === "undefined") { - const { getServerAccessToken } = await import("../auth/token"); - accessToken = await getServerAccessToken(); - } else { - try { - const { getClientAccessToken } = await import("../auth/token"); - accessToken = getClientAccessToken(); - } catch { - // ignore - } - } - if (accessToken) headers.set("Authorization", `Bearer ${accessToken}`); - headers.set("Content-Type", "application/json"); - const res = await fetch(fullUrl, { method, body: body ? JSON.stringify(body) : undefined,