Skip to content

Commit

Permalink
register redirects to the game
Browse files Browse the repository at this point in the history
  • Loading branch information
carolbgmm committed Mar 18, 2024
1 parent 2d65837 commit 944b5ed
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions webapp/src/components/register/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from 'axios';
import { Container, Typography, TextField, Snackbar, Stack, Button } from '@mui/material';
import { useTranslation } from 'react-i18next';
import './Register.scss';
import { useNavigate } from "react-router-dom";

const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';

Expand All @@ -11,6 +12,7 @@ type ActionProps = {
}

const Register = (props:ActionProps) => {
const navigate = useNavigate();
const { t } = useTranslation();
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
Expand All @@ -22,6 +24,21 @@ const Register = (props:ActionProps) => {
// checkear que el username no exista (tiene que ser unico)
await axios.post(`${apiEndpoint}/adduser`, { username, password });
setOpenSnackbar(true);

localStorage.clear();
const user = await axios.post(`${apiEndpoint}/login`, { username, password });

console.log(user.data);
localStorage.setItem("username", user.data.username);
localStorage.setItem("score", user.data.totalScore);
localStorage.setItem("nWins", user.data.nWins);
localStorage.setItem("uuid", user.data.uuid);
localStorage.setItem("isAuthenticated", JSON.stringify(true));
// Extract data from the response
localStorage.setItem('userUUID', user.data.uuid);

setOpenSnackbar(true);
navigate("/game")
} catch (error) {
setError(error.response.data.error);
}
Expand Down

0 comments on commit 944b5ed

Please sign in to comment.