troubleshooting on user registration flow

This commit is contained in:
Mikayla Dobson
2022-12-01 18:49:51 -06:00
parent ba4b6e08c9
commit 671e250c60
9 changed files with 60 additions and 49 deletions

View File

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

View File

@@ -16,7 +16,7 @@ export default function Friends() {
const wrapper = async () => {
// HARD CODED
const result = await getFriendships(1);
const result = await getFriendships();
setFriends(result);
}

View File

@@ -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 (
<Page>
<AboutYou />
</Page>
)
const [displayed, setDisplayed] = useState<JSX.Element>(<AboutYou />);
const authContext = useAuthContext();
return displayed;
}

View File

@@ -23,7 +23,7 @@ export default function AboutYou() {
const authContext = useAuthContext();
const [form, setForm] = useState<JSX.Element[]>([<p key={v4()}>Loading content...</p>]);
const [input, setInput] = useState<IUser>(blankUser);
const [regSuccess, setRegSuccess] = useState<boolean>();
const [regSuccess, setRegSuccess] = useState<any>();
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 (
<Page>

View File

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

View File

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

View File

@@ -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 = `

View File

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

View File

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