Skip to content
Snippets Groups Projects
Commit 797a5a23 authored by Cross, Liam (UG - Comp Sci & Elec Eng)'s avatar Cross, Liam (UG - Comp Sci & Elec Eng)
Browse files

Merge branch 'LC/login-wiring' into 'main'

Login now calls backend

See merge request ma03081/COM3014!13
parents e07f3ebe 3813cfc3
No related branches found
No related tags found
No related merge requests found
...@@ -9,4 +9,4 @@ ...@@ -9,4 +9,4 @@
.login-card { .login-card {
width: 20vw; width: 20vw;
min-width: 350px; min-width: 350px;
} }
\ No newline at end of file
import { useState } from 'react'; import { useState } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { AxiosError } from 'axios';
import { useAuth } from '../../hooks/useAuth'; import { useAuth } from '../../hooks/useAuth';
import { loginUser } from '../../services/Login/Login';
import './Login.scss'; import './Login.scss';
interface ILogin { export interface ILoginForm {
email: string; email: string;
password: string; password: string;
} }
export function Login() { export function Login() {
const [error, setError] = useState(''); const { giveAuth, updateUser } = useAuth();
const { giveAuth } = useAuth();
const navigate = useNavigate(); const navigate = useNavigate();
const { register, handleSubmit } = useForm<ILogin>({mode: 'onChange'}); const [error, setError] = useState('');
const { register, handleSubmit } = useForm<ILoginForm>({mode: 'onChange'});
const onSubmit = async (formValue: ILoginForm) => {
setError('');
try {
const result = await loginUser(formValue);
giveAuth();
updateUser(result.data);
navigate('/customer-dashboard');
} catch (error) {
const errorMessage = (error as AxiosError).response?.data;
const onSubmit = (formValue: ILogin) => { if (typeof errorMessage == 'string') {
setError('TODO: remove me once actual errors are implemented'); setError(errorMessage);
console.log('ready to make login api call', formValue); } else {
giveAuth(); setError('An unexpected error has occurred');
navigate('/'); }
}
}; };
return ( return (
...@@ -51,4 +65,4 @@ export function Login() { ...@@ -51,4 +65,4 @@ export function Login() {
); );
} }
export default Login; export default Login;
\ No newline at end of file
...@@ -39,7 +39,13 @@ export function Register() { ...@@ -39,7 +39,13 @@ export function Register() {
updateUser(result.data); updateUser(result.data);
navigate('/customer-dashboard'); navigate('/customer-dashboard');
} catch (error) { } catch (error) {
setError(((error as AxiosError).response?.data ?? 'unexpected error') as string); const errorMessage = (error as AxiosError).response?.data;
if (typeof errorMessage == 'string') {
setError(errorMessage);
} else {
setError('An unexpected error has occurred');
}
} }
}; };
......
import { AxiosResponse } from 'axios';
import Api from '../../helpers/Api';
import { ILoginForm } from '../../components/Login/Login';
import { IUser } from '../../providers/AuthProvider';
export async function loginUser(form: ILoginForm): Promise<AxiosResponse<IUser>> {
return Api.post('User/login', {
Email: form.email,
Password: form.password
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment