preparing to checkout

This commit is contained in:
Mikayla Dobson
2022-11-30 18:33:40 -06:00
parent 9d0c8a8782
commit ba4b6e08c9
6 changed files with 39 additions and 17 deletions

View File

@@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<title>RECIPIN | Personal Recipe Manager</title>
</head>
<body>
<div id="root"></div>

View File

@@ -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<JSX.Element[]>([<p key={v4()}>Loading content...</p>]);
const [input, setInput] = useState<IUser>({
firstname: '',
lastname: '',
handle: '',
email: '',
password: '',
active: true
});
const [input, setInput] = useState<IUser>(blankUser);
const [regSuccess, setRegSuccess] = useState<boolean>();
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<IUser>(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 (
<Page>

View File

@@ -19,7 +19,7 @@ export const attemptLogin = async (data: IUserAuth): Promise<IAuthContext> => {
data: data
});
const result: Promise<IAuthContext> = Promise.resolve(response.data);
const result = Promise.resolve(response.data);
return result;
} catch (e: any) {
throw e;

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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,