Spaces:
Building
Building
test
Browse files- app/api/auth/logout/route.ts +6 -7
- app/api/auth/route.ts +0 -12
- app/layout.tsx +4 -11
- assets/globals.css +1 -1
- components/editor/ask-ai/settings.tsx +1 -1
- hooks/useUser.ts +15 -9
- lib/api.ts +2 -0
app/api/auth/logout/route.ts
CHANGED
|
@@ -1,23 +1,22 @@
|
|
| 1 |
-
import {
|
| 2 |
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 3 |
|
| 4 |
-
export async function POST(
|
| 5 |
const cookieName = MY_TOKEN_KEY();
|
| 6 |
-
const
|
| 7 |
-
const isSecure = !host.includes("localhost");
|
| 8 |
|
| 9 |
const response = NextResponse.json(
|
| 10 |
{ message: "Logged out successfully" },
|
| 11 |
{ status: 200 }
|
| 12 |
);
|
| 13 |
|
| 14 |
-
// Clear the cookie
|
| 15 |
const cookieOptions = [
|
| 16 |
`${cookieName}=`,
|
| 17 |
"Max-Age=0",
|
| 18 |
"Path=/",
|
| 19 |
-
"
|
| 20 |
-
...(
|
| 21 |
].join("; ");
|
| 22 |
|
| 23 |
response.headers.set("Set-Cookie", cookieOptions);
|
|
|
|
| 1 |
+
import { NextResponse } from "next/server";
|
| 2 |
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 3 |
|
| 4 |
+
export async function POST() {
|
| 5 |
const cookieName = MY_TOKEN_KEY();
|
| 6 |
+
const isProduction = process.env.NODE_ENV === "production";
|
|
|
|
| 7 |
|
| 8 |
const response = NextResponse.json(
|
| 9 |
{ message: "Logged out successfully" },
|
| 10 |
{ status: 200 }
|
| 11 |
);
|
| 12 |
|
| 13 |
+
// Clear the HTTP-only cookie
|
| 14 |
const cookieOptions = [
|
| 15 |
`${cookieName}=`,
|
| 16 |
"Max-Age=0",
|
| 17 |
"Path=/",
|
| 18 |
+
"HttpOnly",
|
| 19 |
+
...(isProduction ? ["Secure", "SameSite=None"] : ["SameSite=Lax"])
|
| 20 |
].join("; ");
|
| 21 |
|
| 22 |
response.headers.set("Set-Cookie", cookieOptions);
|
app/api/auth/route.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
-
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 3 |
|
| 4 |
export async function POST(req: NextRequest) {
|
| 5 |
const body = await req.json();
|
|
@@ -71,16 +70,6 @@ export async function POST(req: NextRequest) {
|
|
| 71 |
}
|
| 72 |
const user = await userResponse.json();
|
| 73 |
|
| 74 |
-
// Calculate cookie expiration
|
| 75 |
-
const expiresIn = response.expires_in || 3600;
|
| 76 |
-
const maxAge = expiresIn;
|
| 77 |
-
|
| 78 |
-
// Determine if we should use secure cookies
|
| 79 |
-
const isSecure = !host.includes("localhost");
|
| 80 |
-
|
| 81 |
-
// Set the cookie via Set-Cookie header
|
| 82 |
-
const cookieValue = `${MY_TOKEN_KEY()}=${response.access_token}; Path=/; Max-Age=${maxAge}; SameSite=Lax${isSecure ? "; Secure" : ""}`;
|
| 83 |
-
|
| 84 |
return NextResponse.json(
|
| 85 |
{
|
| 86 |
access_token: response.access_token,
|
|
@@ -91,7 +80,6 @@ export async function POST(req: NextRequest) {
|
|
| 91 |
status: 200,
|
| 92 |
headers: {
|
| 93 |
"Content-Type": "application/json",
|
| 94 |
-
"Set-Cookie": cookieValue,
|
| 95 |
},
|
| 96 |
}
|
| 97 |
);
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
| 2 |
|
| 3 |
export async function POST(req: NextRequest) {
|
| 4 |
const body = await req.json();
|
|
|
|
| 70 |
}
|
| 71 |
const user = await userResponse.json();
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return NextResponse.json(
|
| 74 |
{
|
| 75 |
access_token: response.access_token,
|
|
|
|
| 80 |
status: 200,
|
| 81 |
headers: {
|
| 82 |
"Content-Type": "application/json",
|
|
|
|
| 83 |
},
|
| 84 |
}
|
| 85 |
);
|
app/layout.tsx
CHANGED
|
@@ -56,16 +56,8 @@ export const viewport: Viewport = {
|
|
| 56 |
|
| 57 |
async function getMe() {
|
| 58 |
const cookieStore = await cookies();
|
| 59 |
-
const
|
| 60 |
-
const token = cookieStore.get(
|
| 61 |
-
|
| 62 |
-
// Debug logging
|
| 63 |
-
console.log("π [DEBUG] Cookie Key:", tokenKey);
|
| 64 |
-
console.log("π [DEBUG] Token found:", !!token);
|
| 65 |
-
console.log(
|
| 66 |
-
"π [DEBUG] All cookies:",
|
| 67 |
-
cookieStore.getAll().map((c) => c.name)
|
| 68 |
-
);
|
| 69 |
|
| 70 |
if (!token) return { user: null, projects: [], errCode: null };
|
| 71 |
try {
|
|
@@ -74,9 +66,10 @@ async function getMe() {
|
|
| 74 |
Authorization: `Bearer ${token}`,
|
| 75 |
},
|
| 76 |
});
|
|
|
|
| 77 |
return { user: res.data.user, projects: res.data.projects, errCode: null };
|
| 78 |
} catch (err: any) {
|
| 79 |
-
console.error("
|
| 80 |
return { user: null, projects: [], errCode: err.status };
|
| 81 |
}
|
| 82 |
}
|
|
|
|
| 56 |
|
| 57 |
async function getMe() {
|
| 58 |
const cookieStore = await cookies();
|
| 59 |
+
const cookieName = MY_TOKEN_KEY();
|
| 60 |
+
const token = cookieStore.get(cookieName)?.value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if (!token) return { user: null, projects: [], errCode: null };
|
| 63 |
try {
|
|
|
|
| 66 |
Authorization: `Bearer ${token}`,
|
| 67 |
},
|
| 68 |
});
|
| 69 |
+
console.log("β
[Server] User authenticated:", res.data.user?.name);
|
| 70 |
return { user: res.data.user, projects: res.data.projects, errCode: null };
|
| 71 |
} catch (err: any) {
|
| 72 |
+
console.error("β [Server] Auth error:", err.status, err.message);
|
| 73 |
return { user: null, projects: [], errCode: err.status };
|
| 74 |
}
|
| 75 |
}
|
assets/globals.css
CHANGED
|
@@ -131,7 +131,7 @@ body {
|
|
| 131 |
.background__noisy {
|
| 132 |
@apply bg-blend-normal pointer-events-none opacity-90;
|
| 133 |
background-size: 25ww auto;
|
| 134 |
-
background-image: url("/background_noisy.webp");
|
| 135 |
@apply fixed w-screen h-screen -z-1 top-0 left-0;
|
| 136 |
}
|
| 137 |
|
|
|
|
| 131 |
.background__noisy {
|
| 132 |
@apply bg-blend-normal pointer-events-none opacity-90;
|
| 133 |
background-size: 25ww auto;
|
| 134 |
+
background-image: url("/deepsite/background_noisy.webp");
|
| 135 |
@apply fixed w-screen h-screen -z-1 top-0 left-0;
|
| 136 |
}
|
| 137 |
|
components/editor/ask-ai/settings.tsx
CHANGED
|
@@ -264,7 +264,7 @@ export function Settings({
|
|
| 264 |
<BrainIcon className="size-4 mr-2" />
|
| 265 |
) : (
|
| 266 |
<Image
|
| 267 |
-
src={`/providers/${id}.svg`}
|
| 268 |
alt={id}
|
| 269 |
className="size-5 mr-2"
|
| 270 |
width={20}
|
|
|
|
| 264 |
<BrainIcon className="size-4 mr-2" />
|
| 265 |
) : (
|
| 266 |
<Image
|
| 267 |
+
src={`/deepsite/providers/${id}.svg`}
|
| 268 |
alt={id}
|
| 269 |
className="size-5 mr-2"
|
| 270 |
width={20}
|
hooks/useUser.ts
CHANGED
|
@@ -75,12 +75,20 @@ export const useUser = (initialData?: {
|
|
| 75 |
const expiresDate = new Date();
|
| 76 |
expiresDate.setTime(expiresDate.getTime() + expiresIn * 1000);
|
| 77 |
|
| 78 |
-
|
| 79 |
expires: expiresDate,
|
| 80 |
path: '/',
|
| 81 |
sameSite: 'lax',
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
const meResponse = await api.get("/me");
|
| 86 |
if (meResponse.data) {
|
|
@@ -93,12 +101,10 @@ export const useUser = (initialData?: {
|
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
router.push("/");
|
| 101 |
-
// }
|
| 102 |
toast.success("Login successful");
|
| 103 |
}
|
| 104 |
})
|
|
|
|
| 75 |
const expiresDate = new Date();
|
| 76 |
expiresDate.setTime(expiresDate.getTime() + expiresIn * 1000);
|
| 77 |
|
| 78 |
+
const cookieOptions: any = {
|
| 79 |
expires: expiresDate,
|
| 80 |
path: '/',
|
| 81 |
sameSite: 'lax',
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
if (window.location.protocol === 'https:') {
|
| 85 |
+
cookieOptions.secure = true;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
setToken(res.data.access_token, cookieOptions);
|
| 89 |
+
|
| 90 |
+
const cookieString = `${MY_TOKEN_KEY()}=${res.data.access_token}; path=/; max-age=${expiresIn}; samesite=lax${cookieOptions.secure ? '; secure' : ''}`;
|
| 91 |
+
document.cookie = cookieString;
|
| 92 |
|
| 93 |
const meResponse = await api.get("/me");
|
| 94 |
if (meResponse.data) {
|
|
|
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
+
setTimeout(() => {
|
| 105 |
+
window.location.href = "/";
|
| 106 |
+
}, 100);
|
| 107 |
+
|
|
|
|
|
|
|
| 108 |
toast.success("Login successful");
|
| 109 |
}
|
| 110 |
})
|
lib/api.ts
CHANGED
|
@@ -6,6 +6,7 @@ export const api = axios.create({
|
|
| 6 |
headers: {
|
| 7 |
cache: "no-store",
|
| 8 |
},
|
|
|
|
| 9 |
});
|
| 10 |
|
| 11 |
export const apiServer = axios.create({
|
|
@@ -13,6 +14,7 @@ export const apiServer = axios.create({
|
|
| 13 |
headers: {
|
| 14 |
cache: "no-store",
|
| 15 |
},
|
|
|
|
| 16 |
});
|
| 17 |
api.interceptors.request.use(
|
| 18 |
async (config) => {
|
|
|
|
| 6 |
headers: {
|
| 7 |
cache: "no-store",
|
| 8 |
},
|
| 9 |
+
withCredentials: true, // Ensure cookies are sent with requests
|
| 10 |
});
|
| 11 |
|
| 12 |
export const apiServer = axios.create({
|
|
|
|
| 14 |
headers: {
|
| 15 |
cache: "no-store",
|
| 16 |
},
|
| 17 |
+
withCredentials: true, // Ensure cookies are sent with requests
|
| 18 |
});
|
| 19 |
api.interceptors.request.use(
|
| 20 |
async (config) => {
|