Skip to content

Commit 8cefcf1

Browse files
authored
Merge pull request #188 from Code-4-Community/147-dev---account-creation-bug
147 dev account creation bug
2 parents 5ccbe8a + 977e8d3 commit 8cefcf1

File tree

2 files changed

+41
-50
lines changed

2 files changed

+41
-50
lines changed

frontend/src/Register.tsx

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React, { useState } from "react";
2-
import { setAuthState } from "./external/bcanSatchel/actions";
32
import { observer } from "mobx-react-lite";
43
import { useNavigate } from "react-router-dom";
54
import logo from "./images/bcan_logo.svg";
6-
import { api } from "./api";
5+
import { useAuthContext } from "./context/auth/authContext";
76

87
/**
98
* Register a new BCAN user
@@ -14,41 +13,15 @@ const Register = observer(() => {
1413
const [password, setPassword] = useState("");
1514
const navigate = useNavigate();
1615

16+
const { register } = useAuthContext();
17+
1718
const handleSubmit = async (e: React.FormEvent) => {
1819
e.preventDefault();
19-
20-
try {
21-
const response = await api("/auth/register", {
22-
method: "POST",
23-
headers: { "Content-Type": "application/json" },
24-
body: JSON.stringify({ username, password, email }),
25-
});
26-
27-
const data = await response.json();
28-
29-
if (!response.ok) {
30-
alert(data.message || "Registration failed.");
31-
return;
32-
}
33-
34-
// If registration succeeded, automatically log in the user
35-
const loginResponse = await api("/auth/login", {
36-
method: "POST",
37-
headers: { "Content-Type": "application/json" },
38-
body: JSON.stringify({ username, password }),
39-
});
40-
41-
const loginData = await loginResponse.json();
42-
43-
if (loginResponse.ok && loginData.access_token) {
44-
setAuthState(true, loginData.user, loginData.access_token);
45-
navigate("/grant-info");
46-
} else {
47-
alert(loginData.message || "Login after registration failed.");
48-
}
49-
} catch (error) {
50-
console.error("Error during registration:", error);
51-
alert("An error occurred while registering. Please try again later.");
20+
const success = await register(username, password, email);
21+
if (success) {
22+
navigate("/login");
23+
} else {
24+
console.warn("Registration failed");
5225
}
5326
};
5427

@@ -80,6 +53,7 @@ const Register = observer(() => {
8053
>
8154
← Back to Sign In
8255
</button>
56+
8357

8458
{/* Username field */}
8559
<label htmlFor="username" style={styles.label}>

frontend/src/context/auth/authContext.tsx

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { api } from '../../api';
1111
interface AuthContextProps {
1212
isAuthenticated: boolean;
1313
login: (username: string, password: string) => Promise<boolean>;
14-
register: (username: string, password: string, email: string) => Promise<void>;
14+
register: (username: string, password: string, email: string) => Promise<boolean>;
1515
logout: () => void;
1616
user: User | null;
1717
}
@@ -57,24 +57,41 @@ export const AuthProvider = observer(({ children }: { children: ReactNode }) =>
5757
/**
5858
* Register a new user and automatically log them in
5959
*/
60-
const register = async (username: string, password: string, email: string) => {
61-
62-
const response = await api('/auth/register', {
63-
method: 'POST',
64-
headers: { 'Content-Type': 'application/json' },
65-
body: JSON.stringify({ username, password, email }),
66-
});
67-
68-
const data = await response.json();
69-
if (response.ok) {
70-
// log the user in after registration
71-
await login(username, password);
72-
} else {
73-
alert(data.message || 'Registration failed');
60+
const register = async (username: string, password: string, email: string): Promise<boolean> => {
61+
try {
62+
const response = await api('/auth/register', {
63+
method: 'POST',
64+
headers: { 'Content-Type': 'application/json' },
65+
body: JSON.stringify({ username, password, email }),
66+
});
67+
68+
const data = await response.json();
69+
70+
if (response.ok) {
71+
const loggedIn = await login(username, password);
72+
if (loggedIn) return true;
73+
console.warn('User registered but auto-login failed');
74+
return false;
75+
}
76+
77+
if (response.status === 409 || data.message?.includes('exists')) {
78+
alert('An account with this username or email already exists.');
79+
} else if (response.status === 400) {
80+
alert(data.message || 'Invalid registration details.');
81+
} else {
82+
alert('Registration failed. Please try again later.');
83+
}
84+
85+
return false;
86+
} catch (error) {
87+
console.error('Error during registration:', error);
88+
alert('An unexpected error occurred. Please try again later.');
89+
return false;
7490
}
7591
};
7692

7793

94+
7895
/** Log out the user */
7996
const logout = () => {
8097
api('/auth/logout', { method: 'POST' });

0 commit comments

Comments
 (0)