return username and id in login and signup page

This commit is contained in:
slawk0
2025-01-05 15:19:58 +01:00
parent 892b394836
commit 33ffc2aeb4
3 changed files with 19 additions and 8 deletions

View File

@@ -13,7 +13,7 @@ export type Inputs = {
};
export default function Login() {
const { setAuthorized } = useContext(AuthContext);
const { setUser, setAuthorized } = useContext(AuthContext);
const [message, setMessage] = useState('');
const navigate = useNavigate();
const [IsLoading, setIsLoading] = useState<boolean>(false);
@@ -27,11 +27,12 @@ export default function Login() {
.post('/api/auth/login', data, {
withCredentials: true,
})
.then(() => {
.then((res) => {
setUser({ username: res.data.username, id: res.data.user_id });
setAuthorized(true);
setIsLoading(false);
console.log('redirecting');
navigate('/');
navigate('/chat');
})
.catch((err) => {
if (err.response) {

View File

@@ -13,7 +13,8 @@ type Inputs = {
};
export default function Signup() {
const { setAuthorized } = useContext(AuthContext);
const { setUser, setAuthorized } = useContext(AuthContext);
const {
register,
handleSubmit,
@@ -41,10 +42,11 @@ export default function Signup() {
.post(`/api/auth/signup`, data, {
withCredentials: true,
})
.then(() => {
.then((res) => {
setUser({ username: res.data.username, id: res.data.user_id });
setAuthorized(true);
setIsLoading(false);
navigate('/');
navigate('/chat');
console.log('Signed up');
})
.catch((err) => {

View File

@@ -139,7 +139,11 @@ app.post("/api/auth/signup", async (req, res) => {
// secure: true,
});
return res.status(200).json({ message: "Successfully signed up" });
return res.status(200).json({
message: "Successfully signed up",
username: username,
user_id: user_id,
});
} catch (e) {
console.error("Signup error: ", e);
return res.status(500).json({ message: "internal server error" });
@@ -185,7 +189,11 @@ app.post("/api/auth/login", async (req, res) => {
res.cookie("token", token, {
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
});
return res.status(200).json({ message: "Successfully logged In" });
return res.status(200).json({
message: "Successfully logged In",
username: username,
user_id: user_id,
});
})
.catch((e) => {
console.error("Failed to compare password: ", e);