Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2: Fixing sign up error messages display #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion frontend/src/hooks/useLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useAuthContext } from "./useAuthContext";

export const useLogin = () => {
const [isLoading, setIsLoading] = useState(null);
const [error,setError] = useState(null)
const { dispatch } = useAuthContext();

const login = async (email, password) => {
Expand All @@ -16,6 +17,7 @@ export const useLogin = () => {
const json = await response.json();

if (!response.ok) {
setError(json.error)
setIsLoading(false);
}
if (response.ok) {
Expand All @@ -29,5 +31,5 @@ export const useLogin = () => {
}
};

return { login, isLoading };
return { login, isLoading ,error};
};
5 changes: 3 additions & 2 deletions frontend/src/hooks/useSignup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useAuthContext } from "./useAuthContext";
export const useSignup = () => {
const [isLoading, setIsLoading] = useState(null);
const { dispatch } = useAuthContext();

const [error, setError] = useState(null);
const signup = async (email, password) => {
setIsLoading(true);

Expand All @@ -16,6 +16,7 @@ export const useSignup = () => {
const json = await response.json();

if (!response.ok) {
setError(json.error);
setIsLoading(false);
}
if (response.ok) {
Expand All @@ -29,5 +30,5 @@ export const useSignup = () => {
}
};

return { signup, isLoading };
return { signup, isLoading, error };
};
5 changes: 5 additions & 0 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@ form.signup, form.login {
padding: 20px;
background: #fff;
border-radius: 4px;
}

.error-element{
color: red;
font-weight: 500;
}
4 changes: 2 additions & 2 deletions frontend/src/pages/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useLogin } from "../hooks/useLogin";
const Login = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const { login, isLoading } = useLogin();
const { login, isLoading, error } = useLogin();

const handleSubmit = async (e) => {
e.preventDefault();
Expand All @@ -28,7 +28,7 @@ const Login = () => {
/>

<button disabled={isLoading}>Log in</button>
{/* Display error message if there was an issue with the login */}
{error && <div className="error-element">{error}</div>}
</form>
);
};
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/Signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSignup } from "../hooks/useSignup";
const Signup = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const { signup, isLoading } = useSignup();
const { signup, isLoading, error } = useSignup();

const handleSubmit = async (e) => {
e.preventDefault();
Expand All @@ -30,7 +30,7 @@ const Signup = () => {
/>

<button disabled={isLoading}>Sign up</button>
{/* Display error message if there was an issue with the Sign up */}
{error && <div className="error-element">{error}</div>}
</form>
);
};
Expand Down