Refactor UserContext more async

Separate signup and signin
This commit is contained in:
jwu-fisker
2021-01-06 18:23:38 -08:00
parent d812daa1ea
commit 31c33447fb
2 changed files with 43 additions and 38 deletions

View File

@@ -5,7 +5,7 @@ import useStyles from '../Styles';
import { useUserContext } from '../Contexts/UserContext'; import { useUserContext } from '../Contexts/UserContext';
export default function SignInForm() { export default function SignInForm() {
const { signUp, fetching, setError } = useUserContext(); const { signUp, signIn, fetching, setError } = useUserContext();
const classes = useStyles(); const classes = useStyles();
const emailEl = useRef(null); const emailEl = useRef(null);
const passwordEl = useRef(null); const passwordEl = useRef(null);
@@ -17,6 +17,7 @@ export default function SignInForm() {
const password = passwordEl.current.value; const password = passwordEl.current.value;
const confirm = confirmEl.current.value; const confirm = confirmEl.current.value;
await signUp(email, password, confirm); await signUp(email, password, confirm);
await signIn(email, password);
} }
catch (e) { catch (e) {
setError(e.message); setError(e.message);

View File

@@ -14,53 +14,57 @@ export const UserProvider = ({ children }) => {
if (!token) return; if (!token) return;
const { accessToken: { jwtToken }} = token; const { accessToken: { jwtToken }} = token;
const verifyToken = async (jwt) => { const verifyToken = async (jwt) => {
debugger; const result = await auth.verify(jwt);
const result = await auth.verify(jwtToken);
if (result.authenticated) { if (result.authenticated) {
setToken(token); setToken(token);
} else { } else {
console.log(result); console.log(result);
await signOut(); await signOut();
} }
}; };
verifyToken(jwtToken); verifyToken();
return () => {};
}, []); }, []);
const signIn = (username, password) => { const signIn = async (username, password) => {
if (!username) throw new Error('Email is required'); try {
if (!password) throw new Error('Password is required'); if (!username) throw new Error('Email is required');
setFetching(true); if (!password) throw new Error('Password is required');
setError(null);
return auth.signIn(username, password) setFetching(true);
.then((result) => { setError(null);
setFetching(false);
if (result.message) throw new Error(result.message); const result = await auth.signIn(username, password);
signedIn(result);
return result; if (result.message) throw new Error(result.message);
}) signedIn(result);
.catch((error) => { }
setError(error.message); catch (error) {
setFetching(false); setError(error.message);
return null; }
}); finally {
setFetching(false);
}
}; };
const signUp = (username, password, confirmPassword) => { const signUp = async (username, password, confirmPassword) => {
if (!username) throw new Error('Email is required'); try {
if (!password) throw new Error('Password is required'); if (!username) throw new Error('Email is required');
if (password !== confirmPassword) throw new Error('Passwords do not match'); if (!password) throw new Error('Password is required');
setFetching(true); if (password !== confirmPassword) throw new Error('Passwords do not match');
setError(null);
return auth.signUp(username, password) setFetching(true);
.then((result) => { setError(null);
if (result.message) throw new Error(result.message);
return signIn(username, password); const result = await auth.signUp(username, password);
}) if (result.message) throw new Error(result.message);
.catch((error) => { }
setError(error.message); catch (error) {
setFetching(false); setError(error.message);
return null; }
}); finally {
setFetching(false);
}
}; };
const signOut = async () => { const signOut = async () => {