From 15ab546c0ae729673b99f22fc0f0893c78b37619 Mon Sep 17 00:00:00 2001 From: Punarv Pawade Date: Thu, 14 Mar 2024 01:51:57 +0530 Subject: [PATCH] add auth endpoint to verify token --- webapp/src/App.tsx | 14 ++++++++------ webapp/src/components/Auth.tsx | 26 ++++++++++++++++++++++++++ webapp/src/index.tsx | 7 +++---- webapp/src/modules/Utils.ts | 3 +++ webapp/src/modules/api.ts | 20 +++++++++++++++++--- 5 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 webapp/src/components/Auth.tsx diff --git a/webapp/src/App.tsx b/webapp/src/App.tsx index dc1a308..d5b1a1d 100644 --- a/webapp/src/App.tsx +++ b/webapp/src/App.tsx @@ -1,20 +1,22 @@ // App.tsx -import { HashRouter as Router, Route, Routes } from 'react-router-dom'; -import Dashboard from './pages/Dashboard'; -import LandingPage from './pages/LandingPage'; -import Server from './pages/Server'; +import { BrowserRouter, Route, Routes } from "react-router-dom"; +import Dashboard from "./pages/Dashboard"; +import LandingPage from "./pages/LandingPage"; +import Server from "./pages/Server"; +import AuthComponent from "./components/Auth"; const App = () => { return ( - +
} /> + } /> } /> } />
-
+ ); }; diff --git a/webapp/src/components/Auth.tsx b/webapp/src/components/Auth.tsx new file mode 100644 index 0000000..9366f65 --- /dev/null +++ b/webapp/src/components/Auth.tsx @@ -0,0 +1,26 @@ +// AuthComponent.tsx +import React, { useEffect } from "react"; +import { useSearchParams, redirect } from "react-router-dom"; +import { verifyToken } from "../modules/api"; + +const AuthComponent = () => { + const verify = async (token: string | null) => { + const res = await verifyToken(token); + console.log(res.data); + }; + + useEffect(() => { + const [searchParams, setSearchParams] = useSearchParams(); + const token = searchParams.get("token"); + console.log("Token:", token); + verify(token); + }, []); + + return ( +
+

Authenticating...

+
+ ); +}; + +export default AuthComponent; diff --git a/webapp/src/index.tsx b/webapp/src/index.tsx index c9383dd..8502e36 100644 --- a/webapp/src/index.tsx +++ b/webapp/src/index.tsx @@ -1,5 +1,5 @@ import React from "react"; -import ReactDOM from "react-dom"; +import ReactDOM from "react-dom/client"; import "./index.css"; import App from "./App"; @@ -42,7 +42,7 @@ import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react"; const wallets = [new PetraWallet()]; -ReactDOM.render( +ReactDOM.createRoot(document.getElementById("root")!).render( {/* {/* */} - , - document.getElementById("root") + ); diff --git a/webapp/src/modules/Utils.ts b/webapp/src/modules/Utils.ts index 312a22d..d612940 100644 --- a/webapp/src/modules/Utils.ts +++ b/webapp/src/modules/Utils.ts @@ -1,4 +1,7 @@ export function getBaseUrl(): string { const { protocol, host } = window.location; return `${protocol}//${host}`; +} +export function getGatewayURL(): string | undefined { + return process.env.GATEWAY_URL; } \ No newline at end of file diff --git a/webapp/src/modules/api.ts b/webapp/src/modules/api.ts index 2ec2e9b..b386697 100644 --- a/webapp/src/modules/api.ts +++ b/webapp/src/modules/api.ts @@ -1,9 +1,9 @@ import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; -import { getBaseUrl } from './Utils'; +import { getBaseUrl, getGatewayURL } from './Utils'; import Cookies from 'js-cookie'; const baseURL = getBaseUrl() - +const gatewayURL = getGatewayURL() export interface UpdateClientPayload { id: string; name: string; @@ -192,4 +192,18 @@ export const getToken = async (signature: string | string[] | undefined, challen throw error; } return response; -}; \ No newline at end of file +}; + +export async function verifyToken(token: string | null) { + const url = `${gatewayURL}/api/v1.0/webapp/auth` + const response = await axios.get(url, { + headers: { + "Authorization": `Bearer ${Cookies.get("token")}` + } + }); + if (response.status === 200) { + return response.data; + } else { + throw new Error(`Request failed with status: ${response.status}`); + } +} \ No newline at end of file