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
|
"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;
|
||||||
|
const email = ticket.getPayload()?.email;
|
||||||
|
|
||||||
// used in subsequent handlers
|
// used in subsequent handlers
|
||||||
res.locals.userId = userId;
|
res.locals.userId = userId;
|
||||||
|
res.locals.email = email;
|
||||||
|
|
||||||
console.log(userId);
|
console.log(userId);
|
||||||
|
|
||||||
|
|||||||
@@ -24,22 +24,59 @@ export default function getUserRoutes() {
|
|||||||
* create user if doesn't exist
|
* create user if doesn't exist
|
||||||
*/
|
*/
|
||||||
async function getUserDetails(req: Request, res: Response) {
|
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 {
|
try {
|
||||||
// return user details if it passed auth middleware
|
// 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);
|
res.status(200).send(user);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res
|
res
|
||||||
.status(404)
|
.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) {
|
async function createUser(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const { access_token } = req.query;
|
const { access_token } = req.query;
|
||||||
|
|||||||
@@ -8,7 +8,27 @@ export class UserService {
|
|||||||
static async getUserById(userId: string) {
|
static async getUserById(userId: string) {
|
||||||
const query = { _id: new ObjectId(userId) };
|
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) {
|
static async createUserIfNotExists(newUser: User) {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ export class User {
|
|||||||
public family_name: string,
|
public family_name: string,
|
||||||
public picture: string,
|
public picture: string,
|
||||||
public locale: string,
|
public locale: string,
|
||||||
public _id?: ObjectId
|
public _id?: ObjectId // additional properties specific to our users collection
|
||||||
) // additional properties specific to our users collection
|
) {}
|
||||||
{}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,6 +104,7 @@
|
|||||||
"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",
|
||||||
|
"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"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,17 +13,15 @@ export default function ProtectedRoute({
|
|||||||
const { isLoading, isError } = useGetUserDetails();
|
const { isLoading, isError } = useGetUserDetails();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (isError) {
|
|
||||||
navigate("/login");
|
|
||||||
}
|
|
||||||
}, [isError]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// console.log(window.electronAPI.store.get(STORE_ITEMS.AUTH_TOKENS));
|
// console.log(window.electronAPI.store.get(STORE_ITEMS.AUTH_TOKENS));
|
||||||
// nirvanaApi.getUserDetails();
|
// nirvanaApi.getUserDetails();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
if (isError) {
|
||||||
|
navigate("/login");
|
||||||
|
}
|
||||||
|
|
||||||
if (isLoading) return <span>please wait while we authenticate you</span>;
|
if (isLoading) return <span>please wait while we authenticate you</span>;
|
||||||
|
|
||||||
// if we can successfully get user details, we are good to continue
|
// if we can successfully get user details, we are good to continue
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ class NirvanaApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
user = {
|
user = {
|
||||||
async getUserDetails(): Promise<User> {
|
async getUserDetails(accessToken: string): Promise<User> {
|
||||||
return await axios.get(localHost + `/users`, {
|
return await axios.get(localHost + `/users?access_token=${accessToken}`, {
|
||||||
headers: { Authorization: this._authToken },
|
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 { QueryClient, QueryClientProvider } from "react-query";
|
||||||
|
|
||||||
import Home from "./pages/Home";
|
import Home from "./pages/Home";
|
||||||
@@ -17,21 +17,19 @@ function NirvanaApp() {
|
|||||||
<>
|
<>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<HashRouter>
|
<HashRouter>
|
||||||
<div className="App">
|
<Routes>
|
||||||
<Routes>
|
<Route path="/" element={<Login />} />
|
||||||
<Route path="/" element={<Login />} />
|
<Route
|
||||||
<Route
|
path="home"
|
||||||
path="/home"
|
element={
|
||||||
element={
|
<ProtectedRoute>
|
||||||
<ProtectedRoute>
|
<Home />
|
||||||
<Home />
|
</ProtectedRoute>
|
||||||
</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>
|
||||||
</div>
|
|
||||||
</HashRouter>
|
</HashRouter>
|
||||||
|
|
||||||
<ReactQueryDevtools initialIsOpen={false} />
|
<ReactQueryDevtools initialIsOpen={false} />
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
|
|||||||
|
|
||||||
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 Logo from "../../components/Logo";
|
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";
|
||||||
@@ -10,10 +11,6 @@ import { useNavigate } from "react-router-dom";
|
|||||||
export default function Login() {
|
export default function Login() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const [isSignUp, setIsSignUp] = useState<boolean>(false);
|
|
||||||
|
|
||||||
const { mutateAsync } = useCreateUser();
|
|
||||||
|
|
||||||
const continueAuth = () => {
|
const continueAuth = () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
// send to main process
|
// send to main process
|
||||||
@@ -32,11 +29,6 @@ export default function Login() {
|
|||||||
|
|
||||||
nirvanaApi.setGoogleIdToken(id_token);
|
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
|
// now can go to home and get authenticated regardless of type of user
|
||||||
navigate("/home");
|
navigate("/home");
|
||||||
}
|
}
|
||||||
@@ -62,19 +54,10 @@ export default function Login() {
|
|||||||
) : (
|
) : (
|
||||||
<button
|
<button
|
||||||
onClick={continueAuth}
|
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"}
|
<FcGoogle className="text-lg" />
|
||||||
</button>
|
<span>Continue with Google</span>
|
||||||
)}
|
|
||||||
|
|
||||||
{isSignUp ? (
|
|
||||||
<button onClick={() => setIsSignUp(false)} className="text-slate-200">
|
|
||||||
Sign In Here
|
|
||||||
</button>
|
|
||||||
) : (
|
|
||||||
<button onClick={() => setIsSignUp(true)} className="text-slate-200">
|
|
||||||
Create Account Here
|
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6081,6 +6081,11 @@ react-dom@^17.0.2:
|
|||||||
object-assign "^4.1.1"
|
object-assign "^4.1.1"
|
||||||
scheduler "^0.20.2"
|
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:
|
react-is@^16.13.1, react-is@^16.7.0:
|
||||||
version "16.13.1"
|
version "16.13.1"
|
||||||
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
|
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
|
||||||
|
|||||||
Reference in New Issue
Block a user