Refactor UserContext more async
Separate signup and signin
This commit is contained in:
@@ -14,53 +14,57 @@ export const UserProvider = ({ children }) => {
|
||||
if (!token) return;
|
||||
const { accessToken: { jwtToken }} = token;
|
||||
const verifyToken = async (jwt) => {
|
||||
debugger;
|
||||
const result = await auth.verify(jwtToken);
|
||||
const result = await auth.verify(jwt);
|
||||
if (result.authenticated) {
|
||||
setToken(token);
|
||||
} else {
|
||||
console.log(result);
|
||||
await signOut();
|
||||
}
|
||||
};
|
||||
verifyToken(jwtToken);
|
||||
};
|
||||
verifyToken();
|
||||
return () => {};
|
||||
}, []);
|
||||
|
||||
const signIn = async (username, password) => {
|
||||
try {
|
||||
if (!username) throw new Error('Email is required');
|
||||
if (!password) throw new Error('Password is required');
|
||||
|
||||
setFetching(true);
|
||||
setError(null);
|
||||
|
||||
const result = await auth.signIn(username, password);
|
||||
|
||||
const signIn = (username, password) => {
|
||||
if (!username) throw new Error('Email is required');
|
||||
if (!password) throw new Error('Password is required');
|
||||
setFetching(true);
|
||||
setError(null);
|
||||
return auth.signIn(username, password)
|
||||
.then((result) => {
|
||||
setFetching(false);
|
||||
if (result.message) throw new Error(result.message);
|
||||
signedIn(result);
|
||||
return result;
|
||||
})
|
||||
.catch((error) => {
|
||||
setError(error.message);
|
||||
setFetching(false);
|
||||
return null;
|
||||
});
|
||||
if (result.message) throw new Error(result.message);
|
||||
signedIn(result);
|
||||
}
|
||||
catch (error) {
|
||||
setError(error.message);
|
||||
}
|
||||
finally {
|
||||
setFetching(false);
|
||||
}
|
||||
};
|
||||
|
||||
const signUp = (username, password, confirmPassword) => {
|
||||
if (!username) throw new Error('Email is required');
|
||||
if (!password) throw new Error('Password is required');
|
||||
if (password !== confirmPassword) throw new Error('Passwords do not match');
|
||||
setFetching(true);
|
||||
setError(null);
|
||||
return auth.signUp(username, password)
|
||||
.then((result) => {
|
||||
if (result.message) throw new Error(result.message);
|
||||
return signIn(username, password);
|
||||
})
|
||||
.catch((error) => {
|
||||
setError(error.message);
|
||||
setFetching(false);
|
||||
return null;
|
||||
});
|
||||
const signUp = async (username, password, confirmPassword) => {
|
||||
try {
|
||||
if (!username) throw new Error('Email is required');
|
||||
if (!password) throw new Error('Password is required');
|
||||
if (password !== confirmPassword) throw new Error('Passwords do not match');
|
||||
|
||||
setFetching(true);
|
||||
setError(null);
|
||||
|
||||
const result = await auth.signUp(username, password);
|
||||
if (result.message) throw new Error(result.message);
|
||||
}
|
||||
catch (error) {
|
||||
setError(error.message);
|
||||
}
|
||||
finally {
|
||||
setFetching(false);
|
||||
}
|
||||
};
|
||||
|
||||
const signOut = async () => {
|
||||
|
||||
Reference in New Issue
Block a user