login.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { NextPage } from "next";
  2. import { useContext, useEffect } from "react";
  3. import Layout from "../components/Layout";
  4. import { UserContext } from "../context/UserContext";
  5. import useForm from "../hooks/useForm";
  6. import { ApiMethods, ApiPaths } from "../utils/api";
  7. const Login: NextPage = () => {
  8. const context = useContext(UserContext);
  9. const { error, submitHandler, success } = useForm({
  10. path: ApiPaths.Login,
  11. method: ApiMethods.POST,
  12. });
  13. useEffect(() => {
  14. if (success && context) {
  15. context.setLogged(true);
  16. }
  17. }, [success, context]);
  18. return (
  19. <Layout>
  20. <form className="form" onSubmit={submitHandler}>
  21. <div className="form-group">
  22. <label htmlFor="email" className="form-label">
  23. Email
  24. </label>
  25. <input
  26. type="email"
  27. name="email"
  28. id="email"
  29. className="form-input"
  30. placeholder="monkey@gmail.com"
  31. required
  32. />
  33. </div>
  34. <div className="form-group">
  35. <label htmlFor="password" className="form-label">
  36. Password
  37. </label>
  38. <input
  39. type="password"
  40. name="password"
  41. id="password"
  42. className="form-input"
  43. placeholder="••••••"
  44. required
  45. />
  46. </div>
  47. <button type="submit" className="form-button">
  48. Log in
  49. </button>
  50. {error && (
  51. <div role="alert" className="form-alert-msg">
  52. {error.message}
  53. </div>
  54. )}
  55. </form>
  56. </Layout>
  57. );
  58. };
  59. export default Login;