diff --git a/client/index.html b/client/index.html index e0d1c84..0b3d01c 100644 --- a/client/index.html +++ b/client/index.html @@ -4,7 +4,7 @@ - Vite + React + TS + RECIPIN | Personal Recipe Manager
diff --git a/client/src/components/pages/Register/register.aboutyou.tsx b/client/src/components/pages/Register/register.aboutyou.tsx index 5737255..87ec893 100644 --- a/client/src/components/pages/Register/register.aboutyou.tsx +++ b/client/src/components/pages/Register/register.aboutyou.tsx @@ -1,23 +1,29 @@ import { useCallback, useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import { v4 } from "uuid"; -import { IUser } from "../../../schemas"; +import { useAuthContext } from "../../../context/AuthContext"; +import { IUser, IUserAuth } from "../../../schemas"; import { attemptLogin, attemptRegister } from "../../../util/apiUtils"; import { Button, Page, Panel } from "../../ui"; import Divider from "../../ui/Divider"; import Form, { FormConfig } from "../../ui/Form"; +const blankUser: IUser = { + firstname: '', + lastname: '', + handle: '', + email: '', + password: '', + active: true, + isadmin: false +} + export default function AboutYou() { const navigate = useNavigate(); + const authContext = useAuthContext(); const [form, setForm] = useState([

Loading content...

]); - const [input, setInput] = useState({ - firstname: '', - lastname: '', - handle: '', - email: '', - password: '', - active: true - }); + const [input, setInput] = useState(blankUser); + const [regSuccess, setRegSuccess] = useState(); const getFormState = useCallback((received: IUser) => { setInput(received); @@ -32,14 +38,24 @@ export default function AboutYou() { getState: getFormState } + async function handleRegister() { + const res = await attemptRegister(input); + setRegSuccess(res); + } + + async function unwrapLogin() { + const login = await attemptLogin({ email: input.email, password: input.password as string } as IUserAuth); + authContext.user = login.user; + navigate('/'); + } + useEffect(() => { setForm(new Form(formConfig).mount()); }, []) - const handleRegister = async () => { - const result = await attemptRegister(input); - await attemptLogin({ email: result.email, password: result.password }).then(() => navigate('/')); - } + useEffect(() => { + if (regSuccess) unwrapLogin(); + }, [regSuccess, handleRegister]) return ( diff --git a/client/src/util/apiUtils.tsx b/client/src/util/apiUtils.tsx index 49f25e5..4dc58a8 100644 --- a/client/src/util/apiUtils.tsx +++ b/client/src/util/apiUtils.tsx @@ -19,7 +19,7 @@ export const attemptLogin = async (data: IUserAuth): Promise => { data: data }); - const result: Promise = Promise.resolve(response.data); + const result = Promise.resolve(response.data); return result; } catch (e: any) { throw e; diff --git a/server/auth/index.ts b/server/auth/index.ts index d29214c..ac361dc 100644 --- a/server/auth/index.ts +++ b/server/auth/index.ts @@ -22,7 +22,7 @@ export default class AuthService { if (user) throw createError('409', 'Email already in use'); // hash password and create new user record - return bcrypt.genSalt(10, (err, salt) => { + bcrypt.genSalt(10, (err, salt) => { if (err) throw err; bcrypt.hash(password!, salt, async (err, hash) => { if (err) throw err; @@ -46,6 +46,8 @@ export default class AuthService { return result; }) }) + + return true; } catch (e: any) { throw new Error(e); } diff --git a/server/db/seed.ts b/server/db/seed.ts index da572ff..638a9a1 100644 --- a/server/db/seed.ts +++ b/server/db/seed.ts @@ -8,6 +8,8 @@ import { appRoot } from "../appRoot"; dotenv.config(); (async function() { + console.clear(); + const setRole = ` SET ROLE postgres; DROP SCHEMA IF EXISTS recipin CASCADE; diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 3e7594c..247a7d4 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -15,8 +15,10 @@ export const authRoute = (app: Express, passport: PassportStatic) => { app.use('/auth', router); router.get('/', restrictAccess, (req, res, next) => { + if (!req.user) return; + // @ts-ignore: does not recognize structure of req.user - const user = req.user?.user; + const { user } = req.user; const userData = { id: user.id, firstname: user.firstname,