Spaces:
Running
Running
another try
Browse files- app/api/auth/logout/route.ts +7 -6
- app/api/auth/route.ts +12 -0
- app/layout.tsx +12 -2
app/api/auth/logout/route.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
| 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
|
|
|
|
| 7 |
|
| 8 |
const response = NextResponse.json(
|
| 9 |
{ message: "Logged out successfully" },
|
| 10 |
{ status: 200 }
|
| 11 |
);
|
| 12 |
|
| 13 |
-
// Clear the
|
| 14 |
const cookieOptions = [
|
| 15 |
`${cookieName}=`,
|
| 16 |
"Max-Age=0",
|
| 17 |
"Path=/",
|
| 18 |
-
"
|
| 19 |
-
...(
|
| 20 |
].join("; ");
|
| 21 |
|
| 22 |
response.headers.set("Set-Cookie", cookieOptions);
|
|
|
|
| 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 cookieName = MY_TOKEN_KEY();
|
| 6 |
+
const host = req.headers.get("host") ?? "localhost:3000";
|
| 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 (matching the same settings as login)
|
| 15 |
const cookieOptions = [
|
| 16 |
`${cookieName}=`,
|
| 17 |
"Max-Age=0",
|
| 18 |
"Path=/",
|
| 19 |
+
"SameSite=Lax",
|
| 20 |
+
...(isSecure ? ["Secure"] : [])
|
| 21 |
].join("; ");
|
| 22 |
|
| 23 |
response.headers.set("Set-Cookie", cookieOptions);
|
app/api/auth/route.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
| 2 |
|
| 3 |
export async function POST(req: NextRequest) {
|
| 4 |
const body = await req.json();
|
|
@@ -70,6 +71,16 @@ export async function POST(req: NextRequest) {
|
|
| 70 |
}
|
| 71 |
const user = await userResponse.json();
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return NextResponse.json(
|
| 74 |
{
|
| 75 |
access_token: response.access_token,
|
|
@@ -80,6 +91,7 @@ export async function POST(req: NextRequest) {
|
|
| 80 |
status: 200,
|
| 81 |
headers: {
|
| 82 |
"Content-Type": "application/json",
|
|
|
|
| 83 |
},
|
| 84 |
}
|
| 85 |
);
|
|
|
|
| 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 |
}
|
| 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 |
status: 200,
|
| 92 |
headers: {
|
| 93 |
"Content-Type": "application/json",
|
| 94 |
+
"Set-Cookie": cookieValue,
|
| 95 |
},
|
| 96 |
}
|
| 97 |
);
|
app/layout.tsx
CHANGED
|
@@ -56,8 +56,17 @@ export const viewport: Viewport = {
|
|
| 56 |
|
| 57 |
async function getMe() {
|
| 58 |
const cookieStore = await cookies();
|
| 59 |
-
const
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if (!token) return { user: null, projects: [], errCode: null };
|
| 62 |
try {
|
| 63 |
const res = await apiServer.get("/me", {
|
|
@@ -67,6 +76,7 @@ async function getMe() {
|
|
| 67 |
});
|
| 68 |
return { user: res.data.user, projects: res.data.projects, errCode: null };
|
| 69 |
} catch (err: any) {
|
|
|
|
| 70 |
return { user: null, projects: [], errCode: err.status };
|
| 71 |
}
|
| 72 |
}
|
|
|
|
| 56 |
|
| 57 |
async function getMe() {
|
| 58 |
const cookieStore = await cookies();
|
| 59 |
+
const tokenKey = MY_TOKEN_KEY();
|
| 60 |
+
const token = cookieStore.get(tokenKey)?.value;
|
| 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 {
|
| 72 |
const res = await apiServer.get("/me", {
|
|
|
|
| 76 |
});
|
| 77 |
return { user: res.data.user, projects: res.data.projects, errCode: null };
|
| 78 |
} catch (err: any) {
|
| 79 |
+
console.error("π [DEBUG] API Error:", err.status, err.message);
|
| 80 |
return { user: null, projects: [], errCode: err.status };
|
| 81 |
}
|
| 82 |
}
|