enzostvs HF Staff commited on
Commit
1406bcb
Β·
1 Parent(s): 8b1282d

final stuffs

Browse files
app/api/auth/route.ts CHANGED
@@ -1,8 +1,4 @@
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
- // TODO refacto to do not use the window.postMessage, doesn't work with iFrame
6
 
7
  export async function GET() {
8
  const redirect_uri = process.env.REDIRECT_URI;
@@ -81,13 +77,6 @@ export async function POST(req: NextRequest) {
81
  );
82
  }
83
  const user = await userResponse.json();
84
- // const cookieStore = await cookies();
85
- // cookieStore.set(MY_TOKEN_KEY(), response.access_token, {
86
- // maxAge: response.expires_in,
87
- // httpOnly: false,
88
- // secure: true,
89
- // sameSite: "lax",
90
- // });
91
 
92
  return NextResponse.json(
93
  {
 
1
  import { NextRequest, NextResponse } from "next/server";
 
 
 
 
2
 
3
  export async function GET() {
4
  const redirect_uri = process.env.REDIRECT_URI;
 
77
  );
78
  }
79
  const user = await userResponse.json();
 
 
 
 
 
 
 
80
 
81
  return NextResponse.json(
82
  {
app/auth/callback/page.tsx CHANGED
@@ -1,28 +1,60 @@
1
  "use client";
2
-
3
  import { use } from "react";
4
- import { useBroadcastChannel } from "@/lib/useBroadcastChannel";
5
  import { useMount } from "react-use";
6
 
 
 
7
  export default function AuthCallback({
8
  searchParams,
9
  }: {
10
  searchParams: Promise<{ code: string }>;
11
  }) {
12
  const { code } = use(searchParams);
 
13
 
14
- const { postMessage } = useBroadcastChannel("auth", () => {});
15
- useMount(() => {
16
- console.log("AuthCallback mounted", code);
17
  if (code) {
18
- postMessage({
19
- code: code,
20
- type: "user-oauth",
21
- });
22
- // window.close();
23
- return;
24
  }
25
  });
26
 
27
- return <div>Login in progress ...</div>;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  }
 
1
  "use client";
2
+ import { useUser } from "@/hooks/useUser";
3
  import { use } from "react";
 
4
  import { useMount } from "react-use";
5
 
6
+ import { Button } from "@/components/ui/button";
7
+ import Link from "next/link";
8
  export default function AuthCallback({
9
  searchParams,
10
  }: {
11
  searchParams: Promise<{ code: string }>;
12
  }) {
13
  const { code } = use(searchParams);
14
+ const { loginFromCode } = useUser();
15
 
16
+ useMount(async () => {
 
 
17
  if (code) {
18
+ await loginFromCode(code);
 
 
 
 
 
19
  }
20
  });
21
 
22
+ return (
23
+ <div className="h-screen flex flex-col justify-center items-center">
24
+ <div className="!rounded-2xl !p-0 !bg-white !border-neutral-100 min-w-xs text-center overflow-hidden ring-[8px] ring-white/20">
25
+ <header className="bg-neutral-50 p-6 border-b border-neutral-200/60">
26
+ <div className="flex items-center justify-center -space-x-4 mb-3">
27
+ <div className="size-9 rounded-full bg-pink-200 shadow-2xs flex items-center justify-center text-xl opacity-50">
28
+ πŸš€
29
+ </div>
30
+ <div className="size-11 rounded-full bg-amber-200 shadow-2xl flex items-center justify-center text-2xl z-2">
31
+ πŸ‘‹
32
+ </div>
33
+ <div className="size-9 rounded-full bg-sky-200 shadow-2xs flex items-center justify-center text-xl opacity-50">
34
+ πŸ™Œ
35
+ </div>
36
+ </div>
37
+ <p className="text-xl font-semibold text-neutral-950">
38
+ Login In Progress...
39
+ </p>
40
+ <p className="text-sm text-neutral-500 mt-1.5">
41
+ Wait a moment while we log you in with your code.
42
+ </p>
43
+ </header>
44
+ <main className="space-y-4 p-6">
45
+ <div>
46
+ <p className="text-sm text-neutral-700 mb-4 max-w-xs">
47
+ If you are not redirected automatically, please click the button
48
+ below:
49
+ </p>
50
+ <Link href="/">
51
+ <Button variant="black" className="relative">
52
+ Go to Home
53
+ </Button>
54
+ </Link>
55
+ </div>
56
+ </main>
57
+ </div>
58
+ </div>
59
+ );
60
  }
hooks/useUser.ts CHANGED
@@ -17,6 +17,7 @@ export const useUser = (initialData?: {
17
  const client = useQueryClient();
18
  const router = useRouter();
19
  const [, setCookie, removeCookie] = useCookie(cookie_name);
 
20
 
21
  const { data: { user, errCode } = { user: null, errCode: null }, isLoading } =
22
  useQuery({
@@ -46,11 +47,8 @@ export const useUser = (initialData?: {
46
  };
47
 
48
  const openLoginWindow = async () => {
49
- return window.open(
50
- "/auth",
51
- "Login to DeepSite",
52
- "menubar=no,width=500,height=777,location=no,resizable=no,scrollbars=yes,status=no"
53
- );
54
  };
55
 
56
  const loginFromCode = async (code: string) => {
@@ -60,7 +58,6 @@ export const useUser = (initialData?: {
60
  .post("/auth", { code })
61
  .then(async (res: any) => {
62
  if (res.data) {
63
- // fix to be able to set the cookie through the space (Hugging Face)
64
  setCookie(res.data.access_token, {
65
  expires: res.data.expires_in
66
  ? new Date(Date.now() + res.data.expires_in * 1000)
@@ -72,6 +69,12 @@ export const useUser = (initialData?: {
72
  user: res.data.user,
73
  errCode: null,
74
  });
 
 
 
 
 
 
75
  toast.success("Login successful");
76
  }
77
  })
 
17
  const client = useQueryClient();
18
  const router = useRouter();
19
  const [, setCookie, removeCookie] = useCookie(cookie_name);
20
+ const [currentRoute, setCurrentRoute] = useCookie("deepsite-currentRoute");
21
 
22
  const { data: { user, errCode } = { user: null, errCode: null }, isLoading } =
23
  useQuery({
 
47
  };
48
 
49
  const openLoginWindow = async () => {
50
+ setCurrentRoute(window.location.pathname);
51
+ return router.push("/auth");
 
 
 
52
  };
53
 
54
  const loginFromCode = async (code: string) => {
 
58
  .post("/auth", { code })
59
  .then(async (res: any) => {
60
  if (res.data) {
 
61
  setCookie(res.data.access_token, {
62
  expires: res.data.expires_in
63
  ? new Date(Date.now() + res.data.expires_in * 1000)
 
69
  user: res.data.user,
70
  errCode: null,
71
  });
72
+ if (currentRoute) {
73
+ router.push(currentRoute);
74
+ setCurrentRoute("");
75
+ } else {
76
+ router.push("/projects");
77
+ }
78
  toast.success("Login successful");
79
  }
80
  })