From f94897ee23363b63649228fdd36f006bb834d4f9 Mon Sep 17 00:00:00 2001 From: Suja16 Date: Sat, 7 Dec 2024 23:00:27 +0530 Subject: [PATCH] login done correctly --- packages/nextjs/app/layout.tsx | 59 +++++++++++------------- packages/nextjs/app/metadata.ts | 2 +- packages/nextjs/components/AuthGuard.tsx | 21 +++++++++ 3 files changed, 50 insertions(+), 32 deletions(-) create mode 100644 packages/nextjs/components/AuthGuard.tsx diff --git a/packages/nextjs/app/layout.tsx b/packages/nextjs/app/layout.tsx index dd92149..4676bb0 100644 --- a/packages/nextjs/app/layout.tsx +++ b/packages/nextjs/app/layout.tsx @@ -1,8 +1,9 @@ "use client"; import { useEffect } from "react"; -import axios from "axios"; import "@rainbow-me/rainbowkit/styles.css"; +import axios from "axios"; +import { AuthGuard } from "~~/components/AuthGuard"; import { ScaffoldEthAppWithProviders } from "~~/components/ScaffoldEthAppWithProviders"; import { ThemeProvider } from "~~/components/ThemeProvider"; import { ApolloProvider } from "~~/context/ApolloProvider"; @@ -14,18 +15,18 @@ const TokenHandler = ({ children }: { children: React.ReactNode }) => { useEffect(() => { const urlParams = new URLSearchParams(window.location.search); - const accessToken = urlParams.get('access_token'); - const idToken = urlParams.get('id_token'); - const refreshToken = urlParams.get('refresh_token'); + const accessToken = urlParams.get("access_token"); + const idToken = urlParams.get("id_token"); + const refreshToken = urlParams.get("refresh_token"); if (accessToken || idToken || refreshToken) { - console.log('Access Token:', accessToken); - console.log('ID Token:', idToken); - console.log('Refresh Token:', refreshToken); - - localStorage.setItem('access_token', accessToken || ''); - localStorage.setItem('id_token', idToken || ''); - localStorage.setItem('refresh_token', refreshToken || ''); + console.log("Access Token:", accessToken); + console.log("ID Token:", idToken); + console.log("Refresh Token:", refreshToken); + + localStorage.setItem("access_token", accessToken || ""); + localStorage.setItem("id_token", idToken || ""); + localStorage.setItem("refresh_token", refreshToken || ""); if (accessToken) { setAccessToken(accessToken); @@ -41,31 +42,27 @@ const TokenHandler = ({ children }: { children: React.ReactNode }) => { const data = { aggregateBy: [ { - dataTypeName: "com.google.step_count.delta" - } + dataTypeName: "com.google.step_count.delta", + }, ], startTimeMillis: startOfDay, endTimeMillis: endOfDay, bucketByTime: { - durationMillis: 86400000 // 24 hours in milliseconds - } + durationMillis: 86400000, // 24 hours in milliseconds + }, }; try { - const response = await axios.post( - 'https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate', - data, - { - headers: { - 'Authorization': `Bearer ${accessToken}`, - 'Content-Type': 'application/json' - } - } - ); - console.log('Fitness Data:', JSON.stringify(response.data)); + const response = await axios.post("https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate", data, { + headers: { + Authorization: `Bearer ${accessToken}`, + "Content-Type": "application/json", + }, + }); + console.log("Fitness Data:", JSON.stringify(response.data)); setFitnessData(response.data); } catch (error) { - console.error('Fitness API Error:', error); + console.error("Fitness API Error:", error); } }; @@ -84,9 +81,9 @@ const ScaffoldEthApp = ({ children }: { children: React.ReactNode }) => { - - {children} - + + {children} + @@ -96,4 +93,4 @@ const ScaffoldEthApp = ({ children }: { children: React.ReactNode }) => { ); }; -export default ScaffoldEthApp; \ No newline at end of file +export default ScaffoldEthApp; diff --git a/packages/nextjs/app/metadata.ts b/packages/nextjs/app/metadata.ts index 15fc28f..75f5412 100644 --- a/packages/nextjs/app/metadata.ts +++ b/packages/nextjs/app/metadata.ts @@ -1,3 +1,3 @@ import { getMetadata } from "~~/utils/scaffold-eth/getMetadata"; -export const metadata = getMetadata({ title: "Momentum", description: "Built with 🏗 Scaffold-ETH 2" }); \ No newline at end of file +export const metadata = getMetadata({ title: "Momentum", description: "Built with 🏗 Scaffold-ETH 2" }); diff --git a/packages/nextjs/components/AuthGuard.tsx b/packages/nextjs/components/AuthGuard.tsx new file mode 100644 index 0000000..61ef904 --- /dev/null +++ b/packages/nextjs/components/AuthGuard.tsx @@ -0,0 +1,21 @@ +"use client"; + +import { useEffect } from "react"; +import { usePathname, useRouter } from "next/navigation"; + +const PUBLIC_PATHS = ["/login"]; + +export function AuthGuard({ children }: { children: React.ReactNode }) { + const router = useRouter(); + const pathname = usePathname(); + + useEffect(() => { + const accessToken = localStorage.getItem("access_token"); + + if (!accessToken && !PUBLIC_PATHS.includes(pathname)) { + router.push("/login"); + } + }, [router, pathname]); + + return <>{children}; +}