diff --git a/packages/api/middleware/auth.ts b/packages/api/middleware/auth.ts index 9f34d4b..a338b9a 100644 --- a/packages/api/middleware/auth.ts +++ b/packages/api/middleware/auth.ts @@ -13,7 +13,6 @@ export const authCheck = async ( next: NextFunction ) => { const { authorization } = req.headers; - console.log(authorization); try { const ticket = await client.verifyIdToken({ @@ -28,8 +27,6 @@ export const authCheck = async ( res.locals.userId = userId; res.locals.email = email; - console.log(userId); - next(); } catch (error) { res.status(401).send("unauthorized"); diff --git a/packages/api/routes/user.ts b/packages/api/routes/user.ts index d2f573f..e8123aa 100644 --- a/packages/api/routes/user.ts +++ b/packages/api/routes/user.ts @@ -61,10 +61,13 @@ async function getUserDetails(req: Request, res: Response) { // create user if not exists const insertResult = await UserService.createUserIfNotExists(newUser); + newUser._id = insertResult?.insertedId; insertResult - ? res.status(200).send("User created") + ? res.status(200).send(newUser) : res.status(500).send("Failed to create account, already exists"); + + return; } // otherwise, just return the user details diff --git a/packages/desktop/package.json b/packages/desktop/package.json index 95e841f..bbe7487 100644 --- a/packages/desktop/package.json +++ b/packages/desktop/package.json @@ -106,6 +106,7 @@ "react-dom": "^17.0.2", "react-icons": "^4.3.1", "react-query": "^3.34.16", - "react-router-dom": "^6.2.2" + "react-router-dom": "^6.2.2", + "recoil": "^0.6.1" } } diff --git a/packages/desktop/src/controller/index.tsx b/packages/desktop/src/controller/index.tsx index 5554a71..8b38319 100644 --- a/packages/desktop/src/controller/index.tsx +++ b/packages/desktop/src/controller/index.tsx @@ -1,16 +1,37 @@ import { useMutation, useQuery } from "react-query"; +import { $authTokens } from "./recoil"; +import { User } from "@nirvana/core/models"; +import axios from "axios"; import { nirvanaApi } from "./nirvanaApi"; import { queryClient } from "../nirvanaApp"; +import { useRecoilValue } from "recoil"; export enum Querytypes { GET_USER_DETAILS = "GET_USER_DETAILS", } -export function useGetUserDetails() { - return useQuery(Querytypes.GET_USER_DETAILS, nirvanaApi.user.getUserDetails, { - retry: false, +export const localHost = "http://localhost:5000/api"; + +const getUserDetails = async ( + accessToken: string, + idToken: string +): Promise => { + return await axios.get(localHost + `/users?access_token=${accessToken}`, { + headers: { Authorization: idToken }, }); +}; + +export function useGetUserDetails() { + const authTokens = useRecoilValue($authTokens); + + return useQuery( + Querytypes.GET_USER_DETAILS, + () => getUserDetails(authTokens.accessToken, authTokens.idToken), + { + retry: false, + } + ); } export function useCreateUser() { diff --git a/packages/desktop/src/controller/recoil.tsx b/packages/desktop/src/controller/recoil.tsx new file mode 100644 index 0000000..0b0aa81 --- /dev/null +++ b/packages/desktop/src/controller/recoil.tsx @@ -0,0 +1,10 @@ +import { atom } from "recoil"; + +export const $authTokens = atom<{ + accessToken: string; + refreshToken: string; + idToken: string; +}>({ + key: "AUTH_TOKENS", // unique ID (with respect to other atoms/selectors) + default: null, // default value (aka initial value) +}); diff --git a/packages/desktop/src/electron/handleLogin.ts b/packages/desktop/src/electron/handleLogin.ts index c1555d8..ce4a72a 100644 --- a/packages/desktop/src/electron/handleLogin.ts +++ b/packages/desktop/src/electron/handleLogin.ts @@ -37,8 +37,6 @@ export async function handleLogin() { const tokens = await myApiOauth.openAuthWindowAndGetTokens(); - console.log(tokens); - store.set(STORE_ITEMS.AUTH_TOKENS, tokens); browserWindow.webContents.send(Channels.AUTH_TOKENS, tokens); diff --git a/packages/desktop/src/nirvanaApp.tsx b/packages/desktop/src/nirvanaApp.tsx index 602061c..7bacdbd 100644 --- a/packages/desktop/src/nirvanaApp.tsx +++ b/packages/desktop/src/nirvanaApp.tsx @@ -5,6 +5,7 @@ import Home from "./pages/Home"; import Login from "./pages/Login"; import ProtectedRoute from "./components/ProtectedRoute"; import { ReactQueryDevtools } from "react-query/devtools"; +import { RecoilRoot } from "recoil"; import testConnection from "@nirvana/core"; testConnection(); @@ -16,23 +17,25 @@ function NirvanaApp() { return ( <> - - - } /> - - - - } - /> - {/* + + + + } /> + + + + } + /> + {/* */} - - + + - + + ); diff --git a/packages/desktop/src/pages/Login/index.tsx b/packages/desktop/src/pages/Login/index.tsx index 88248c6..3cfea2b 100644 --- a/packages/desktop/src/pages/Login/index.tsx +++ b/packages/desktop/src/pages/Login/index.tsx @@ -1,5 +1,6 @@ import { useEffect, useState } from "react"; +import { $authTokens } from "../../controller/recoil"; import Channels from "../../electron/constants"; import { CircularProgress } from "@mui/material"; import { FcGoogle } from "react-icons/fc"; @@ -7,9 +8,11 @@ import Logo from "../../components/Logo"; import { nirvanaApi } from "../../controller/nirvanaApi"; import { useCreateUser } from "../../controller/index"; import { useNavigate } from "react-router-dom"; +import { useSetRecoilState } from "recoil"; export default function Login() { const navigate = useNavigate(); + const setAuthTokens = useSetRecoilState($authTokens); const continueAuth = () => { setIsLoading(true); @@ -21,13 +24,14 @@ export default function Login() { useEffect(() => { window.electronAPI.auth.receiveTokens( Channels.AUTH_TOKENS, - async (tokens: any) => { - console.log(tokens); - + (tokens: any) => { // todo: implement refresh token procedure in api layer by sending refresh_token and such const { access_token, id_token, refresh_token } = tokens; - - nirvanaApi.setGoogleIdToken(id_token); + setAuthTokens({ + accessToken: access_token, + idToken: id_token, + refreshToken: refresh_token, + }); // now can go to home and get authenticated regardless of type of user navigate("/home"); diff --git a/yarn.lock b/yarn.lock index b1f94a4..6d0206f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3871,6 +3871,11 @@ gtoken@^5.0.4: google-p12-pem "^3.1.3" jws "^4.0.0" +hamt_plus@1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/hamt_plus/-/hamt_plus-1.0.2.tgz#e21c252968c7e33b20f6a1b094cd85787a265601" + integrity sha1-4hwlKWjH4zsg9qGwlM2FeHomVgE= + handle-thing@^2.0.0: version "2.0.1" resolved "https://registry.npmjs.org/handle-thing/-/handle-thing-2.0.1.tgz" @@ -6194,6 +6199,13 @@ readdirp@~3.6.0: dependencies: picomatch "^2.2.1" +recoil@^0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/recoil/-/recoil-0.6.1.tgz#6fdf1ba6cb20b5a2e54ec4abcbdaa287e8a03fa2" + integrity sha512-J7oT3LZl2vpyFClgSUpOQjpykz84VSX/NJE/PavAtR8n7Z+whEdVBPUtwc2TEWjONeL/lJmiac2XQ+qEOQA52Q== + dependencies: + hamt_plus "1.0.2" + regenerator-runtime@^0.13.4: version "0.13.9" resolved "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz"