From 3831f110a3f238f1234d5253b7a78373184aa26d Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Mon, 26 Dec 2022 18:22:12 -0600 Subject: [PATCH] axios problems --- client/src/App.tsx | 4 +-- client/src/components/pages/Browser.tsx | 18 ------------- .../components/pages/Register/collection.tsx | 20 ++++++++------- client/src/components/ui/Browser.tsx | 25 +++++++++++++++++++ client/src/components/ui/Form.tsx | 6 +++-- client/src/util/Protect.tsx | 4 +-- client/src/util/apiUtils.tsx | 8 +++--- server/auth/index.ts | 14 ++++++----- server/auth/middlewares.ts | 2 -- server/routes/auth.ts | 2 +- server/routes/collection.ts | 2 ++ 11 files changed, 59 insertions(+), 46 deletions(-) delete mode 100644 client/src/components/pages/Browser.tsx create mode 100644 client/src/components/ui/Browser.tsx diff --git a/client/src/App.tsx b/client/src/App.tsx index c15c2a7..07883b3 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -7,7 +7,7 @@ import { IUser } from './schemas'; // pages, ui, styles import Subscriptions from './components/pages/Subscriptions/Subscriptions'; -import Browser from './components/pages/Browser'; +import Browser from './components/ui/Browser'; import Collection from './components/pages/Collection'; import Login from './components/pages/Login'; import Profile from './components/pages/Profile'; @@ -61,7 +61,7 @@ function App() { } /> } /> } /> - } /> + {}} />} /> } /> } /> } /> diff --git a/client/src/components/pages/Browser.tsx b/client/src/components/pages/Browser.tsx deleted file mode 100644 index c7a9499..0000000 --- a/client/src/components/pages/Browser.tsx +++ /dev/null @@ -1,18 +0,0 @@ -import Protect from "../../util/Protect"; -import { Button, Page } from "../ui"; - -export default function Browser() { - return ( - -

Search recipes

-
- - -
- - {/* divider */} - - {/* recipe cards, or "no recipes matching your search" */} -
- ) -} \ No newline at end of file diff --git a/client/src/components/pages/Register/collection.tsx b/client/src/components/pages/Register/collection.tsx index 1de24a6..ee97dfc 100644 --- a/client/src/components/pages/Register/collection.tsx +++ b/client/src/components/pages/Register/collection.tsx @@ -18,6 +18,14 @@ const InitialCollection: RegisterVariantType = ({ transitionDisplay, receiveChan setUser(login.user); } + useEffect(() => { + if (input) { + setTimeout(() => { + unwrapLogin(input); + }, 750); + } + }, []) + const handleClick = async () => { if (!user) return; const collection: ICollection = { @@ -29,20 +37,14 @@ const InitialCollection: RegisterVariantType = ({ transitionDisplay, receiveChan datemodified: now } + console.log(collection); + const result = await createNewCollection(collection); console.log(result); if (result) transitionDisplay(VariantLabel.AddFriends); } - useEffect(() => { - if (input) { - setTimeout(() => { - unwrapLogin(input); - }, 2000); - } - }, []) - - useEffect(() => { + useEffect(() => { if (user && receiveChange) { receiveChange(user); setView( diff --git a/client/src/components/ui/Browser.tsx b/client/src/components/ui/Browser.tsx new file mode 100644 index 0000000..11545ae --- /dev/null +++ b/client/src/components/ui/Browser.tsx @@ -0,0 +1,25 @@ +import { FC, useEffect, useState } from "react"; +import Protect from "../../util/Protect"; +import Form from "./Form"; + +interface BrowserProps { + children?: JSX.Element[] + header: string + searchFunction: (...params: any) => any +} + +const Browser: FC = ({ children, header, searchFunction }) => { + const [form, setForm] = useState(); + + useEffect(() => { + + }) + + return ( + +

{header}

+
+ ) +} + +export default Browser; \ No newline at end of file diff --git a/client/src/components/ui/Form.tsx b/client/src/components/ui/Form.tsx index 2904f96..f0541a8 100644 --- a/client/src/components/ui/Form.tsx +++ b/client/src/components/ui/Form.tsx @@ -1,4 +1,4 @@ -import { ChangeEvent } from "react"; +import { ChangeEvent, FC } from "react"; import { v4 } from 'uuid'; import RichText from "./RichText"; @@ -20,7 +20,7 @@ export interface FormConfig { extraStyles?: string } -export default class Form{ +export default class Form { private parent: string; private labels: string[]; private keys: string[]; @@ -39,6 +39,8 @@ export default class Form{ this.getState = config.getState; this.richTextInitialValue = config.richTextInitialValue; this.extraStyles = config.extraStyles; + + this.mount(); } update(e: ChangeEvent, idx: number) { diff --git a/client/src/util/Protect.tsx b/client/src/util/Protect.tsx index 93d1c5d..3e4042c 100644 --- a/client/src/util/Protect.tsx +++ b/client/src/util/Protect.tsx @@ -4,7 +4,7 @@ import Divider from "../components/ui/Divider"; import { useAuthContext } from "../context/AuthContext"; import { ProtectPortal } from "./types"; -const Protect: ProtectPortal = ({ children = <> }) => { +const Protect: ProtectPortal = ({ children }) => { const { user } = useAuthContext(); const navigate = useNavigate(); @@ -22,7 +22,7 @@ const Protect: ProtectPortal = ({ children = <> }) => { } else { return ( - { children } + { children || <> } ) } diff --git a/client/src/util/apiUtils.tsx b/client/src/util/apiUtils.tsx index 19d3c62..8653762 100644 --- a/client/src/util/apiUtils.tsx +++ b/client/src/util/apiUtils.tsx @@ -3,7 +3,7 @@ import { IAuthContext } from "../context/AuthContext"; import axios from "axios"; const API = import.meta.env.APISTRING || "http://localhost:8080"; -axios.defaults.withCredentials = true; +axios.defaults.withCredentials = false; axios.defaults.headers['Content-Type'] = 'application/json'; export const getBaseAPI = async () => { @@ -57,7 +57,7 @@ export const attemptRegister = async (body: IUser) => { const response = await axios({ method: "POST", url: API + '/auth/register', - data: JSON.stringify(body) + data: body }) return Promise.resolve(response.data); @@ -72,8 +72,8 @@ export const createNewCollection = async (body: ICollection) => { const response = await axios({ method: "POST", url: API + '/collection', - data: JSON.stringify(body) - }); + data: body + }) return Promise.resolve(response.data); } catch (e: any) { diff --git a/server/auth/index.ts b/server/auth/index.ts index d7019e4..2ad7ab1 100644 --- a/server/auth/index.ts +++ b/server/auth/index.ts @@ -9,8 +9,6 @@ const UserInstance = new User(); export default class AuthService { // methods for local strategies async register(data: IUser) { - const { email, password } = data; - data.datecreated = now; data.datemodified = now; data.active = true; @@ -18,19 +16,23 @@ export default class AuthService { try { // not allowed to use email address that already exists - const user = await UserInstance.getOneByEmail(email); + const user = await UserInstance.getOneByEmail(data.email); + if (user) throw createError('409', 'Email already in use'); // hash password and create new user record - const salt = await bcrypt.genSalt(); - bcrypt.hash(password!, salt, async (err, hash) => { + const salt = await bcrypt.genSalt(12); + console.log(salt); + console.log(data.password); + + bcrypt.hash(data.password!, salt, (err, hash) => { if (err) throw err; const newData = { ...data, password: hash } - await UserInstance.post(newData); + UserInstance.post(newData); }) return true; diff --git a/server/auth/middlewares.ts b/server/auth/middlewares.ts index 23f6a33..6c8451b 100644 --- a/server/auth/middlewares.ts +++ b/server/auth/middlewares.ts @@ -4,8 +4,6 @@ import { StatusCode } from "../util/types"; export function restrictAccess(req: Request, res: Response, next: NextFunction) { if (req.isAuthenticated()) { next(); - } else { - res.status(StatusCode.Forbidden).send({ ok: false, user: undefined }) } } diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 6cb2fcb..82d8a54 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -57,7 +57,7 @@ export const authRoute = (app: Express, passport: PassportStatic) => { router.post('/register', async (req, res, next) => { try { - const data: IUser = req.body; + const data = req.body; const response = await AuthInstance.register(data); if (!response) res.status(400).send({ ok: false }); res.status(200).send({ ok: true }); diff --git a/server/routes/collection.ts b/server/routes/collection.ts index 1c1d14a..8c835b8 100644 --- a/server/routes/collection.ts +++ b/server/routes/collection.ts @@ -30,6 +30,8 @@ export const collectionRoute = (app: Express) => { router.post('/', restrictAccess, async (req, res, next) => { const data = req.body; + console.log(data); + try { const result = await CollectionInstance.post(data); res.status(result.code).send(result.data);