ConceptPortal-public/rsconcept/frontend/src/context/AuthContext.tsx

104 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-07-15 17:46:19 +03:00
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
2023-07-20 17:11:03 +03:00
import { ICurrentUser, IUserSignupData } from '../utils/models';
2023-07-15 17:46:19 +03:00
import { ErrorInfo } from '../components/BackendError';
import useLocalStorage from '../hooks/useLocalStorage';
2023-07-20 17:11:03 +03:00
import { getAuth, postLogin, postLogout, postSignup } from '../utils/backendAPI';
2023-07-15 17:46:19 +03:00
interface IAuthContext {
user: ICurrentUser | undefined
login: (username: string, password: string, onSuccess?: () => void) => void
logout: (onSuccess?: () => void) => void
signup: (data: IUserSignupData, onSuccess?: () => void) => void
2023-07-15 17:46:19 +03:00
loading: boolean
error: ErrorInfo
setError: (error: ErrorInfo) => void
}
export const AuthContext = createContext<IAuthContext>({
user: undefined,
login: () => {},
logout: () => {},
signup: () => {},
loading: false,
error: '',
setError: () => {}
});
interface AuthStateProps {
children: React.ReactNode
}
export const AuthState = ({ children }: AuthStateProps) => {
const [user, setUser] = useLocalStorage<ICurrentUser | undefined>('user', undefined);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<ErrorInfo>(undefined);
const loadCurrentUser = useCallback(
async () => {
getAuth({
onError: error => setUser(undefined),
onSucccess: response => {
if (response.data.id) {
setUser(response.data);
} else {
setUser(undefined)
}
2023-07-15 17:46:19 +03:00
}
});
}, [setUser]
);
async function login(uname: string, pw: string, onSuccess?: () => void) {
2023-07-15 17:46:19 +03:00
setError(undefined);
postLogin({
data: {username: uname, password: pw},
showError: true,
setLoading: setLoading,
onError: error => setError(error),
onSucccess: response => {
loadCurrentUser();
if(onSuccess) onSuccess();
2023-07-15 17:46:19 +03:00
}
});
}
async function logout(onSuccess?: () => void) {
2023-07-15 17:46:19 +03:00
setError(undefined);
postLogout({
showError: true,
onSucccess: response => {
loadCurrentUser();
if(onSuccess) onSuccess();
2023-07-15 17:46:19 +03:00
}
});
}
async function signup(data: IUserSignupData, onSuccess?: () => void) {
2023-07-15 17:46:19 +03:00
setError(undefined);
postSignup({
data: data,
showError: true,
setLoading: setLoading,
onError: error => setError(error),
onSucccess: response => {
loadCurrentUser();
if(onSuccess) onSuccess();
2023-07-15 17:46:19 +03:00
}
});
}
useEffect(() => {
loadCurrentUser();
}, [loadCurrentUser])
return (
<AuthContext.Provider
value={{ user, login, logout, signup, loading, error, setError }}
>
{children}
</AuthContext.Provider>
);
};
export const useAuth = () => useContext(AuthContext);