Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register redirects to the game #55

Merged
merged 7 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions webapp/src/components/login/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// src/components/Login.js
import { useState } from 'react';
import { useState, KeyboardEvent } from 'react';
import axios from 'axios';
import { Container, Typography, TextField, Snackbar, Button, Stack } from '@mui/material';
import { useTranslation } from 'react-i18next';
Expand All @@ -25,13 +25,13 @@ const Login = (props: ActionProps) => {
props.goBack();
};

const loginUser = async () => {
async function loginUser () {

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

console.log(user.data);
console.log(user);
localStorage.setItem("username", user.data.username);
localStorage.setItem("score", user.data.totalScore);
localStorage.setItem("nWins", user.data.nWins);
Expand All @@ -51,6 +51,11 @@ const Login = (props: ActionProps) => {
setOpenSnackbar(false);
};

const handleKeyPress = (event: KeyboardEvent<HTMLDivElement>) => {
if(event.key === 'Enter'){
loginUser();
}
};

return (
<Container component="main" maxWidth="xs" sx={{ marginTop: 3 }}>
Expand All @@ -72,6 +77,7 @@ const Login = (props: ActionProps) => {
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
onKeyDown={handleKeyPress}
/>
<Stack direction="column" spacing={2}>
<Button color="primary" onClick={loginUser}>
Expand Down
28 changes: 26 additions & 2 deletions webapp/src/components/register/Register.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useState } from 'react';
import { useState, KeyboardEvent } from 'react';
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,17 +12,33 @@ type ActionProps = {
}

const Register = (props:ActionProps) => {
const navigate = useNavigate();
const { t } = useTranslation();
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
const [openSnackbar, setOpenSnackbar] = useState(false);

const addUser = async () => {
async function addUser () {
try {
// 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 All @@ -36,6 +53,12 @@ const Register = (props:ActionProps) => {
props.goBack();
};

const handleKeyPress = (event: KeyboardEvent<HTMLDivElement>) => {
if(event.key === 'Enter'){
addUser();
}
};

return (
<Container component="main" maxWidth="xs" sx={{ marginTop: 3 }}>
<Typography component="h1" variant="h5">
Expand All @@ -57,6 +80,7 @@ const Register = (props:ActionProps) => {
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
onKeyDown={handleKeyPress}
/>
<Stack direction="column">
<Button color="primary" onClick={addUser}>
Expand Down
Loading