Skip to content

Commit

Permalink
add auth endpoint to verify token
Browse files Browse the repository at this point in the history
  • Loading branch information
inciner8r committed Mar 13, 2024
1 parent f3054c5 commit 15ab546
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
14 changes: 8 additions & 6 deletions webapp/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Router>
<BrowserRouter>
<div>
<Routes>
<Route path="/" element={<LandingPage />} />
<Route path="/auth" element={<AuthComponent />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/server" element={<Server />} />
</Routes>
</div>
</Router>
</BrowserRouter>
);
};

Expand Down
26 changes: 26 additions & 0 deletions webapp/src/components/Auth.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<p>Authenticating...</p>
</div>
);
};

export default AuthComponent;
7 changes: 3 additions & 4 deletions webapp/src/index.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -42,7 +42,7 @@ import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react";

const wallets = [new PetraWallet()];

ReactDOM.render(
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
{/* <WagmiConfig client={wagmiClient}>
<RainbowKitProvider
Expand All @@ -63,6 +63,5 @@ ReactDOM.render(
</AptosWalletAdapterProvider>
{/* </RainbowKitProvider>
</WagmiConfig> */}
</React.StrictMode>,
document.getElementById("root")
</React.StrictMode>
);
3 changes: 3 additions & 0 deletions webapp/src/modules/Utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 17 additions & 3 deletions webapp/src/modules/api.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -192,4 +192,18 @@ export const getToken = async (signature: string | string[] | undefined, challen
throw error;
}
return response;
};
};

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}`);
}
}

0 comments on commit 15ab546

Please sign in to comment.