adding axios and call to backend working with the auth check middleware
This commit is contained in:
@@ -12,17 +12,21 @@ export const authCheck = async (
|
|||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction
|
next: NextFunction
|
||||||
) => {
|
) => {
|
||||||
console.log(req.headers);
|
const { authorization } = req.headers;
|
||||||
|
console.log(authorization);
|
||||||
|
|
||||||
// todo get idToken from frontend
|
try {
|
||||||
const ticket = await client.verifyIdToken({
|
const ticket = await client.verifyIdToken({
|
||||||
idToken: "",
|
idToken: authorization ?? "",
|
||||||
audience:
|
audience:
|
||||||
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com", // Specify the CLIENT_ID of the app that accesses the backend
|
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com", // Specify the CLIENT_ID of the app that accesses the backend
|
||||||
});
|
});
|
||||||
const userid = ticket.getPayload()?.sub;
|
const userid = ticket.getPayload()?.sub;
|
||||||
|
|
||||||
console.log(userid);
|
console.log(userid);
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
res.status(401).send(error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"ignore": ["**/*.test.ts", "**/*.spec.ts", "node_modules"],
|
"ignore": ["**/*.test.ts", "**/*.spec.ts", "node_modules"],
|
||||||
"watch": ["src"],
|
"watch": ["./"],
|
||||||
"exec": "npm start",
|
"exec": "npm start",
|
||||||
"ext": "ts"
|
"ext": "ts"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
import express, { Application, Request, Response } from "express";
|
import express, { Application, Request, Response } from "express";
|
||||||
|
|
||||||
|
import { authCheck } from "../middleware/auth";
|
||||||
|
|
||||||
export default function getUserRoutes() {
|
export default function getUserRoutes() {
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// get user details based on id from token
|
// get user details based on id from token
|
||||||
router.get("/", getUserDetails);
|
router.get("/", authCheck, getUserDetails);
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getUserDetails(req: Request, res: Response) {
|
async function getUserDetails(req: Request, res: Response) {
|
||||||
console.log(req);
|
|
||||||
|
|
||||||
res.send({ message: "check" });
|
res.send({ message: "check" });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
REACT_APP_GOOGLE_OAUTH_CLIENT_ID=423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com
|
REACT_APP_GOOGLE_OAUTH_CLIENT_ID=423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com
|
||||||
REACT_APP_GOOGLE_OAUTH_SECRET=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
|
REACT_APP_GOOGLE_OAUTH_SECRET=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
|
||||||
|
REACT_APP_API_DOMAIN=http://localhost:5000/api
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
"@types/node": "^16.11.26",
|
"@types/node": "^16.11.26",
|
||||||
"@types/react": "^17.0.40",
|
"@types/react": "^17.0.40",
|
||||||
"@types/react-dom": "^17.0.13",
|
"@types/react-dom": "^17.0.13",
|
||||||
|
"axios": "^0.26.1",
|
||||||
"electron-store": "^8.0.1",
|
"electron-store": "^8.0.1",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
import "./App.css";
|
import "./App.css";
|
||||||
|
|
||||||
import { HashRouter, Route, Routes } from "react-router-dom";
|
import { HashRouter, Route, Routes } from "react-router-dom";
|
||||||
|
import {
|
||||||
|
QueryClient,
|
||||||
|
QueryClientProvider,
|
||||||
|
useMutation,
|
||||||
|
useQuery,
|
||||||
|
useQueryClient,
|
||||||
|
} from "react-query";
|
||||||
|
|
||||||
import Home from "./pages/Home";
|
import Home from "./pages/Home";
|
||||||
import Login from "./pages/Login";
|
import Login from "./pages/Login";
|
||||||
@@ -8,19 +15,24 @@ import testConnection from "@nirvana/core";
|
|||||||
|
|
||||||
testConnection();
|
testConnection();
|
||||||
|
|
||||||
|
// Create a client
|
||||||
|
const queryClient = new QueryClient();
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<HashRouter>
|
<QueryClientProvider client={queryClient}>
|
||||||
<div className="App">
|
<HashRouter>
|
||||||
<Routes>
|
<div className="App">
|
||||||
<Route path="/" element={<Login />} />
|
<Routes>
|
||||||
<Route path="/home" element={<Home />} />
|
<Route path="/" element={<Login />} />
|
||||||
{/* <Route exact path="/profile/create" component={Sit} />
|
<Route path="/home" element={<Home />} />
|
||||||
|
{/* <Route exact path="/profile/create" component={Sit} />
|
||||||
<Route exact path="/profile/edit" component={Sit} /> */}
|
<Route exact path="/profile/edit" component={Sit} /> */}
|
||||||
</Routes>
|
</Routes>
|
||||||
</div>
|
</div>
|
||||||
</HashRouter>
|
</HashRouter>
|
||||||
|
</QueryClientProvider>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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" },
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import { Button, CircularProgress } from "@mui/material";
|
import { Button, CircularProgress } from "@mui/material";
|
||||||
|
import { localHost, useGetUserDetails } from "../../controller";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import Logo from "../../components/Logo";
|
import Logo from "../../components/Logo";
|
||||||
import { UserInfo } from "@nirvana/core/models";
|
import { UserInfo } from "@nirvana/core/models";
|
||||||
|
import axios from "axios";
|
||||||
import { channels } from "../../shared/constants";
|
import { channels } from "../../shared/constants";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
@@ -15,20 +17,31 @@ export default function Login() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.API.receive(channels.AUTH_TOKENS, async (accessToken: string) => {
|
window.API.receive(channels.AUTH_TOKENS, async (tokens: any) => {
|
||||||
console.log(accessToken);
|
console.log(tokens);
|
||||||
|
|
||||||
|
const { access_token, id_token, refresh_token } = tokens;
|
||||||
|
|
||||||
// get user info from access token
|
// get user info from access token
|
||||||
const userInfo: UserInfo = await (
|
const userInfo: UserInfo = await (
|
||||||
await fetch(
|
await fetch(
|
||||||
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`
|
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${access_token}`
|
||||||
)
|
)
|
||||||
).json();
|
).json();
|
||||||
|
|
||||||
console.log("user data: ");
|
console.log("user data: ");
|
||||||
console.log(userInfo);
|
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:
|
||||||
|
"eyJhbGciOiJSUzI1NiIsImtpZCI6ImQ2M2RiZTczYWFkODhjODU0ZGUwZDhkNmMwMTRjMzZkY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xoA",
|
||||||
|
},
|
||||||
|
}).then((res) => console.log(res));
|
||||||
|
|
||||||
navigate("/home");
|
navigate("/home");
|
||||||
|
|
||||||
// if user is not an existing user, then show create account page
|
// if user is not an existing user, then show create account page
|
||||||
|
|||||||
@@ -3276,6 +3276,13 @@ axios@^0.25.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
follow-redirects "^1.14.7"
|
follow-redirects "^1.14.7"
|
||||||
|
|
||||||
|
axios@^0.26.1:
|
||||||
|
version "0.26.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
|
||||||
|
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
|
||||||
|
dependencies:
|
||||||
|
follow-redirects "^1.14.8"
|
||||||
|
|
||||||
axobject-query@^2.2.0:
|
axobject-query@^2.2.0:
|
||||||
version "2.2.0"
|
version "2.2.0"
|
||||||
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
|
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
|
||||||
@@ -5864,7 +5871,7 @@ flora-colossus@^1.0.0:
|
|||||||
debug "^4.1.1"
|
debug "^4.1.1"
|
||||||
fs-extra "^7.0.0"
|
fs-extra "^7.0.0"
|
||||||
|
|
||||||
follow-redirects@^1.0.0, follow-redirects@^1.14.7:
|
follow-redirects@^1.0.0, follow-redirects@^1.14.7, follow-redirects@^1.14.8:
|
||||||
version "1.14.9"
|
version "1.14.9"
|
||||||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
|
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
|
||||||
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==
|
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==
|
||||||
|
|||||||
Reference in New Issue
Block a user