From 9f77d5e7409001fa553ec8db78bedf703ff32127 Mon Sep 17 00:00:00 2001 From: KamyarTaher <61198701+KamyarTaher@users.noreply.github.com> Date: Sat, 9 Nov 2024 01:18:53 +0100 Subject: [PATCH] p --- src/pages/api/set-cookie.ts | 64 ++++++++----------------------------- 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/src/pages/api/set-cookie.ts b/src/pages/api/set-cookie.ts index 25f9c3f..5fd0c0e 100644 --- a/src/pages/api/set-cookie.ts +++ b/src/pages/api/set-cookie.ts @@ -1,78 +1,42 @@ +// pages/api/set-cookie.ts import type { NextApiRequest, NextApiResponse } from "next"; import axios from "axios"; -import cookie from "cookie"; const BASE_URL = "https://api.metacube.games:8080/"; -const createApi = () => { - return axios.create({ - baseURL: BASE_URL, - }); -}; -let api = createApi(); export default async function handler( req: NextApiRequest, res: NextApiResponse ) { - // Allow credentials and specific origin for cookies to be set - res.setHeader("Access-Control-Allow-Origin", "https://play.metacube.games"); - res.setHeader("Access-Control-Allow-Credentials", "true"); - - if (req?.method === "OPTIONS") { - // Handle CORS preflight request - res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); - res.setHeader("Access-Control-Allow-Headers", "Content-Type"); - res.status(200).end(); - return; - } - try { - const reconnect = req?.query?.reconnect || "false"; + // Define your parameters (assuming `reconnect` is part of req.query) + const reconnect = req?.query?.reconnect || "false"; // Default to 'false' if not provided - // Attempt to fetch the token from backend - const backendResponse = await api.get("auth/refresh", { - params: { reconnect: reconnect?.toString() }, - withCredentials: true, + // Make a request to the backend to refresh the token + const backendResponse = await axios.get(`${BASE_URL}auth/refresh`, { + params: { reconnect: reconnect.toString() }, + withCredentials: true, // Ensures cookies are included in the request if required }); - // Check if the backend response status is successful - if (backendResponse?.status !== 200) { - console.error("Error: Non-200 response from backend:", backendResponse); + // Check if the response is successful + if (backendResponse.status !== 200) { return res .status(backendResponse.status) .json({ error: "Failed to fetch token" }); } - // Extract token and set it as a cookie - const token = backendResponse?.data?.accessToken; - if (!token) { - console.error( - "Error: Token missing in backend response:", - backendResponse - ); - return res.status(500).json({ error: "Token not found in response" }); - } + // Extract the token or cookies from the backend response + const token = backendResponse.data.token; + // Set the token as a cookie in the response res.setHeader( "Set-Cookie", - cookie.serialize("userToken", token, { - httpOnly: true, - secure: true, // Use secure only in production - sameSite: "none", // Cross-site cookie setting - path: "/", - }) + `refreshToken=${token}; Path=/; HttpOnly; Secure; SameSite=None; Max-Age=86400;` ); res.status(200).json({ message: "Token cookie set successfully" }); } catch (error) { - if (axios.isAxiosError(error)) { - console.error( - "Axios error response:", - error.response?.data || error.message - ); - } else { - console.error("Unexpected error:", error); - } + console.error("Error fetching token from backend:", error); res.status(500).json({ error: "Internal server error" }); } }