Spaces:
Running
Running
do it client side
Browse files- app/layout.tsx +18 -20
- components/contexts/app-context.tsx +21 -22
- hooks/useUser.ts +13 -22
app/layout.tsx
CHANGED
|
@@ -54,32 +54,30 @@ export const viewport: Viewport = {
|
|
| 54 |
themeColor: "#000000",
|
| 55 |
};
|
| 56 |
|
| 57 |
-
async function getMe() {
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
}
|
| 75 |
-
}
|
| 76 |
|
| 77 |
export default async function RootLayout({
|
| 78 |
children,
|
| 79 |
}: Readonly<{
|
| 80 |
children: React.ReactNode;
|
| 81 |
}>) {
|
| 82 |
-
const data = await getMe();
|
| 83 |
|
| 84 |
// Generate structured data
|
| 85 |
const structuredData = generateStructuredData("WebApplication", {
|
|
@@ -118,7 +116,7 @@ export default async function RootLayout({
|
|
| 118 |
<IframeDetector />
|
| 119 |
<Toaster richColors position="bottom-center" />
|
| 120 |
<TanstackContext>
|
| 121 |
-
<AppContext
|
| 122 |
<LoginProvider>
|
| 123 |
<ProProvider>{children}</ProProvider>
|
| 124 |
</LoginProvider>
|
|
|
|
| 54 |
themeColor: "#000000",
|
| 55 |
};
|
| 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 {
|
| 64 |
+
// const res = await apiServer.get("/me", {
|
| 65 |
+
// headers: {
|
| 66 |
+
// Authorization: `Bearer ${token}`,
|
| 67 |
+
// },
|
| 68 |
+
// });
|
| 69 |
+
// return { user: res.data.user, projects: res.data.projects, errCode: null };
|
| 70 |
+
// } catch (err: any) {
|
| 71 |
+
// return { user: null, projects: [], errCode: err.status };
|
| 72 |
+
// }
|
| 73 |
+
// }
|
|
|
|
|
|
|
| 74 |
|
| 75 |
export default async function RootLayout({
|
| 76 |
children,
|
| 77 |
}: Readonly<{
|
| 78 |
children: React.ReactNode;
|
| 79 |
}>) {
|
| 80 |
+
// const data = await getMe();
|
| 81 |
|
| 82 |
// Generate structured data
|
| 83 |
const structuredData = generateStructuredData("WebApplication", {
|
|
|
|
| 116 |
<IframeDetector />
|
| 117 |
<Toaster richColors position="bottom-center" />
|
| 118 |
<TanstackContext>
|
| 119 |
+
<AppContext>
|
| 120 |
<LoginProvider>
|
| 121 |
<ProProvider>{children}</ProProvider>
|
| 122 |
</LoginProvider>
|
components/contexts/app-context.tsx
CHANGED
|
@@ -10,33 +10,32 @@ import { useBroadcastChannel } from "@/lib/useBroadcastChannel";
|
|
| 10 |
|
| 11 |
export default function AppContext({
|
| 12 |
children,
|
| 13 |
-
|
| 14 |
-
|
| 15 |
children: React.ReactNode;
|
| 16 |
-
me?: {
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
};
|
| 21 |
}) {
|
| 22 |
-
const { loginFromCode, user, logout, loading, errCode } =
|
| 23 |
-
useUser(initialData);
|
| 24 |
const pathname = usePathname();
|
| 25 |
const router = useRouter();
|
| 26 |
|
| 27 |
-
useMount(() => {
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
});
|
| 40 |
|
| 41 |
const events: any = {};
|
| 42 |
|
|
|
|
| 10 |
|
| 11 |
export default function AppContext({
|
| 12 |
children,
|
| 13 |
+
}: // me: initialData,
|
| 14 |
+
{
|
| 15 |
children: React.ReactNode;
|
| 16 |
+
// me?: {
|
| 17 |
+
// user: User | null;
|
| 18 |
+
// projects: ProjectType[];
|
| 19 |
+
// errCode: number | null;
|
| 20 |
+
// };
|
| 21 |
}) {
|
| 22 |
+
const { loginFromCode, user, logout, loading, errCode } = useUser();
|
|
|
|
| 23 |
const pathname = usePathname();
|
| 24 |
const router = useRouter();
|
| 25 |
|
| 26 |
+
// useMount(() => {
|
| 27 |
+
// if (!initialData?.user && !user) {
|
| 28 |
+
// if ([401, 403].includes(errCode as number)) {
|
| 29 |
+
// logout();
|
| 30 |
+
// } else if (pathname.includes("/spaces")) {
|
| 31 |
+
// if (errCode) {
|
| 32 |
+
// toast.error("An error occured while trying to log in");
|
| 33 |
+
// }
|
| 34 |
+
// // If we did not manage to log in (probs because api is down), we simply redirect to the home page
|
| 35 |
+
// router.push("/");
|
| 36 |
+
// }
|
| 37 |
+
// }
|
| 38 |
+
// });
|
| 39 |
|
| 40 |
const events: any = {};
|
| 41 |
|
hooks/useUser.ts
CHANGED
|
@@ -18,22 +18,26 @@ export const useUser = (initialData?: {
|
|
| 18 |
const client = useQueryClient();
|
| 19 |
const router = useRouter();
|
| 20 |
const [, setCurrentRoute, removeCurrentRoute] = useCookie("deepsite-currentRoute");
|
| 21 |
-
const [, setToken, removeToken] = useCookie(MY_TOKEN_KEY());
|
| 22 |
|
| 23 |
-
const { data: { user, errCode } = { user: null, errCode: null }, isLoading } =
|
| 24 |
useQuery({
|
| 25 |
queryKey: ["user.me"],
|
| 26 |
queryFn: async () => {
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
},
|
| 29 |
refetchOnWindowFocus: false,
|
| 30 |
refetchOnReconnect: false,
|
| 31 |
refetchOnMount: false,
|
| 32 |
retry: false,
|
| 33 |
-
|
| 34 |
-
? { user: initialData?.user, errCode: initialData?.errCode }
|
| 35 |
-
: undefined,
|
| 36 |
-
enabled: false,
|
| 37 |
});
|
| 38 |
|
| 39 |
const { data: loadingAuth } = useQuery({
|
|
@@ -90,21 +94,8 @@ export const useUser = (initialData?: {
|
|
| 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 |
-
|
| 94 |
-
|
| 95 |
-
client.setQueryData(["user.me"], {
|
| 96 |
-
user: meResponse.data.user,
|
| 97 |
-
errCode: null,
|
| 98 |
-
});
|
| 99 |
-
if (meResponse.data.projects) {
|
| 100 |
-
setProjects(meResponse.data.projects);
|
| 101 |
-
}
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
setTimeout(() => {
|
| 105 |
-
window.location.href = "/";
|
| 106 |
-
}, 100);
|
| 107 |
-
|
| 108 |
toast.success("Login successful");
|
| 109 |
}
|
| 110 |
})
|
|
|
|
| 18 |
const client = useQueryClient();
|
| 19 |
const router = useRouter();
|
| 20 |
const [, setCurrentRoute, removeCurrentRoute] = useCookie("deepsite-currentRoute");
|
| 21 |
+
const [token, setToken, removeToken] = useCookie(MY_TOKEN_KEY());
|
| 22 |
|
| 23 |
+
const { data: { user, errCode } = { user: null, errCode: null }, isLoading, refetch: refetchMe } =
|
| 24 |
useQuery({
|
| 25 |
queryKey: ["user.me"],
|
| 26 |
queryFn: async () => {
|
| 27 |
+
const me = await api.get("/me");
|
| 28 |
+
if (me.data) {
|
| 29 |
+
if (me.data.projects) {
|
| 30 |
+
setProjects(me.data.projects);
|
| 31 |
+
}
|
| 32 |
+
return { user: me.data.user, errCode: me.data.errCode };
|
| 33 |
+
}
|
| 34 |
+
return { user: null, errCode: null };
|
| 35 |
},
|
| 36 |
refetchOnWindowFocus: false,
|
| 37 |
refetchOnReconnect: false,
|
| 38 |
refetchOnMount: false,
|
| 39 |
retry: false,
|
| 40 |
+
enabled: true,
|
|
|
|
|
|
|
|
|
|
| 41 |
});
|
| 42 |
|
| 43 |
const { data: loadingAuth } = useQuery({
|
|
|
|
| 94 |
const cookieString = `${MY_TOKEN_KEY()}=${res.data.access_token}; path=/; max-age=${expiresIn}; samesite=lax${cookieOptions.secure ? '; secure' : ''}`;
|
| 95 |
document.cookie = cookieString;
|
| 96 |
|
| 97 |
+
refetchMe();
|
| 98 |
+
router.push("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
toast.success("Login successful");
|
| 100 |
}
|
| 101 |
})
|