fixing things across the board
This commit is contained in:
@@ -22,9 +22,11 @@ export const authCheck = async (
|
||||
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com", // Specify the CLIENT_ID of the app that accesses the backend
|
||||
});
|
||||
const userId = ticket.getPayload()?.sub;
|
||||
const email = ticket.getPayload()?.email;
|
||||
|
||||
// used in subsequent handlers
|
||||
res.locals.userId = userId;
|
||||
res.locals.email = email;
|
||||
|
||||
console.log(userId);
|
||||
|
||||
|
||||
@@ -24,22 +24,59 @@ export default function getUserRoutes() {
|
||||
* create user if doesn't exist
|
||||
*/
|
||||
async function getUserDetails(req: Request, res: Response) {
|
||||
const userId: string = res.locals.userId;
|
||||
const email: string = res.locals.email;
|
||||
|
||||
console.log(`getting data for ${userId}`);
|
||||
// passed in accesstoken no matter what
|
||||
const { access_token } = req.query;
|
||||
|
||||
console.log(`getting data for ${email}`);
|
||||
|
||||
try {
|
||||
// return user details if it passed auth middleware
|
||||
const user = await UserService.getUserById(userId);
|
||||
const user = await UserService.getUserByEmail(email);
|
||||
|
||||
// if no user found, then go ahead and create user
|
||||
if (!user) {
|
||||
if (!access_token) {
|
||||
res.status(400).send("No access token provided");
|
||||
return;
|
||||
}
|
||||
|
||||
// get google user info from access token
|
||||
const userInfo: GoogleUserInfo =
|
||||
await UserService.getGoogleUserInfoWithAccessToken(
|
||||
access_token as string
|
||||
);
|
||||
|
||||
// create initial user model object
|
||||
const newUser = new User(
|
||||
userInfo.email,
|
||||
userInfo.verifiedEmail,
|
||||
userInfo.name,
|
||||
userInfo.given_name,
|
||||
userInfo.family_name,
|
||||
userInfo.picture,
|
||||
userInfo.locale
|
||||
);
|
||||
|
||||
// create user if not exists
|
||||
const insertResult = await UserService.createUserIfNotExists(newUser);
|
||||
|
||||
insertResult
|
||||
? res.status(200).send("User created")
|
||||
: res.status(500).send("Failed to create account, already exists");
|
||||
}
|
||||
|
||||
// otherwise, just return the user details
|
||||
res.status(200).send(user);
|
||||
} catch (error) {
|
||||
res
|
||||
.status(404)
|
||||
.send(`unable to find a matching document with id: ${userId}`);
|
||||
.send(`unable to find a matching document with email: ${email}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** DEPRECATED...USING THE SAME SIGN IN ROUTE TO CREATE */
|
||||
async function createUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { access_token } = req.query;
|
||||
|
||||
@@ -8,7 +8,27 @@ export class UserService {
|
||||
static async getUserById(userId: string) {
|
||||
const query = { _id: new ObjectId(userId) };
|
||||
|
||||
return (await collections.users?.findOne(query)) as unknown as User;
|
||||
const res = await collections.users?.findOne(query);
|
||||
|
||||
// exists
|
||||
if (res?._id) {
|
||||
return res as User;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static async getUserByEmail(email: string) {
|
||||
const query = { email };
|
||||
|
||||
const res = await collections.users?.findOne(query);
|
||||
|
||||
// exists
|
||||
if (res?._id) {
|
||||
return res as User;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static async createUserIfNotExists(newUser: User) {
|
||||
|
||||
@@ -9,7 +9,6 @@ export class User {
|
||||
public family_name: string,
|
||||
public picture: string,
|
||||
public locale: string,
|
||||
public _id?: ObjectId
|
||||
) // additional properties specific to our users collection
|
||||
{}
|
||||
public _id?: ObjectId // additional properties specific to our users collection
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -104,6 +104,7 @@
|
||||
"electron-store": "^8.0.1",
|
||||
"react": "^17.0.2",
|
||||
"react-dom": "^17.0.2",
|
||||
"react-icons": "^4.3.1",
|
||||
"react-query": "^3.34.16",
|
||||
"react-router-dom": "^6.2.2"
|
||||
}
|
||||
|
||||
@@ -13,17 +13,15 @@ export default function ProtectedRoute({
|
||||
const { isLoading, isError } = useGetUserDetails();
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
if (isError) {
|
||||
navigate("/login");
|
||||
}
|
||||
}, [isError]);
|
||||
|
||||
useEffect(() => {
|
||||
// console.log(window.electronAPI.store.get(STORE_ITEMS.AUTH_TOKENS));
|
||||
// nirvanaApi.getUserDetails();
|
||||
}, []);
|
||||
|
||||
if (isError) {
|
||||
navigate("/login");
|
||||
}
|
||||
|
||||
if (isLoading) return <span>please wait while we authenticate you</span>;
|
||||
|
||||
// if we can successfully get user details, we are good to continue
|
||||
|
||||
@@ -41,8 +41,8 @@ class NirvanaApi {
|
||||
}
|
||||
|
||||
user = {
|
||||
async getUserDetails(): Promise<User> {
|
||||
return await axios.get(localHost + `/users`, {
|
||||
async getUserDetails(accessToken: string): Promise<User> {
|
||||
return await axios.get(localHost + `/users?access_token=${accessToken}`, {
|
||||
headers: { Authorization: this._authToken },
|
||||
});
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { HashRouter, Route, Routes } from "react-router-dom";
|
||||
import { BrowserRouter, HashRouter, Route, Routes } from "react-router-dom";
|
||||
import { QueryClient, QueryClientProvider } from "react-query";
|
||||
|
||||
import Home from "./pages/Home";
|
||||
@@ -17,21 +17,19 @@ function NirvanaApp() {
|
||||
<>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<HashRouter>
|
||||
<div className="App">
|
||||
<Routes>
|
||||
<Route path="/" element={<Login />} />
|
||||
<Route
|
||||
path="/home"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<Home />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
{/* <Route exact path="/profile/create" component={Sit} />
|
||||
<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>
|
||||
</div>
|
||||
</Routes>
|
||||
</HashRouter>
|
||||
|
||||
<ReactQueryDevtools initialIsOpen={false} />
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
|
||||
|
||||
import Channels from "../../electron/constants";
|
||||
import { CircularProgress } from "@mui/material";
|
||||
import { FcGoogle } from "react-icons/fc";
|
||||
import Logo from "../../components/Logo";
|
||||
import { nirvanaApi } from "../../controller/nirvanaApi";
|
||||
import { useCreateUser } from "../../controller/index";
|
||||
@@ -10,10 +11,6 @@ import { useNavigate } from "react-router-dom";
|
||||
export default function Login() {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [isSignUp, setIsSignUp] = useState<boolean>(false);
|
||||
|
||||
const { mutateAsync } = useCreateUser();
|
||||
|
||||
const continueAuth = () => {
|
||||
setIsLoading(true);
|
||||
// send to main process
|
||||
@@ -32,11 +29,6 @@ export default function Login() {
|
||||
|
||||
nirvanaApi.setGoogleIdToken(id_token);
|
||||
|
||||
// create user if on sign up page
|
||||
if (isSignUp) {
|
||||
await mutateAsync(access_token);
|
||||
}
|
||||
|
||||
// now can go to home and get authenticated regardless of type of user
|
||||
navigate("/home");
|
||||
}
|
||||
@@ -62,19 +54,10 @@ export default function Login() {
|
||||
) : (
|
||||
<button
|
||||
onClick={continueAuth}
|
||||
className="text-white p-3 rounded shadow border border-white"
|
||||
className=" text-md text-slate-200 py-2 px-5 border border-gray-200 transition-all hover:bg-gray-200 hover:text-teal-500 rounded flex flex-row items-center space-x-2"
|
||||
>
|
||||
{isSignUp ? "Sign Up" : "Sign In"}
|
||||
</button>
|
||||
)}
|
||||
|
||||
{isSignUp ? (
|
||||
<button onClick={() => setIsSignUp(false)} className="text-slate-200">
|
||||
Sign In Here
|
||||
</button>
|
||||
) : (
|
||||
<button onClick={() => setIsSignUp(true)} className="text-slate-200">
|
||||
Create Account Here
|
||||
<FcGoogle className="text-lg" />
|
||||
<span>Continue with Google</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -6081,6 +6081,11 @@ react-dom@^17.0.2:
|
||||
object-assign "^4.1.1"
|
||||
scheduler "^0.20.2"
|
||||
|
||||
react-icons@^4.3.1:
|
||||
version "4.3.1"
|
||||
resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-4.3.1.tgz#2fa92aebbbc71f43d2db2ed1aed07361124e91ca"
|
||||
integrity sha512-cB10MXLTs3gVuXimblAdI71jrJx8njrJZmNMEMC+sQu5B/BIOmlsAjskdqpn81y8UBVEGuHODd7/ci5DvoSzTQ==
|
||||
|
||||
react-is@^16.13.1, react-is@^16.7.0:
|
||||
version "16.13.1"
|
||||
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
|
||||
|
||||
Reference in New Issue
Block a user