| [0c86e77] | 1 | import { useState } from "react";
|
|---|
| 2 | import { useNavigate } from "react-router";
|
|---|
| 3 | import axiosInstance, { scheduleTokenRefresh } from "./api/axiosInstance";
|
|---|
| 4 | import { useAuth } from "./context/authContext";
|
|---|
| 5 | import type { User, UserResponse } from "./types";
|
|---|
| 6 |
|
|---|
| 7 | const Register = () => {
|
|---|
| [4022db1] | 8 | const { setUser } = useAuth();
|
|---|
| [0c86e77] | 9 | const [username, setUsername] = useState("");
|
|---|
| 10 | const [password, setPassword] = useState("");
|
|---|
| 11 | const [fullname, setFullname] = useState("");
|
|---|
| 12 | const [email, setEmail] = useState("");
|
|---|
| 13 | const [profilePhoto, setProfilePhoto] = useState<string | null>(null);
|
|---|
| 14 |
|
|---|
| 15 | const navigate = useNavigate();
|
|---|
| 16 |
|
|---|
| [24a2a2a] | 17 | const handleRegister = async (e: React.FormEvent) => {
|
|---|
| 18 | e.preventDefault();
|
|---|
| [0c86e77] | 19 | const newUser: User = {
|
|---|
| 20 | username,
|
|---|
| 21 | fullName: fullname,
|
|---|
| 22 | email: email,
|
|---|
| 23 | profilePhoto: profilePhoto,
|
|---|
| 24 | };
|
|---|
| 25 | try {
|
|---|
| 26 | const response = await axiosInstance.post<{
|
|---|
| 27 | user: UserResponse;
|
|---|
| 28 | tokenExpiresIn: number;
|
|---|
| 29 | }>("/auth/register", {
|
|---|
| 30 | username,
|
|---|
| 31 | fullname,
|
|---|
| 32 | email,
|
|---|
| 33 | password,
|
|---|
| 34 | profilePhoto,
|
|---|
| 35 | });
|
|---|
| 36 | scheduleTokenRefresh(response.data.tokenExpiresIn);
|
|---|
| 37 | setUser(newUser);
|
|---|
| [24a2a2a] | 38 | // todo
|
|---|
| [0c86e77] | 39 | // setUser(response.data.user);
|
|---|
| 40 | navigate("/");
|
|---|
| 41 | } catch (error) {
|
|---|
| 42 | console.error("Registration failed:", error);
|
|---|
| 43 | }
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | return (
|
|---|
| [4022db1] | 47 | <div className="flex flex-col items-center justify-center min-h-[90vh] bg-gray-100">
|
|---|
| [0c86e77] | 48 | <h2 className="text-2xl mb-4">Register</h2>
|
|---|
| 49 | <form className="bg-white p-6 rounded shadow-md w-80">
|
|---|
| 50 | <div className="mb-4">
|
|---|
| 51 | <label className="block text-gray-700 mb-2" htmlFor="username">
|
|---|
| 52 | Username
|
|---|
| 53 | </label>
|
|---|
| 54 | <input
|
|---|
| 55 | type="text"
|
|---|
| 56 | id="username"
|
|---|
| 57 | className="w-full p-2 border border-gray-300 rounded"
|
|---|
| 58 | value={username}
|
|---|
| 59 | onChange={(e) => setUsername(e.target.value)}
|
|---|
| 60 | />
|
|---|
| 61 | </div>
|
|---|
| 62 | <div className="mb-4">
|
|---|
| 63 | <label className="block text-gray-700 mb-2" htmlFor="password">
|
|---|
| 64 | Password
|
|---|
| 65 | </label>
|
|---|
| 66 | <input
|
|---|
| 67 | type="password"
|
|---|
| 68 | id="password"
|
|---|
| 69 | className="w-full p-2 border border-gray-300 rounded"
|
|---|
| 70 | value={password}
|
|---|
| 71 | onChange={(e) => setPassword(e.target.value)}
|
|---|
| 72 | />
|
|---|
| 73 | </div>
|
|---|
| 74 | <div className="mb-4">
|
|---|
| 75 | <label className="block text-gray-700 mb-2" htmlFor="fullName">
|
|---|
| 76 | Full Name
|
|---|
| 77 | </label>
|
|---|
| 78 | <input
|
|---|
| 79 | type="text"
|
|---|
| 80 | id="fullName"
|
|---|
| 81 | className="w-full p-2 border border-gray-300 rounded"
|
|---|
| 82 | value={fullname}
|
|---|
| 83 | onChange={(e) => setFullname(e.target.value)}
|
|---|
| 84 | />
|
|---|
| 85 | </div>
|
|---|
| 86 | <div className="mb-4">
|
|---|
| 87 | <label className="block text-gray-700 mb-2" htmlFor="email">
|
|---|
| 88 | Email
|
|---|
| 89 | </label>
|
|---|
| 90 | <input
|
|---|
| 91 | type="email"
|
|---|
| 92 | id="email"
|
|---|
| 93 | className="w-full p-2 border border-gray-300 rounded"
|
|---|
| 94 | value={email}
|
|---|
| 95 | onChange={(e) => setEmail(e.target.value)}
|
|---|
| 96 | />
|
|---|
| 97 | </div>
|
|---|
| 98 | <div className="mb-4">
|
|---|
| 99 | <label className="block text-gray-700 mb-2" htmlFor="profilePhoto">
|
|---|
| 100 | Profile Photo URL
|
|---|
| 101 | </label>
|
|---|
| 102 | <input
|
|---|
| [4022db1] | 103 | placeholder="todo"
|
|---|
| [0c86e77] | 104 | type="text"
|
|---|
| 105 | id="profilePhoto"
|
|---|
| 106 | className="w-full p-2 border border-gray-300 rounded"
|
|---|
| 107 | value={profilePhoto || ""}
|
|---|
| 108 | onChange={(e) => setProfilePhoto(e.target.value)}
|
|---|
| 109 | />
|
|---|
| 110 | </div>
|
|---|
| 111 | <button
|
|---|
| 112 | type="submit"
|
|---|
| 113 | className="w-full bg-blue-500 text-white p-2 rounded hover:bg-blue-600"
|
|---|
| [24a2a2a] | 114 | onClick={(e: React.MouseEvent<HTMLButtonElement>) =>
|
|---|
| 115 | handleRegister(e)
|
|---|
| 116 | }
|
|---|
| [0c86e77] | 117 | >
|
|---|
| 118 | Register
|
|---|
| 119 | </button>
|
|---|
| 120 | </form>
|
|---|
| 121 | </div>
|
|---|
| 122 | );
|
|---|
| 123 | };
|
|---|
| 124 |
|
|---|
| 125 | export default Register;
|
|---|