aki-008 commited on
Commit
23c3caf
·
1 Parent(s): acceac1

chore: signup page

Browse files
Frontend/src/components/auth/SignUp.tsx CHANGED
@@ -1,11 +1,12 @@
1
  import React, { useState } from "react";
2
  import { User, Mail, Lock, UserPlus } from "lucide-react";
3
- import API from "../../api/api"; // Assuming the API file is one directory up
4
 
5
  interface SignUpProps {
6
  onClose: () => void;
7
  onSwitchToSignIn: () => void;
8
- onAuthSuccess: (username: string) => void;
 
9
  }
10
 
11
  const SignUp: React.FC<SignUpProps> = ({ onSwitchToSignIn, onAuthSuccess }) => {
@@ -24,34 +25,33 @@ const SignUp: React.FC<SignUpProps> = ({ onSwitchToSignIn, onAuthSuccess }) => {
24
  }
25
 
26
  try {
27
- // 1. Registration via API (uses axios and the correct base URL)
28
  await API.post("/auth/register", {
29
  username,
30
  email,
31
  password,
32
  });
33
 
34
- // 2. Login immediately after successful registration
35
  const loginResponse = await API.post("/auth/login", {
36
  email,
37
  password,
38
  });
39
 
40
- // The response data is directly on response.data when using axios
41
- const loginData = loginResponse.data;
42
- localStorage.setItem("token", loginData.access_token);
43
- const loggedInUsername = loginData.username;
44
- onAuthSuccess(loggedInUsername);
 
45
  } catch (err: any) {
46
  console.error("Registration/Login error:", err);
47
 
48
- // Axios error handling: Check for response data and detail for custom error message
49
  let errorMessage = "Registration failed";
 
50
  if (err.response && err.response.data && err.response.data.detail) {
51
- // Extracts the specific error detail from FastAPI (e.g., 'Username already registered')
52
  errorMessage = err.response.data.detail;
53
  } else if (err.message) {
54
- // Use generic network or request error message
55
  errorMessage = err.message;
56
  }
57
 
@@ -126,7 +126,7 @@ const SignUp: React.FC<SignUpProps> = ({ onSwitchToSignIn, onAuthSuccess }) => {
126
 
127
  <button
128
  onClick={handleSubmit}
129
- className="w-full px-5 py-3 rounded-lg bg-linear-to-r from-blue-500 to-purple-500 text-white flex items-center justify-center gap-2 hover:from-blue-600 hover:to-purple-600 transition"
130
  >
131
  <UserPlus className="w-5 h-5" />
132
  Sign Up
@@ -135,7 +135,10 @@ const SignUp: React.FC<SignUpProps> = ({ onSwitchToSignIn, onAuthSuccess }) => {
135
 
136
  <p className="mt-8 text-center text-sm text-gray-400">
137
  Already have an account?
138
- <button onClick={onSwitchToSignIn} className="text-blue-400 ml-2 hover:underline">
 
 
 
139
  Sign In
140
  </button>
141
  </p>
@@ -143,4 +146,4 @@ const SignUp: React.FC<SignUpProps> = ({ onSwitchToSignIn, onAuthSuccess }) => {
143
  );
144
  };
145
 
146
- export default SignUp;
 
1
  import React, { useState } from "react";
2
  import { User, Mail, Lock, UserPlus } from "lucide-react";
3
+ import API from "../../api/api";
4
 
5
  interface SignUpProps {
6
  onClose: () => void;
7
  onSwitchToSignIn: () => void;
8
+ // ✅ UPDATED: Signature now includes token
9
+ onAuthSuccess: (username: string, token: string) => void;
10
  }
11
 
12
  const SignUp: React.FC<SignUpProps> = ({ onSwitchToSignIn, onAuthSuccess }) => {
 
25
  }
26
 
27
  try {
28
+ // 1. Register via API
29
  await API.post("/auth/register", {
30
  username,
31
  email,
32
  password,
33
  });
34
 
35
+ // 2. Login immediately after to get the token
36
  const loginResponse = await API.post("/auth/login", {
37
  email,
38
  password,
39
  });
40
 
41
+ // Extract token and username
42
+ const { access_token, username: loggedInUser } = loginResponse.data;
43
+
44
+ // 3. Pass both to the parent handler
45
+ [cite_start]; // This ensures the AuthContext gets the token to persist the session [cite: 334, 335, 336]
46
+ onAuthSuccess(loggedInUser, access_token);
47
  } catch (err: any) {
48
  console.error("Registration/Login error:", err);
49
 
 
50
  let errorMessage = "Registration failed";
51
+ // Handle FastAPI error details
52
  if (err.response && err.response.data && err.response.data.detail) {
 
53
  errorMessage = err.response.data.detail;
54
  } else if (err.message) {
 
55
  errorMessage = err.message;
56
  }
57
 
 
126
 
127
  <button
128
  onClick={handleSubmit}
129
+ className="w-full px-5 py-3 rounded-lg bg-gradient-to-r from-blue-500 to-purple-500 text-white flex items-center justify-center gap-2 hover:from-blue-600 hover:to-purple-600 transition"
130
  >
131
  <UserPlus className="w-5 h-5" />
132
  Sign Up
 
135
 
136
  <p className="mt-8 text-center text-sm text-gray-400">
137
  Already have an account?
138
+ <button
139
+ onClick={onSwitchToSignIn}
140
+ className="text-blue-400 ml-2 hover:underline"
141
+ >
142
  Sign In
143
  </button>
144
  </p>
 
146
  );
147
  };
148
 
149
+ export default SignUp;