after login, redirect to /boxes

This commit is contained in:
Steve White 2024-10-08 08:50:10 -05:00
parent a9e976ecd3
commit a618a3d602
2 changed files with 4 additions and 2 deletions

View File

@ -2,11 +2,12 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { Button, TextField, Container, Typography } from '@mui/material'; import { Button, TextField, Container, Typography } from '@mui/material';
import axios from 'axios'; import axios from 'axios';
import { useNavigate } from 'react-router-dom'; // Import useNavigate
export default function Login({ setToken }) { export default function Login({ setToken }) {
const [username, setUsername] = useState(''); const [username, setUsername] = useState('');
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const navigate = useNavigate(); // Initialize useNavigate
const handleLogin = async (e) => { const handleLogin = async (e) => {
e.preventDefault(); e.preventDefault();
try { try {
@ -14,6 +15,7 @@ export default function Login({ setToken }) {
const response = await axios.post(`${process.env.REACT_APP_API_URL}/login`, { username, password }); const response = await axios.post(`${process.env.REACT_APP_API_URL}/login`, { username, password });
setToken(response.data.token); // Store the token in state or context setToken(response.data.token); // Store the token in state or context
navigate('/boxes'); //redirect to /boxes after successful login
} catch (error) { } catch (error) {
console.error('Login failed', error); console.error('Login failed', error);
} }