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
|
next: NextFunction
|
||||||
) => {
|
) => {
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
console.log(authorization);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ticket = await client.verifyIdToken({
|
const ticket = await client.verifyIdToken({
|
||||||
@@ -28,8 +27,6 @@ export const authCheck = async (
|
|||||||
res.locals.userId = userId;
|
res.locals.userId = userId;
|
||||||
res.locals.email = email;
|
res.locals.email = email;
|
||||||
|
|
||||||
console.log(userId);
|
|
||||||
|
|
||||||
next();
|
next();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(401).send("unauthorized");
|
res.status(401).send("unauthorized");
|
||||||
|
|||||||
@@ -61,10 +61,13 @@ async function getUserDetails(req: Request, res: Response) {
|
|||||||
|
|
||||||
// create user if not exists
|
// create user if not exists
|
||||||
const insertResult = await UserService.createUserIfNotExists(newUser);
|
const insertResult = await UserService.createUserIfNotExists(newUser);
|
||||||
|
newUser._id = insertResult?.insertedId;
|
||||||
|
|
||||||
insertResult
|
insertResult
|
||||||
? res.status(200).send("User created")
|
? res.status(200).send(newUser)
|
||||||
: res.status(500).send("Failed to create account, already exists");
|
: res.status(500).send("Failed to create account, already exists");
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, just return the user details
|
// otherwise, just return the user details
|
||||||
|
|||||||
@@ -106,6 +106,7 @@
|
|||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
"react-icons": "^4.3.1",
|
"react-icons": "^4.3.1",
|
||||||
"react-query": "^3.34.16",
|
"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 { useMutation, useQuery } from "react-query";
|
||||||
|
|
||||||
|
import { $authTokens } from "./recoil";
|
||||||
|
import { User } from "@nirvana/core/models";
|
||||||
|
import axios from "axios";
|
||||||
import { nirvanaApi } from "./nirvanaApi";
|
import { nirvanaApi } from "./nirvanaApi";
|
||||||
import { queryClient } from "../nirvanaApp";
|
import { queryClient } from "../nirvanaApp";
|
||||||
|
import { useRecoilValue } from "recoil";
|
||||||
|
|
||||||
export enum Querytypes {
|
export enum Querytypes {
|
||||||
GET_USER_DETAILS = "GET_USER_DETAILS",
|
GET_USER_DETAILS = "GET_USER_DETAILS",
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useGetUserDetails() {
|
export const localHost = "http://localhost:5000/api";
|
||||||
return useQuery(Querytypes.GET_USER_DETAILS, nirvanaApi.user.getUserDetails, {
|
|
||||||
retry: false,
|
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() {
|
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();
|
const tokens = await myApiOauth.openAuthWindowAndGetTokens();
|
||||||
|
|
||||||
console.log(tokens);
|
|
||||||
|
|
||||||
store.set(STORE_ITEMS.AUTH_TOKENS, tokens);
|
store.set(STORE_ITEMS.AUTH_TOKENS, tokens);
|
||||||
|
|
||||||
browserWindow.webContents.send(Channels.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 Login from "./pages/Login";
|
||||||
import ProtectedRoute from "./components/ProtectedRoute";
|
import ProtectedRoute from "./components/ProtectedRoute";
|
||||||
import { ReactQueryDevtools } from "react-query/devtools";
|
import { ReactQueryDevtools } from "react-query/devtools";
|
||||||
|
import { RecoilRoot } from "recoil";
|
||||||
import testConnection from "@nirvana/core";
|
import testConnection from "@nirvana/core";
|
||||||
|
|
||||||
testConnection();
|
testConnection();
|
||||||
@@ -16,23 +17,25 @@ function NirvanaApp() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<HashRouter>
|
<RecoilRoot>
|
||||||
<Routes>
|
<HashRouter>
|
||||||
<Route path="/" element={<Login />} />
|
<Routes>
|
||||||
<Route
|
<Route path="/" element={<Login />} />
|
||||||
path="home"
|
<Route
|
||||||
element={
|
path="home"
|
||||||
<ProtectedRoute>
|
element={
|
||||||
<Home />
|
<ProtectedRoute>
|
||||||
</ProtectedRoute>
|
<Home />
|
||||||
}
|
</ProtectedRoute>
|
||||||
/>
|
}
|
||||||
{/* <Route exact path="/profile/create" component={Sit} />
|
/>
|
||||||
|
{/* <Route exact path="/profile/create" component={Sit} />
|
||||||
<Route exact path="/profile/edit" component={Sit} /> */}
|
<Route exact path="/profile/edit" component={Sit} /> */}
|
||||||
</Routes>
|
</Routes>
|
||||||
</HashRouter>
|
</HashRouter>
|
||||||
|
|
||||||
<ReactQueryDevtools initialIsOpen={false} />
|
<ReactQueryDevtools initialIsOpen={false} />
|
||||||
|
</RecoilRoot>
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
import { $authTokens } from "../../controller/recoil";
|
||||||
import Channels from "../../electron/constants";
|
import Channels from "../../electron/constants";
|
||||||
import { CircularProgress } from "@mui/material";
|
import { CircularProgress } from "@mui/material";
|
||||||
import { FcGoogle } from "react-icons/fc";
|
import { FcGoogle } from "react-icons/fc";
|
||||||
@@ -7,9 +8,11 @@ import Logo from "../../components/Logo";
|
|||||||
import { nirvanaApi } from "../../controller/nirvanaApi";
|
import { nirvanaApi } from "../../controller/nirvanaApi";
|
||||||
import { useCreateUser } from "../../controller/index";
|
import { useCreateUser } from "../../controller/index";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useSetRecoilState } from "recoil";
|
||||||
|
|
||||||
export default function Login() {
|
export default function Login() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const setAuthTokens = useSetRecoilState($authTokens);
|
||||||
|
|
||||||
const continueAuth = () => {
|
const continueAuth = () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
@@ -21,13 +24,14 @@ export default function Login() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.electronAPI.auth.receiveTokens(
|
window.electronAPI.auth.receiveTokens(
|
||||||
Channels.AUTH_TOKENS,
|
Channels.AUTH_TOKENS,
|
||||||
async (tokens: any) => {
|
(tokens: any) => {
|
||||||
console.log(tokens);
|
|
||||||
|
|
||||||
// todo: implement refresh token procedure in api layer by sending refresh_token and such
|
// todo: implement refresh token procedure in api layer by sending refresh_token and such
|
||||||
const { access_token, id_token, refresh_token } = tokens;
|
const { access_token, id_token, refresh_token } = tokens;
|
||||||
|
setAuthTokens({
|
||||||
nirvanaApi.setGoogleIdToken(id_token);
|
accessToken: access_token,
|
||||||
|
idToken: id_token,
|
||||||
|
refreshToken: refresh_token,
|
||||||
|
});
|
||||||
|
|
||||||
// now can go to home and get authenticated regardless of type of user
|
// now can go to home and get authenticated regardless of type of user
|
||||||
navigate("/home");
|
navigate("/home");
|
||||||
|
|||||||
@@ -3871,6 +3871,11 @@ gtoken@^5.0.4:
|
|||||||
google-p12-pem "^3.1.3"
|
google-p12-pem "^3.1.3"
|
||||||
jws "^4.0.0"
|
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:
|
handle-thing@^2.0.0:
|
||||||
version "2.0.1"
|
version "2.0.1"
|
||||||
resolved "https://registry.npmjs.org/handle-thing/-/handle-thing-2.0.1.tgz"
|
resolved "https://registry.npmjs.org/handle-thing/-/handle-thing-2.0.1.tgz"
|
||||||
@@ -6194,6 +6199,13 @@ readdirp@~3.6.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
picomatch "^2.2.1"
|
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:
|
regenerator-runtime@^0.13.4:
|
||||||
version "0.13.9"
|
version "0.13.9"
|
||||||
resolved "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz"
|
resolved "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz"
|
||||||
|
|||||||
Reference in New Issue
Block a user