forked from NilsDelage/countdown-page
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c9411e
commit 9f77d5e
Showing
1 changed file
with
14 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" }); | ||
} | ||
} |