working sign up and sign in as far as I know
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<User> => {
|
||||
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() {
|
||||
|
||||
@@ -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)
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
@@ -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 (
|
||||
<>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<HashRouter>
|
||||
<Routes>
|
||||
<Route path="/" element={<Login />} />
|
||||
<Route
|
||||
path="home"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<Home />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
{/* <Route exact path="/profile/create" component={Sit} />
|
||||
<RecoilRoot>
|
||||
<HashRouter>
|
||||
<Routes>
|
||||
<Route path="/" element={<Login />} />
|
||||
<Route
|
||||
path="home"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<Home />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
{/* <Route exact path="/profile/create" component={Sit} />
|
||||
<Route exact path="/profile/edit" component={Sit} /> */}
|
||||
</Routes>
|
||||
</HashRouter>
|
||||
</Routes>
|
||||
</HashRouter>
|
||||
|
||||
<ReactQueryDevtools initialIsOpen={false} />
|
||||
<ReactQueryDevtools initialIsOpen={false} />
|
||||
</RecoilRoot>
|
||||
</QueryClientProvider>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -3871,6 +3871,11 @@ gtoken@^5.0.4:
|
||||
google-p12-pem "^3.1.3"
|
||||
jws "^4.0.0"
|
||||
|
||||
[email protected]:
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user