Client will now login immediately after registration
This commit is contained in:
+43
-12
@@ -1,6 +1,8 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import config from "../config";
|
import config from "../config";
|
||||||
|
import { useAuth } from "../AuthContext";
|
||||||
|
|
||||||
|
|
||||||
const styles = {
|
const styles = {
|
||||||
form: {
|
form: {
|
||||||
@@ -45,6 +47,8 @@ const styles = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const Register = () => {
|
const Register = () => {
|
||||||
|
const { login } = useAuth();
|
||||||
|
|
||||||
const [form, setForm] = useState({
|
const [form, setForm] = useState({
|
||||||
username: "",
|
username: "",
|
||||||
email: "",
|
email: "",
|
||||||
@@ -60,27 +64,54 @@ const Register = () => {
|
|||||||
const handleSubmit = async (e) => {
|
const handleSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
username: form.username,
|
||||||
|
password: form.password,
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${config.apiBaseUrl}/api/Auth/register`, {
|
// Step 1: Register the user
|
||||||
|
const registerRes = await fetch(
|
||||||
|
`${config.apiBaseUrl}/api/Auth/register`,
|
||||||
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify(payload),
|
||||||
username: form.username,
|
}
|
||||||
email: form.email,
|
);
|
||||||
password: form.password,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!registerRes.ok) {
|
||||||
const data = await res.json();
|
const errorData = await registerRes.json();
|
||||||
throw new Error(data.message || "Registration failed");
|
throw new Error(errorData.message || "Registration failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
navigate("/login");
|
// Consume body even if empty to ensure flush
|
||||||
|
await registerRes.text(); // <-- this is the key to making chaining clean
|
||||||
|
|
||||||
|
// Step 2: Immediately login
|
||||||
|
const loginRes = await fetch(`${config.apiBaseUrl}/api/Auth/login`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!loginRes.ok) {
|
||||||
|
throw new Error("Registration succeeded but login failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
const loginData = await loginRes.json();
|
||||||
|
login(loginData.accessToken);
|
||||||
|
|
||||||
|
localStorage.setItem("token", loginData.accessToken);
|
||||||
|
localStorage.setItem("role", loginData.role);
|
||||||
|
|
||||||
|
navigate("/dashboard");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Register error:", err);
|
console.error("Register/login error:", err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user