From 6a186f9e33da5fe2b6eacd86406b57de981b60db Mon Sep 17 00:00:00 2001 From: nayan-kute21 Date: Thu, 2 Oct 2025 01:46:50 +0530 Subject: [PATCH 1/4] feat:integratee register api fixes #5 --- src/App.jsx | 7 ++++- src/api/api.config.js | 4 +-- src/api/auth/register.js | 19 +++++++------ src/pages/RegisterPage.jsx | 57 ++++++++++++++++++++++++++++++-------- 4 files changed, 64 insertions(+), 23 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 7281dd5..e3a0567 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,11 +1,16 @@ import { useState } from "react"; import { RouterProvider } from "react-router-dom"; +import { MyProvider } from "./context/MyContext"; import routes from "./routes"; import "./App.css"; function App() { - return ; + return ( + + + + ); } export default App; diff --git a/src/api/api.config.js b/src/api/api.config.js index 68cd41e..5414e7c 100644 --- a/src/api/api.config.js +++ b/src/api/api.config.js @@ -1,8 +1,8 @@ -export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000'; +export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080'; export const endpoints = { // Auth endpoints - register: `${API_BASE_URL}/api/v1/users/register`, + register: `${API_BASE_URL}/users/create`, login: `${API_BASE_URL}/api/v1/users/login`, logout: `${API_BASE_URL}/api/v1/users/logout`, diff --git a/src/api/auth/register.js b/src/api/auth/register.js index 7eb8cf9..f2913c3 100644 --- a/src/api/auth/register.js +++ b/src/api/auth/register.js @@ -3,19 +3,22 @@ import api from "../api.intercepter"; /** * Register function that creates a new user account - * @param {string} firstName - User's first name - * @param {string} lastName - User's last name - * @param {string} email - User email - * @param {string} password - User password - * @returns {Promise} - Promise that resolves to the registration response + * @param {Object} userData - User registration data + * @param {string} userData.name - User's full name + * @param {string} userData.username - User's username + * @param {string} userData.email - User email + * @param {string} userData.password - User password + * @returns {Promise} - Promise that resolves to the registration response (timestamp) */ -export const registerAPI = async (firstName, lastName, email, password) => { +export const registerAPI = async (userData) => { try { + const { name, username, email, password } = userData; const response = await api.post(endpoints.register, { - firstName, - lastName, + name, + username, email, password, + blogs: [] }); return response.data; } catch (error) { diff --git a/src/pages/RegisterPage.jsx b/src/pages/RegisterPage.jsx index 510f652..775a495 100644 --- a/src/pages/RegisterPage.jsx +++ b/src/pages/RegisterPage.jsx @@ -2,11 +2,13 @@ import { useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import { useMyContext } from "../context/MyContext"; import { FiEye, FiEyeOff } from "react-icons/fi"; +import { registerAPI } from "../api"; import "./AuthPages.css"; const RegisterPage = () => { const [formData, setFormData] = useState({ name: "", + username: "", email: "", password: "", confirmPassword: "", @@ -15,7 +17,7 @@ const RegisterPage = () => { const [isLoading, setIsLoading] = useState(false); const [showPassword1, setShowPassword1] = useState(false); const [showPassword2, setShowPassword2] = useState(false); - const { updateUserProfile, setLoadingState, setErrorState } = useMyContext(); + const { setLoadingState, setErrorState } = useMyContext(); const navigate = useNavigate(); const handleChange = (e) => { @@ -42,6 +44,14 @@ const RegisterPage = () => { newErrors.name = "Name must be at least 2 characters"; } + if (!formData.username.trim()) { + newErrors.username = "Username is required"; + } else if (formData.username.trim().length < 3) { + newErrors.username = "Username must be at least 3 characters"; + } else if (!/^[a-zA-Z0-9_]+$/.test(formData.username)) { + newErrors.username = "Username can only contain letters, numbers, and underscores"; + } + if (!formData.email.trim()) { newErrors.email = "Email is required"; } else if (!/\S+@\S+\.\S+/.test(formData.email)) { @@ -75,20 +85,24 @@ const RegisterPage = () => { setLoadingState(true); try { - // Simulate API call - replace with actual registration logic - await new Promise((resolve) => setTimeout(resolve, 2000)); - - // Mock successful registration - replace with actual API response - const mockUser = { - id: Date.now(), + // Call the actual register API + const timestamp = await registerAPI({ name: formData.name, + username: formData.username, email: formData.email, - }; - - updateUserProfile(mockUser); - navigate("/dashboard"); + password: formData.password + }); + + // Registration successful - redirect to dashboard + setErrorState(""); // Clear any previous errors + navigate("/dashboard", { + state: { + message: "Registration successful! Please login with your credentials.", + email: formData.email + } + }); } catch (error) { - setErrorState("Registration failed. Please try again."); + setErrorState(error.message || "Registration failed. Please try again."); } finally { setIsLoading(false); setLoadingState(false); @@ -124,6 +138,25 @@ const RegisterPage = () => { )} +
+ + + {errors.username && ( + {errors.username} + )} +
+