adding axios and call to backend working with the auth check middleware

This commit is contained in:
talksik
2022-03-16 09:24:05 -04:00
parent 565b01cd8e
commit 0ad5429b53
9 changed files with 85 additions and 28 deletions
+21 -9
View File
@@ -1,6 +1,13 @@
import "./App.css";
import { HashRouter, Route, Routes } from "react-router-dom";
import {
QueryClient,
QueryClientProvider,
useMutation,
useQuery,
useQueryClient,
} from "react-query";
import Home from "./pages/Home";
import Login from "./pages/Login";
@@ -8,19 +15,24 @@ import testConnection from "@nirvana/core";
testConnection();
// Create a client
const queryClient = new QueryClient();
function App() {
return (
<>
<HashRouter>
<div className="App">
<Routes>
<Route path="/" element={<Login />} />
<Route path="/home" element={<Home />} />
{/* <Route exact path="/profile/create" component={Sit} />
<QueryClientProvider client={queryClient}>
<HashRouter>
<div className="App">
<Routes>
<Route path="/" element={<Login />} />
<Route path="/home" element={<Home />} />
{/* <Route exact path="/profile/create" component={Sit} />
<Route exact path="/profile/edit" component={Sit} /> */}
</Routes>
</div>
</HashRouter>
</Routes>
</div>
</HashRouter>
</QueryClientProvider>
</>
);
}
+19
View File
@@ -0,0 +1,19 @@
import { useMutation, useQuery } from "react-query";
import axios from "axios";
export enum Querytypes {
GET_USER_DETAILS = "GET_USER_DETAILS",
}
export const localHost = process.env.REACT_APP_API_DOMAIN;
export function useGetUserDetails() {
return useQuery(Querytypes.GET_USER_DETAILS, () =>
axios({
method: "GET",
url: localHost + `/users`,
headers: { Authorization: "fake_id_token" },
})
);
}
+17 -4
View File
@@ -1,8 +1,10 @@
import { Button, CircularProgress } from "@mui/material";
import { localHost, useGetUserDetails } from "../../controller";
import { useEffect, useState } from "react";
import Logo from "../../components/Logo";
import { UserInfo } from "@nirvana/core/models";
import axios from "axios";
import { channels } from "../../shared/constants";
import { useNavigate } from "react-router-dom";
@@ -15,20 +17,31 @@ export default function Login() {
};
useEffect(() => {
window.API.receive(channels.AUTH_TOKENS, async (accessToken: string) => {
console.log(accessToken);
window.API.receive(channels.AUTH_TOKENS, async (tokens: any) => {
console.log(tokens);
const { access_token, id_token, refresh_token } = tokens;
// get user info from access token
const userInfo: UserInfo = await (
await fetch(
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${access_token}`
)
).json();
console.log("user data: ");
console.log(userInfo);
// if user is a user, then continue to next route
// if user is an existing user, then continue to next route
axios({
method: "GET",
url: localHost + `/user`,
headers: {
Authorization:
"eyJhbGciOiJSUzI1NiIsImtpZCI6ImQ2M2RiZTczYWFkODhjODU0ZGUwZDhkNmMwMTRjMzZkYzI1YzQyOTIiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI0MjM1MzMyNDQ5NTMtYmFubGlnb2JnYm9mOGhnODlpNmNyMWw3dTBwN2MycGsuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI0MjM1MzMyNDQ5NTMtYmFubGlnb2JnYm9mOGhnODlpNmNyMWw3dTBwN2MycGsuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTM0NzA3ODY2OTAzNTMxMDkwODYiLCJlbWFpbCI6InBhdGVsLmFyanVuNTBAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiJKRlBBOC15TGgxeGlGOW5acUZQU0FBIiwibmFtZSI6IkFyanVuIFBhdGVsIiwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BT2gxNEdqbTI5U2QxV25tOE52WmJYa29zdmY2U29JRDZrQlA1T0hSTFZJT0JRPXM5Ni1jIiwiZ2l2ZW5fbmFtZSI6IkFyanVuIiwiZmFtaWx5X25hbWUiOiJQYXRlbCIsImxvY2FsZSI6ImVuIiwiaWF0IjoxNjQ3Mzc4MzgzLCJleHAiOjE2NDczODE5ODN9.llIYMEjq3Ye1UFxiKIsgtjnRho2Ad7dQjCSFVDDlsiH3AVq_MssoKTXMS0tuHpQAZAfyqbi1kRpp4Ax3mFJb44z2OIyzOTDzhcNB_L_3C8U3gRuSmUaVfBa7EdkobkxByee3ddvjekD7Y9dobtXs3IllldjewD9Eg9FZmEkuSN6yYVxxRLYNYTjk_BAA17QKKkPPpyanAHHXLwxpMFhQc34tuAYCYmmDQQijV8YVvU4r9ruPpysumVXlUmX3CuNg9P415Dq1lUHQxaf5hWf_6RxIsw9PNg1wZ8CuJGvmLFnGBRQACKgnAkI9E7-tu2tWe3glRhORZlQmgRCKqG5xoA",
},
}).then((res) => console.log(res));
navigate("/home");
// if user is not an existing user, then show create account page