Fix sign up form bug

This commit is contained in:
jwu-fisker
2021-01-08 15:06:19 -08:00
parent 26e25a5b23
commit 1a7ad4c2a2
5 changed files with 54 additions and 15 deletions

View File

@@ -26,6 +26,8 @@ export const UserProvider = ({ children }) => {
}, []);
const signIn = async (username, password) => {
let result = null;
try {
if (!username) throw new Error('Email is required');
if (!password) throw new Error('Password is required');
@@ -33,7 +35,7 @@ export const UserProvider = ({ children }) => {
setFetching(true);
setError(null);
const result = await auth.signIn(username, password);
result = await auth.signIn(username, password);
if (result.message) throw new Error(result.message);
signedIn(result);
@@ -44,9 +46,13 @@ export const UserProvider = ({ children }) => {
finally {
setFetching(false);
}
return result;
};
const signUp = async (username, password, confirmPassword) => {
let result = null;
try {
if (!username) throw new Error('Email is required');
if (!password) throw new Error('Password is required');
@@ -55,7 +61,7 @@ export const UserProvider = ({ children }) => {
setFetching(true);
setError(null);
const result = await auth.signUp(username, password);
result = await auth.signUp(username, password);
if (result.message) throw new Error(result.message);
}
catch (error) {
@@ -64,6 +70,26 @@ export const UserProvider = ({ children }) => {
finally {
setFetching(false);
}
return result;
};
const signUpAndIn = async (username, password, confirmPassword) => {
let result = null;
try {
result = await signUp(username, password, confirmPassword);
if (result.message) throw new Error(result.message);
result = await signIn(username, password);
}
catch (error) {
setError(error.message);
}
finally {
setFetching(false);
}
return result;
};
const signOut = async () => {
@@ -86,6 +112,7 @@ export const UserProvider = ({ children }) => {
setError,
signIn,
signUp,
signUpAndIn,
signOut,
}}>
{children}