login in frontend
This commit is contained in:
		
							parent
							
								
									cfabd4e7ff
								
							
						
					
					
						commit
						5ac961330e
					
				|  | @ -69,3 +69,37 @@ export const postUser = async (content) => { | ||||||
|         throw new Error('Failed to save user. ' || error.message); |         throw new Error('Failed to save user. ' || error.message); | ||||||
|     } |     } | ||||||
| }; | }; | ||||||
|  | 
 | ||||||
|  | export const loginUser = async (loginValue) => { | ||||||
|  |     const protocol = process.env.REACT_APP_API_PROTOCOL; // "http"
 | ||||||
|  |     const host = process.env.REACT_APP_API_HOST; // "localhost"
 | ||||||
|  |     const port = process.env.REACT_APP_API_PORT; // "8080"
 | ||||||
|  |     const path = process.env.REACT_APP_API_PATH; // "/api"
 | ||||||
|  |     const portPart = port ? `:${port}` : ''; // port is optional
 | ||||||
|  |     const API_URL = `${protocol}://${host}${portPart}${path}`; | ||||||
|  | 
 | ||||||
|  |     try { | ||||||
|  | 
 | ||||||
|  |         const response = await fetch(`${API_URL}/users/login`, { | ||||||
|  |             method: 'POST', | ||||||
|  |             headers: { | ||||||
|  |                 'Content-Type': 'application/json' | ||||||
|  |             }, | ||||||
|  |             body: JSON.stringify({ | ||||||
|  |                 email: `${loginValue.email}`, | ||||||
|  |                 password: `${loginValue.password}`, | ||||||
|  |             }) | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         if (!response.ok) { | ||||||
|  |             const errorData = await response.json; | ||||||
|  |             throw new Error(errorData.message || 'Server response failed.'); | ||||||
|  |         } | ||||||
|  |         const data = response; | ||||||
|  |         console.log('User successfully logged in:', data); | ||||||
|  |         return true; | ||||||
|  |     } catch (error) { | ||||||
|  |         console.error('Failed to log in:', error.message); | ||||||
|  |         throw new Error('Failed to log in. ' || error.message); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -4,7 +4,7 @@ import { Outlet, Link } from "react-router-dom"; | ||||||
|  * Layout |  * Layout | ||||||
|  * @author Peter Rutschmann |  * @author Peter Rutschmann | ||||||
|  */ |  */ | ||||||
| const Layout = ({loginValues}) => { | const Layout = ({ loginValues }) => { | ||||||
|     return ( |     return ( | ||||||
|         <> |         <> | ||||||
|             <nav> |             <nav> | ||||||
|  | @ -38,8 +38,8 @@ const Layout = ({loginValues}) => { | ||||||
|                     </li> |                     </li> | ||||||
|                 </ul> |                 </ul> | ||||||
|             </nav> |             </nav> | ||||||
|             <hr/> |             <hr /> | ||||||
|             <Outlet/> |             <Outlet /> | ||||||
|         </> |         </> | ||||||
|     ) |     ) | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -1,16 +1,27 @@ | ||||||
| import { useNavigate } from 'react-router-dom'; | import { useNavigate } from 'react-router-dom'; | ||||||
|  | import { loginUser } from '../../comunication/FetchUser'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * LoginUser |  * LoginUser | ||||||
|  * @author Peter Rutschmann |  * @author Peter Rutschmann | ||||||
|  */ |  */ | ||||||
| function LoginUser({loginValues, setLoginValues}) { | function LoginUser({ loginValues, setLoginValues }) { | ||||||
|     const navigate = useNavigate(); |     const navigate = useNavigate(); | ||||||
| 
 | 
 | ||||||
|     const handleSubmit = async (e) => { |     const handleSubmit = async (e) => { | ||||||
|         e.preventDefault(); |         e.preventDefault(); | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             let isLoginValid = false | ||||||
|  |             isLoginValid = await loginUser(loginValues); | ||||||
|             console.log(loginValues); |             console.log(loginValues); | ||||||
|         navigate('/') |             if (isLoginValid) { | ||||||
|  |                 setLoginValues({ userName: loginValues.email, password: loginValues.password }); | ||||||
|  |                 navigate('/'); | ||||||
|  |             } | ||||||
|  |         } catch (error) { | ||||||
|  |             console.error('Failed to fetch to server:', error.message); | ||||||
|  |         } | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     return ( |     return ( | ||||||
|  | @ -25,7 +36,7 @@ function LoginUser({loginValues, setLoginValues}) { | ||||||
|                                 type="text" |                                 type="text" | ||||||
|                                 value={loginValues.email} |                                 value={loginValues.email} | ||||||
|                                 onChange={(e) => |                                 onChange={(e) => | ||||||
|                                     setLoginValues(prevValues => ({...prevValues, email: e.target.value}))} |                                     setLoginValues(prevValues => ({ ...prevValues, email: e.target.value }))} | ||||||
|                                 required |                                 required | ||||||
|                                 placeholder="Please enter your email *" |                                 placeholder="Please enter your email *" | ||||||
|                             /> |                             /> | ||||||
|  | @ -36,7 +47,7 @@ function LoginUser({loginValues, setLoginValues}) { | ||||||
|                                 type="text" |                                 type="text" | ||||||
|                                 value={loginValues.password} |                                 value={loginValues.password} | ||||||
|                                 onChange={(e) => |                                 onChange={(e) => | ||||||
|                                     setLoginValues(prevValues => ({...prevValues, password: e.target.value}))} |                                     setLoginValues(prevValues => ({ ...prevValues, password: e.target.value }))} | ||||||
|                                 required |                                 required | ||||||
|                                 placeholder="Please enter your password *" |                                 placeholder="Please enter your password *" | ||||||
|                             /> |                             /> | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue