Spaces:
Running
Running
test cookie
Browse files- app/api/auth/route.ts +9 -9
- components/contexts/app-context.tsx +1 -0
- hooks/useUser.ts +8 -7
app/api/auth/route.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
-
import { cookies } from "next/headers";
|
| 3 |
-
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 4 |
|
| 5 |
export async function GET() {
|
| 6 |
const redirect_uri = process.env.REDIRECT_URI;
|
|
@@ -79,13 +79,13 @@ export async function POST(req: NextRequest) {
|
|
| 79 |
);
|
| 80 |
}
|
| 81 |
const user = await userResponse.json();
|
| 82 |
-
const cookieStore = await cookies();
|
| 83 |
-
cookieStore.set(MY_TOKEN_KEY(), response.access_token, {
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
});
|
| 89 |
|
| 90 |
return NextResponse.json(
|
| 91 |
{
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
// import { cookies } from "next/headers";
|
| 3 |
+
// import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 4 |
|
| 5 |
export async function GET() {
|
| 6 |
const redirect_uri = process.env.REDIRECT_URI;
|
|
|
|
| 79 |
);
|
| 80 |
}
|
| 81 |
const user = await userResponse.json();
|
| 82 |
+
// const cookieStore = await cookies();
|
| 83 |
+
// cookieStore.set(MY_TOKEN_KEY(), response.access_token, {
|
| 84 |
+
// maxAge: response.expires_in,
|
| 85 |
+
// httpOnly: false,
|
| 86 |
+
// secure: true,
|
| 87 |
+
// sameSite: "lax",
|
| 88 |
+
// });
|
| 89 |
|
| 90 |
return NextResponse.json(
|
| 91 |
{
|
components/contexts/app-context.tsx
CHANGED
|
@@ -45,6 +45,7 @@ export default function AppContext({
|
|
| 45 |
|
| 46 |
if (!message.code) return;
|
| 47 |
if (message.type === "user-oauth" && message?.code && !events.code) {
|
|
|
|
| 48 |
loginFromCode(message.code);
|
| 49 |
}
|
| 50 |
});
|
|
|
|
| 45 |
|
| 46 |
if (!message.code) return;
|
| 47 |
if (message.type === "user-oauth" && message?.code && !events.code) {
|
| 48 |
+
console.log("Received code from broadcast channel:", message.code);
|
| 49 |
loginFromCode(message.code);
|
| 50 |
}
|
| 51 |
});
|
hooks/useUser.ts
CHANGED
|
@@ -54,6 +54,7 @@ export const useUser = (initialData?: {
|
|
| 54 |
};
|
| 55 |
|
| 56 |
const loginFromCode = async (code: string) => {
|
|
|
|
| 57 |
setLoadingAuth(true);
|
| 58 |
if (loadingAuth) return;
|
| 59 |
await api
|
|
@@ -61,13 +62,13 @@ export const useUser = (initialData?: {
|
|
| 61 |
.then(async (res: any) => {
|
| 62 |
if (res.data) {
|
| 63 |
// fix to be able to set the cookie through the space (Hugging Face)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
client.setQueryData(["user.me"], {
|
| 72 |
user: res.data.user,
|
| 73 |
errCode: null,
|
|
|
|
| 54 |
};
|
| 55 |
|
| 56 |
const loginFromCode = async (code: string) => {
|
| 57 |
+
console.log("Login from code => ", code);
|
| 58 |
setLoadingAuth(true);
|
| 59 |
if (loadingAuth) return;
|
| 60 |
await api
|
|
|
|
| 62 |
.then(async (res: any) => {
|
| 63 |
if (res.data) {
|
| 64 |
// fix to be able to set the cookie through the space (Hugging Face)
|
| 65 |
+
setCookie(res.data.access_token, {
|
| 66 |
+
expires: res.data.expires_in
|
| 67 |
+
? new Date(Date.now() + res.data.expires_in * 1000)
|
| 68 |
+
: undefined,
|
| 69 |
+
sameSite: "none",
|
| 70 |
+
secure: true,
|
| 71 |
+
});
|
| 72 |
client.setQueryData(["user.me"], {
|
| 73 |
user: res.data.user,
|
| 74 |
errCode: null,
|