From 671e250c60b68a83ca4b3f7ce7cfc8af2db7d789 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Thu, 1 Dec 2022 18:49:51 -0600 Subject: [PATCH] troubleshooting on user registration flow --- client/src/App.tsx | 10 +++-- client/src/components/derived/Friends.tsx | 2 +- .../src/components/pages/Register/index.tsx | 11 ++--- .../pages/Register/register.aboutyou.tsx | 10 +++-- client/src/util/apiUtils.tsx | 2 + server/auth/index.ts | 40 +++++++++---------- server/models/collection.ts | 1 - server/models/user.ts | 6 +-- server/routes/auth.ts | 27 +++++++------ 9 files changed, 60 insertions(+), 49 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index fd8d6eb..df8859f 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -17,9 +17,13 @@ function App() { useEffect(() => { const wrapper = async () => { - const result: IAuthContext | undefined = await checkCredientials(); - if (result == undefined) setUser({ user: undefined }); - setUser(result!); + try { + const result: IAuthContext | undefined = await checkCredientials(); + if (result == undefined) setUser({ user: undefined }); + setUser(result!); + } catch(e) { + console.error(e); + } } wrapper(); diff --git a/client/src/components/derived/Friends.tsx b/client/src/components/derived/Friends.tsx index 652452d..e97b1bd 100644 --- a/client/src/components/derived/Friends.tsx +++ b/client/src/components/derived/Friends.tsx @@ -16,7 +16,7 @@ export default function Friends() { const wrapper = async () => { // HARD CODED - const result = await getFriendships(1); + const result = await getFriendships(); setFriends(result); } diff --git a/client/src/components/pages/Register/index.tsx b/client/src/components/pages/Register/index.tsx index 1543b25..eb0fa70 100644 --- a/client/src/components/pages/Register/index.tsx +++ b/client/src/components/pages/Register/index.tsx @@ -1,10 +1,11 @@ +import { useState } from "react"; +import { useAuthContext } from "../../../context/AuthContext"; import { Page } from "../../ui"; import AboutYou from "./register.aboutyou"; export default function Register() { - return ( - - - - ) + const [displayed, setDisplayed] = useState(); + const authContext = useAuthContext(); + + return displayed; } \ No newline at end of file diff --git a/client/src/components/pages/Register/register.aboutyou.tsx b/client/src/components/pages/Register/register.aboutyou.tsx index 87ec893..57779fe 100644 --- a/client/src/components/pages/Register/register.aboutyou.tsx +++ b/client/src/components/pages/Register/register.aboutyou.tsx @@ -23,7 +23,7 @@ export default function AboutYou() { const authContext = useAuthContext(); const [form, setForm] = useState([

Loading content...

]); const [input, setInput] = useState(blankUser); - const [regSuccess, setRegSuccess] = useState(); + const [regSuccess, setRegSuccess] = useState(); const getFormState = useCallback((received: IUser) => { setInput(received); @@ -40,11 +40,15 @@ export default function AboutYou() { async function handleRegister() { const res = await attemptRegister(input); + console.log(res); setRegSuccess(res); } async function unwrapLogin() { - const login = await attemptLogin({ email: input.email, password: input.password as string } as IUserAuth); + const data: IUserAuth = { email: input.email, password: input.password || "" } + console.log(data); + const login = await attemptLogin(data); + console.log(login); authContext.user = login.user; navigate('/'); } @@ -55,7 +59,7 @@ export default function AboutYou() { useEffect(() => { if (regSuccess) unwrapLogin(); - }, [regSuccess, handleRegister]) + }, [regSuccess]) return ( diff --git a/client/src/util/apiUtils.tsx b/client/src/util/apiUtils.tsx index 4dc58a8..b734363 100644 --- a/client/src/util/apiUtils.tsx +++ b/client/src/util/apiUtils.tsx @@ -19,6 +19,8 @@ export const attemptLogin = async (data: IUserAuth): Promise => { data: data }); + console.log(response); + const result = Promise.resolve(response.data); return result; } catch (e: any) { diff --git a/server/auth/index.ts b/server/auth/index.ts index ac361dc..0f717d3 100644 --- a/server/auth/index.ts +++ b/server/auth/index.ts @@ -18,33 +18,33 @@ export default class AuthService { data.isadmin = false; try { + let receivedUser: IUser | undefined; + + // not allowed to use email address that already exists const user = await UserInstance.getOneByEmail(email); if (user) throw createError('409', 'Email already in use'); // hash password and create new user record - bcrypt.genSalt(10, (err, salt) => { + const salt = await bcrypt.genSalt(); + bcrypt.hash(password!, salt, async (err, hash) => { if (err) throw err; - bcrypt.hash(password!, salt, async (err, hash) => { - if (err) throw err; - const newData = { - ...data, - password: hash - } - - const result: IUser = await UserInstance.post(newData); + const newData = { + ...data, + password: hash + } - // basic profile setup - await CollectionInstance.post({ - name: `${data.firstname}'s Collection`, - active: true, - ismaincollection: true, - ownerid: result.id!.toString(), - datecreated: now, - datemodified: now - }); + const res: IUser | null = await UserInstance.post(newData); + if (res) receivedUser = res; - return result; - }) + // basic profile setup + res && await CollectionInstance.post({ + name: `${data.firstname}'s Collection`, + active: true, + ismaincollection: true, + ownerid: res.id!.toString(), + datecreated: now, + datemodified: now + }); }) return true; diff --git a/server/models/collection.ts b/server/models/collection.ts index cb85259..d1b9e86 100644 --- a/server/models/collection.ts +++ b/server/models/collection.ts @@ -47,7 +47,6 @@ export class Collection { async post(data: ICollection) { console.log('new default collection'); - console.log(data); const { name, active, ismaincollection, ownerid } = data; try { const statement = ` diff --git a/server/models/user.ts b/server/models/user.ts index f9c7bad..5fa1ed0 100644 --- a/server/models/user.ts +++ b/server/models/user.ts @@ -72,8 +72,6 @@ export class User { async post(data: IUser) { const { firstname, lastname, handle, email, password, active, isadmin } = data; - const datecreated = now; - const datemodified = now; try { const statement = ` @@ -83,9 +81,9 @@ export class User { VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *; `; - const params = [firstname, lastname, handle, email, password, active, isadmin, datecreated, datemodified]; + const params = [firstname, lastname, handle, email, password, active, isadmin, now, now]; const result = await pool.query(statement, params); - if (result.rows.length) return result.rows[0]; + if (result.rows.length) return result.rows[0] as IUser; return null; } catch (error: any) { throw new Error(error); diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 247a7d4..2b3717a 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -15,7 +15,7 @@ export const authRoute = (app: Express, passport: PassportStatic) => { app.use('/auth', router); router.get('/', restrictAccess, (req, res, next) => { - if (!req.user) return; + if (!req.user) res.send({ user: undefined }); // @ts-ignore: does not recognize structure of req.user const { user } = req.user; @@ -55,25 +55,28 @@ export const authRoute = (app: Express, passport: PassportStatic) => { } }) + router.post('/register', async (req, res, next) => { + try { + const data: IUser = req.body; + const response = await AuthInstance.register(data); + if (!response) res.sendStatus(400); + // const login = await AuthInstance.login({ email: data.email, password: data.password! }); + // console.log(login); + res.status(200).redirect('/'); + } catch(e) { + next(e); + } + }) + router.delete('/logout', async (req, res, next) => { try { req.session.destroy((err) => { if (err) throw err; }) res.clearCookie('userid'); - res.status(204).send({ message: "Logout successful", success: true }); + res.status(204).redirect('/'); } catch(e) { next(e); } }); - - router.post('/register', async (req, res, next) => { - try { - const data: IUser = req.body; - const response = await AuthInstance.register(data); - res.status(200).send(response); - } catch(e) { - next(e); - } - }) } \ No newline at end of file