If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
-Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
+Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc.) right into your project so you have full control over them. All the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
-You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
+You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you could not customize it when you are ready for it.
## Learn More
* @file Authentication context for user and role data
*/
-import React, { createContext, useState, useEffect } from 'react';
+import React, { createContext, useEffect, useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useSnackbar } from 'notistack';
import { AuthService } from '../services/auth';
-export const AuthContext = createContext();
+export const AuthContext = createContext( undefined );
-export const AuthProvider = ({ children }) => {
- const [user, setUser] = useState(null);
+export const AuthProvider = ( { children } ) => {
+ const [user, setUser] = useState( null );
const navigate = useNavigate();
const { enqueueSnackbar } = useSnackbar();
- useEffect(() => {
- const fetchUser = async () => {
- try {
- const token = localStorage.getItem('token');
- if (token) {
- const response = await AuthService.getCurrentUser();
- setUser(response.user);
- }
- } catch (error) {
- enqueueSnackbar(`Error fetching user: ${error.message}`, { variant: 'error' });
+ const fetchUser = useMemo( () => async () => {
+ try {
+ const token = localStorage.getItem( 'token' );
+ if (token) {
+ const response = await AuthService.getCurrentUser();
+ setUser( response.user );
}
- };
- fetchUser();
- }, [enqueueSnackbar]);
+ } catch (error) {
+ enqueueSnackbar( `Error fetching user: ${ error.message }`, { variant:'error' } );
+ }
+ }, [enqueueSnackbar] );
+ useEffect( () => {
+ fetchUser().catch();
+ }, [enqueueSnackbar, fetchUser] );
- const login = async (email, password) => {
+ const login = async ( email, password ) => {
try {
- const response = await AuthService.login(email, password);
+ const response = await AuthService.login( email, password );
if (response.success) {
- localStorage.setItem('token', response.token);
- setUser(response.user);
- navigate('/');
+ localStorage.setItem( 'token', response.token );
+ setUser( response.user );
+ navigate( '/' );
return true;
}
return false;
} catch (error) {
- enqueueSnackbar(`Login error: ${error.message}`, { variant: 'error' });
+ enqueueSnackbar( `Login error: ${ error.message }`, { variant:'error' } );
return false;
}
};
const logout = () => {
- localStorage.removeItem('token');
- setUser(null);
- navigate('/login');
+ localStorage.removeItem( 'token' );
+ setUser( null );
+ navigate( '/login' );
};
return (
- <AuthContext.Provider value={{ user, setUser, login, logout }}>
- {children}
+ <AuthContext.Provider value={ { user, setUser, login, logout } }>
+ { children }
</AuthContext.Provider>
);
};
\ No newline at end of file